comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java @ 806:2cea76f1112e

Added Javadoc in utils package. gnv-artifacts/trunk@888 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 08 Apr 2010 13:10:39 +0000
parents c4156275c1e1
children a645bd23c1c8
comparison
equal deleted inserted replaced
805:bb7afd783321 806:2cea76f1112e
30 import org.w3c.dom.Node; 30 import org.w3c.dom.Node;
31 31
32 import org.xml.sax.SAXException; 32 import org.xml.sax.SAXException;
33 33
34 /** 34 /**
35 * This class provides some methods for creating and working with xml documents.
36 *
35 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a> 37 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
36 * 38 *
37 */ 39 */
38 public class ArtifactXMLUtilities implements Serializable { 40 public class ArtifactXMLUtilities implements Serializable {
39 /** 41 /**
49 51
50 public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; 52 public static final String XFORM_URL = "http://www.w3.org/2002/xforms";
51 public static final String XFORM_PREFIX = "xform"; 53 public static final String XFORM_PREFIX = "xform";
52 54
53 /** 55 /**
54 * Constructor 56 * Constructor.<br>
57 * <b>Note:</b> It should not be necessary to create an object of this
58 * class - which is a helper class. Call static methods instead.
55 */ 59 */
56 public ArtifactXMLUtilities() { 60 public ArtifactXMLUtilities() {
57 } 61 }
58 62
59 /** 63 /**
60 * @param document 64 * Creates an <code>Element</code> and returns it.
61 * @return 65 *
66 * @param document A document
67 * @param name Name of a node.
68 * @return an Element.
62 */ 69 */
63 public static Element createArtifactElement(Document document, String name) { 70 public static Element createArtifactElement(Document document, String name) {
64 Element node = document.createElementNS( 71 Element node = document.createElementNS(
65 ArtifactNamespaceContext.NAMESPACE_URI, name); 72 ArtifactNamespaceContext.NAMESPACE_URI, name);
66 node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX); 73 node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX);
67 return node; 74 return node;
68 } 75 }
69 76
77
78 /**
79 * Turns an xml document into a string.
80 *
81 * @param document An xml document.
82 * @return String representation of <i>document</i>.
83 */
70 public static String writeDocument2String(Document document) { 84 public static String writeDocument2String(Document document) {
71 try { 85 try {
72 TransformerFactory transformerFactory = TransformerFactory 86 TransformerFactory transformerFactory = TransformerFactory
73 .newInstance(); 87 .newInstance();
74 Transformer transformer = transformerFactory.newTransformer(); 88 Transformer transformer = transformerFactory.newTransformer();
85 log.error(e, e); 99 log.error(e, e);
86 } 100 }
87 return null; 101 return null;
88 } 102 }
89 103
104 /**
105 * Read a document from input stream.
106 *
107 * @param inputStream The input stream.
108 * @return the document read from stream.
109 */
90 public static Document readDocument(InputStream inputStream) { 110 public static Document readDocument(InputStream inputStream) {
91 Document returnValue = null; 111 Document returnValue = null;
92 try { 112 try {
93 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory 113 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
94 .newInstance(); 114 .newInstance();
102 log.error(e, e); 122 log.error(e, e);
103 } 123 }
104 return returnValue; 124 return returnValue;
105 } 125 }
106 126
127
128 @SuppressWarnings("static-access")
107 public Document reInitDocument(Document document) { 129 public Document reInitDocument(Document document) {
108 try { 130 try {
109 byte[] barray = this.writeDocument2String(document).getBytes( 131 @SuppressWarnings("static-access")
132 byte[] barray = ArtifactXMLUtilities.writeDocument2String(document).getBytes(
110 "UTF-8"); 133 "UTF-8");
111 InputStream inputStream = new ByteArrayInputStream(barray); 134 InputStream inputStream = new ByteArrayInputStream(barray);
112 return this.readDocument(inputStream); 135 return ArtifactXMLUtilities.readDocument(inputStream);
113 } catch (UnsupportedEncodingException e) { 136 } catch (UnsupportedEncodingException e) {
114 log.error(e, e); 137 log.error(e, e);
115 } 138 }
116 return document; 139 return document;
117 } 140 }
118 141
142
143 /**
144 * Creates an <code>Element</code> with {@link #XFORM_PREFIX} and <i>name
145 * </i>.
146 *
147 * @param document A document.
148 * @param name The node name.
149 * @return the created element.
150 */
119 public static Element createXFormElement(Document document, String name) { 151 public static Element createXFormElement(Document document, String name) {
120 Element node = document.createElementNS(XFORM_URL, name); 152 Element node = document.createElementNS(XFORM_URL, name);
121 node.setPrefix(XFORM_PREFIX); 153 node.setPrefix(XFORM_PREFIX);
122 return node; 154 return node;
123 } 155 }
124 156
157 /**
158 * Creates an exception node.
159 *
160 * @param message The message in the node.
161 * @param document A document.
162 * @return the document containing the exception node.
163 */
125 public static Document createExceptionReport(String message, Document document) { 164 public static Document createExceptionReport(String message, Document document) {
126 log.debug("ArtifactXMLUtilities.createExceptionReport"); 165 log.debug("ArtifactXMLUtilities.createExceptionReport");
127 Element exceptionReportNode = createArtifactElement(document, 166 Element exceptionReportNode = createArtifactElement(document,
128 "exceptionreport"); 167 "exceptionreport");
129 document.appendChild(exceptionReportNode); 168 document.appendChild(exceptionReportNode);
132 exceptionNode.setTextContent(message); 171 exceptionNode.setTextContent(message);
133 exceptionReportNode.appendChild(exceptionNode); 172 exceptionReportNode.appendChild(exceptionNode);
134 return document; 173 return document;
135 } 174 }
136 175
176 /**
177 * Creates an input exception node.
178 *
179 * @param msg The message in the node.
180 * @param doc A document.
181 * @return the document containing the exception node.
182 */
137 public static Document createInputExceptionReport(String msg, Document doc) { 183 public static Document createInputExceptionReport(String msg, Document doc) {
138 Element exceptionReportNode = createArtifactElement( 184 Element exceptionReportNode = createArtifactElement(
139 doc,"exceptionreport"); 185 doc,"exceptionreport");
140 Element exceptionNode = createArtifactElement( 186 Element exceptionNode = createArtifactElement(
141 doc,"exception"); 187 doc,"exception");
146 exceptionReportNode.appendChild(exceptionNode); 192 exceptionReportNode.appendChild(exceptionNode);
147 doc.appendChild(exceptionReportNode); 193 doc.appendChild(exceptionReportNode);
148 return doc; 194 return doc;
149 } 195 }
150 196
197 /**
198 * Creates a success node.
199 *
200 * @param message The message.
201 * @param document A document.
202 * @return the document containing the success node.
203 */
151 public static Document createSuccessReport(String message, Document document) { 204 public static Document createSuccessReport(String message, Document document) {
152 log.debug("ArtifactXMLUtilities.creatSuccessReport"); 205 log.debug("ArtifactXMLUtilities.creatSuccessReport");
153 Element reportNode = createArtifactElement(document, "result"); 206 Element reportNode = createArtifactElement(document, "result");
154 document.appendChild(reportNode); 207 document.appendChild(reportNode);
155 Element successNode = createArtifactElement(document, "success"); 208 Element successNode = createArtifactElement(document, "success");
156 successNode.setTextContent(message); 209 successNode.setTextContent(message);
157 reportNode.appendChild(successNode); 210 reportNode.appendChild(successNode);
158 return document; 211 return document;
159 } 212 }
160 213
214 /**
215 * Read <i>fileName</i> and return the first child node.
216 *
217 * @param fileName An xml document.
218 * @return the first child node in this document.
219 */
161 public Node readConfiguration(String fileName){ 220 public Node readConfiguration(String fileName){
162 try { 221 try {
163 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 222 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
164 factory.setValidating(false); 223 factory.setValidating(false);
165 return factory.newDocumentBuilder().parse(fileName).getChildNodes().item(0); 224 return factory.newDocumentBuilder().parse(fileName).getChildNodes().item(0);
172 } catch (ParserConfigurationException e) { 231 } catch (ParserConfigurationException e) {
173 log.error(e,e); 232 log.error(e,e);
174 return null; 233 return null;
175 } 234 }
176 } 235 }
177
178 } 236 }
237 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org