comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java @ 171:7fb9441dd8af

Format Code to max 80 Chars per Row and Cleanup gnv-artifacts/trunk@208 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 09 Oct 2009 07:54:48 +0000
parents cde042a0a395
children 3dcd2b0b456e
comparison
equal deleted inserted replaced
170:7be22e76c270 171:7fb9441dd8af
29 import de.intevation.artifacts.ArtifactNamespaceContext; 29 import de.intevation.artifacts.ArtifactNamespaceContext;
30 import de.intevation.gnv.transition.timeseries.TimeSeriesOutputTransition; 30 import de.intevation.gnv.transition.timeseries.TimeSeriesOutputTransition;
31 31
32 /** 32 /**
33 * @author Tim Englich <tim.englich@intevation.de> 33 * @author Tim Englich <tim.englich@intevation.de>
34 * 34 *
35 */ 35 */
36 public class ArtifactXMLUtilities implements Serializable{ 36 public class ArtifactXMLUtilities implements Serializable {
37 /** 37 /**
38 * 38 *
39 */ 39 */
40 private static final long serialVersionUID = -6236340358303411758L; 40 private static final long serialVersionUID = -6236340358303411758L;
41 41
42 /** 42 /**
43 * the logger, used to log exceptions and additonaly information 43 * the logger, used to log exceptions and additonaly information
44 */ 44 */
45 private static Logger log = Logger.getLogger(TimeSeriesOutputTransition.class); 45 private static Logger log = Logger
46 46 .getLogger(TimeSeriesOutputTransition.class);
47
47 public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; 48 public static final String XFORM_URL = "http://www.w3.org/2002/xforms";
48 public static final String XFORM_PREFIX = "xform"; 49 public static final String XFORM_PREFIX = "xform";
49 50
50 /** 51 /**
51 * Constructor 52 * Constructor
52 */ 53 */
53 public ArtifactXMLUtilities() { 54 public ArtifactXMLUtilities() {
54 } 55 }
55 56
56 /** 57 /**
57 * @param document 58 * @param document
58 * @return 59 * @return
59 */ 60 */
60 public Element createArtifactElement(Document document, String name) { 61 public Element createArtifactElement(Document document, String name) {
61 Element node = document.createElementNS(ArtifactNamespaceContext.NAMESPACE_URI, name); 62 Element node = document.createElementNS(
63 ArtifactNamespaceContext.NAMESPACE_URI, name);
62 node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX); 64 node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX);
63 return node; 65 return node;
64 } 66 }
65 67
66 public String writeDocument2String(Document document){ 68 public String writeDocument2String(Document document) {
67 try { 69 try {
68 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 70 TransformerFactory transformerFactory = TransformerFactory
71 .newInstance();
69 Transformer transformer = transformerFactory.newTransformer(); 72 Transformer transformer = transformerFactory.newTransformer();
70 DOMSource source = new DOMSource(document); 73 DOMSource source = new DOMSource(document);
71 StringWriter sw = new StringWriter(); 74 StringWriter sw = new StringWriter();
72 StreamResult result = new StreamResult(sw); 75 StreamResult result = new StreamResult(sw);
73 transformer.transform(source, result); 76 transformer.transform(source, result);
74 return sw.getBuffer().toString(); 77 return sw.getBuffer().toString();
75 } catch (TransformerConfigurationException e) { 78 } catch (TransformerConfigurationException e) {
76 log.error(e,e); 79 log.error(e, e);
77 } catch (TransformerFactoryConfigurationError e) { 80 } catch (TransformerFactoryConfigurationError e) {
78 log.error(e,e); 81 log.error(e, e);
79 } catch (TransformerException e) { 82 } catch (TransformerException e) {
80 log.error(e,e); 83 log.error(e, e);
81 } 84 }
82 return null; 85 return null;
83 } 86 }
84
85 87
86 88 public Document readDocument(InputStream inputStream) {
87
88 public Document readDocument(InputStream inputStream){
89 Document returnValue = null; 89 Document returnValue = null;
90 try { 90 try {
91 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 91 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
92 .newInstance();
92 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 93 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
93 returnValue = docBuilder.parse (inputStream); 94 returnValue = docBuilder.parse(inputStream);
94 } catch (ParserConfigurationException e) { 95 } catch (ParserConfigurationException e) {
95 log.error(e,e); 96 log.error(e, e);
96 } catch (SAXException e) { 97 } catch (SAXException e) {
97 log.error(e,e); 98 log.error(e, e);
98 } catch (IOException e) { 99 } catch (IOException e) {
99 log.error(e,e); 100 log.error(e, e);
100 } 101 }
101 return returnValue; 102 return returnValue;
102 } 103 }
103 104
104 public Document reInitDocument(Document document){ 105 public Document reInitDocument(Document document) {
105 try { 106 try {
106 byte[] barray = this.writeDocument2String(document).getBytes("UTF-8"); 107 byte[] barray = this.writeDocument2String(document).getBytes(
107 InputStream inputStream = new ByteArrayInputStream(barray); 108 "UTF-8");
109 InputStream inputStream = new ByteArrayInputStream(barray);
108 return this.readDocument(inputStream); 110 return this.readDocument(inputStream);
109 } catch (UnsupportedEncodingException e) { 111 } catch (UnsupportedEncodingException e) {
110 log.error(e,e); 112 log.error(e, e);
111 } 113 }
112 return document; 114 return document;
113 } 115 }
114 116
115 public Element createXFormElement(Document document, String name) { 117 public Element createXFormElement(Document document, String name) {
116 Element node = document.createElementNS(XFORM_URL, name); 118 Element node = document.createElementNS(XFORM_URL, name);
117 node.setPrefix(XFORM_PREFIX); 119 node.setPrefix(XFORM_PREFIX);
118 return node; 120 return node;
119 } 121 }
120 122
121 public Document createExceptionReport(String message, Document document){ 123 public Document createExceptionReport(String message, Document document) {
122 log.debug("ArtifactXMLUtilities.createExceptionReport"); 124 log.debug("ArtifactXMLUtilities.createExceptionReport");
123 Element exceptionReportNode = this.createArtifactElement(document, "exceptionreport"); 125 Element exceptionReportNode = this.createArtifactElement(document,
126 "exceptionreport");
124 document.appendChild(exceptionReportNode); 127 document.appendChild(exceptionReportNode);
125 Element exceptionNode = this.createArtifactElement(document, "exception"); 128 Element exceptionNode = this.createArtifactElement(document,
129 "exception");
126 exceptionNode.setTextContent(message); 130 exceptionNode.setTextContent(message);
127 exceptionReportNode.appendChild(exceptionNode); 131 exceptionReportNode.appendChild(exceptionNode);
128 return document; 132 return document;
129 } 133 }
130 134
131 public Document createSuccessReport(String message, Document document){ 135 public Document createSuccessReport(String message, Document document) {
132 log.debug("ArtifactXMLUtilities.creatSuccessReport"); 136 log.debug("ArtifactXMLUtilities.creatSuccessReport");
133 Element reportNode = this.createArtifactElement(document, "result"); 137 Element reportNode = this.createArtifactElement(document, "result");
134 document.appendChild(reportNode); 138 document.appendChild(reportNode);
135 Element successNode = this.createArtifactElement(document, "success"); 139 Element successNode = this.createArtifactElement(document, "success");
136 successNode.setTextContent(message); 140 successNode.setTextContent(message);
137 reportNode.appendChild(successNode); 141 reportNode.appendChild(successNode);
138 return document; 142 return document;
139 } 143 }
140
141 144
142 } 145 }

http://dive4elements.wald.intevation.org