Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/repository/utils/XMLUtil.java @ 127:3e772f7f43e0 default tip
ismi-date with long month names in xml dump.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 11 May 2023 18:15:45 +0200 |
parents | 8d79021099a4 |
children |
rev | line source |
---|---|
1 | 1 package org.mpi.openmind.repository.utils; |
2 | |
3 //JAXP | |
4 import javax.xml.parsers.DocumentBuilder; | |
5 import javax.xml.parsers.DocumentBuilderFactory; | |
6 import javax.xml.parsers.ParserConfigurationException; | |
7 import javax.xml.transform.*; | |
8 import javax.xml.transform.dom.*; | |
9 import javax.xml.transform.stream.*; | |
10 import org.w3c.dom.Document; | |
11 import org.xml.sax.SAXException; | |
12 import java.io.*; | |
13 | |
14 import org.w3c.dom.Node; | |
15 import org.w3c.dom.NodeList; | |
16 | |
17 | |
18 | |
19 /** | |
20 * | |
21 * @author Jorge UrzĂșa | |
22 */ | |
23 public class XMLUtil { | |
24 | |
25 private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
26 | |
27 public static String OWN_VALUE_CONFIG = "own-value-configuration"; | |
28 public static String OWN_VALUE_RULES = "own-value-rules"; | |
29 public static String OWN_VALUE_RULE = "own-value-rule"; | |
30 public static String TARGET_RELATION = "target-relation"; | |
31 public static String SOURCE_RELATION = "source-relation"; | |
32 public static String SUBSTRING = "substring"; | |
33 public static String NAME = "name"; | |
34 public static String SOURCE = "source"; | |
35 public static String TARGET = "target"; | |
36 public static String END_NODE = "end-node"; | |
37 public static String XOR = "xor"; | |
38 public static String RULE = "rule"; | |
39 public static String REF = "ref"; | |
40 public static String VALUE = "value"; | |
41 public static String TYPE = "type"; | |
42 public static String CONSTANT = "constant"; | |
43 | |
44 public static String PRINT_RULES = "print-rules"; | |
45 public static String PRINT_RULE = "print-rule"; | |
46 | |
47 public static String OPENMIND_DATA = "openmind-data"; | |
48 public static String ENTITIES = "entities"; | |
49 public static String ENTITY = "entity"; | |
50 public static String ENTITY_ID = "id"; | |
51 public static String ATTRIBUTES = "attributes"; | |
52 public static String ATTRIBUTE = "attribute"; | |
53 public static String ATTRIBUTE_NAME = "name"; | |
54 public static String ATTRIBUTE_VALUE = "value"; | |
55 public static String RELATION_SOURCE_ID = "source-id"; | |
56 public static String RELATION_TARGET_ID = "target-id"; | |
57 public static String RELATION_ID = "id"; | |
58 public static String VIEW = "view"; | |
59 public static String VIEWS = "views"; | |
29 | 60 public static String MODIFICATION_TIME = "mtime"; |
1 | 61 public static String VERSION = "version"; |
62 public static String ID = "id"; | |
29 | 63 public static String ROW_ID = "row-id"; |
64 public static String USER = "user"; | |
65 public static String PUBLIC = "public"; | |
1 | 66 public static String CONCEPTS = "concepts"; |
67 public static String CONCEPT = "concept"; | |
68 public static String ASSERTIONS = "assertions"; | |
69 public static String ASSERTION = "assertion"; | |
77 | 70 public static String NORMALIZED = "norm"; |
120
3b0ce5e3302d
add Node status field to XML export.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
119
diff
changeset
|
71 public static String STATUS = "status"; |
1 | 72 |
79
b0aebac0780a
put statistics about number of entities, relations and attributes in xml dump.
casties
parents:
77
diff
changeset
|
73 public static String ENTITY_STATS = "entity-statistics"; |
b0aebac0780a
put statistics about number of entities, relations and attributes in xml dump.
casties
parents:
77
diff
changeset
|
74 public static String RELATION_STATS = "relation-statistics"; |
b0aebac0780a
put statistics about number of entities, relations and attributes in xml dump.
casties
parents:
77
diff
changeset
|
75 public static String COUNT = "count"; |
b0aebac0780a
put statistics about number of entities, relations and attributes in xml dump.
casties
parents:
77
diff
changeset
|
76 |
1 | 77 public static String META_DATA = "openmind-meta"; |
78 public static String DEFINITIONS = "definitions"; | |
79 public static String DEFINITION = "definition"; | |
80 public static String LABEL = "label"; | |
81 public static String MAIN_LABEL = "main-label"; | |
82 public static String OBJECT_CLASS = "object-class"; | |
83 public static String OWN_VALUE = "own-value"; | |
84 public static String LABEL_NAME = "name"; | |
85 public static String RELATIONS = "relations"; | |
119
4eac7c57e593
add entities' incoming relations to xml export.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
106
diff
changeset
|
86 public static String INVRELATIONS = "invrelations"; |
1 | 87 public static String RELATION = "relation"; |
88 public static String RELATION_LABEL = "label"; | |
89 public static String RELATION_SOURCE = "source-class"; | |
90 public static String RELATION_TARGET = "target-class"; | |
91 public static String CONTENT_TYPE = "content-type"; | |
92 | |
93 public static String INSERTION_MODE = "insertion-mode"; | |
94 public static String MERGE = "merge"; | |
95 public static String REPLACE = "replace"; | |
96 public static String INSERT = "insert"; | |
103
1149eb948036
add isodate tag to XML dump format 4.9.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
86
diff
changeset
|
97 |
1149eb948036
add isodate tag to XML dump format 4.9.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
86
diff
changeset
|
98 public static String ISODATE = "isodate"; |
106
93c7dbfaf062
add bibid tag to xml export of endnote-id attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
103
diff
changeset
|
99 public static String BIBID = "bibid"; |
122
8d79021099a4
XML dump with new ismi-date element.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
120
diff
changeset
|
100 public static String ISMIDATE = "ismi-date"; |
1 | 101 |
102 public static Node getNodeByName(NodeList nodeList, String name){ | |
103 for(int i=0; i < nodeList.getLength(); i++){ | |
104 Node tmp = nodeList.item(i); | |
105 if(tmp.getNodeName().equals(name)) | |
106 return tmp; | |
107 } | |
108 return null; | |
109 } | |
110 | |
111 /** Parses XML file and returns XML document. | |
112 * @param fileName XML file to parse | |
113 * @return XML document or <B>null</B> if error occured | |
114 */ | |
115 public static Document getDocument(String fileName) { | |
116 //System.out.println("Parsing XML file... " + fileName); | |
117 DocumentBuilder docBuilder; | |
118 Document doc = null; | |
119 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); | |
120 docBuilderFactory.setIgnoringElementContentWhitespace(true); | |
121 try { | |
122 docBuilder = docBuilderFactory.newDocumentBuilder(); | |
123 } | |
124 catch (ParserConfigurationException e) { | |
125 System.out.println("Wrong parser configuration: " + e.getMessage()); | |
126 e.printStackTrace(); | |
127 return null; | |
128 } | |
129 File sourceFile = new File(fileName); | |
130 try { | |
131 doc = docBuilder.parse(sourceFile); | |
132 } | |
133 catch (SAXException e) { | |
134 System.out.println("Wrong XML file structure: " + e.getMessage()); | |
135 e.printStackTrace(); | |
136 return null; | |
137 } | |
138 catch (IOException e) { | |
139 System.out.println("Could not read source file: " + e.getMessage()); | |
140 e.printStackTrace(); | |
141 } | |
142 //System.out.println("XML file parsed"); | |
143 return doc; | |
144 } | |
145 | |
146 /** | |
147 * This method looks in the class-path for a file with the name given in input. | |
148 * @param fileName | |
149 * @return | |
150 */ | |
151 public static Document getDocumentFromPathContext(String fileName) { | |
152 //System.out.println("Parsing XML file... " + fileName); | |
153 DocumentBuilder docBuilder; | |
154 Document doc = null; | |
155 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); | |
156 docBuilderFactory.setIgnoringElementContentWhitespace(true); | |
157 try { | |
158 docBuilder = docBuilderFactory.newDocumentBuilder(); | |
159 } | |
160 catch (ParserConfigurationException e) { | |
161 System.out.println("Wrong parser configuration: " + e.getMessage()); | |
162 e.printStackTrace(); | |
163 return null; | |
164 } | |
165 | |
166 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | |
167 InputStream stream = classLoader.getResourceAsStream( fileName ); | |
168 try { | |
169 doc = docBuilder.parse(stream); | |
170 } | |
171 catch (SAXException e) { | |
172 System.out.println("Wrong XML file structure: " + e.getMessage()); | |
173 e.printStackTrace(); | |
174 return null; | |
175 } | |
176 catch (IOException e) { | |
177 System.out.println("Could not read source file: " + e.getMessage()); | |
178 e.printStackTrace(); | |
179 } | |
180 //System.out.println("XML file parsed"); | |
181 return doc; | |
182 } | |
183 | |
184 /** Saves XML Document into XML file. | |
185 * @param fileName XML file name | |
186 * @param doc XML document to save | |
187 * @return <B>true</B> if method success <B>false</B> otherwise | |
188 */ | |
189 static public boolean saveXMLDocument(String fileName, Document doc) { | |
190 | |
191 File xmlOutputFile = new File(fileName); | |
192 FileOutputStream fos; | |
193 Transformer transformer; | |
194 try { | |
195 fos = new FileOutputStream(xmlOutputFile); | |
196 } | |
197 catch (FileNotFoundException e) { | |
198 System.out.println("Error occured: " + e.getMessage()); | |
199 return false; | |
200 } | |
201 // Use a Transformer for output | |
202 TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |
203 try { | |
204 transformer = transformerFactory.newTransformer(); | |
205 } | |
206 catch (TransformerConfigurationException e) { | |
207 System.out.println("Transformer configuration error: " + e.getMessage()); | |
86
d4b456623d43
Updated XML export. Saves relation source-type and target-type. Expanded statistics with per-entity-type relation counts.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
79
diff
changeset
|
208 try { |
d4b456623d43
Updated XML export. Saves relation source-type and target-type. Expanded statistics with per-entity-type relation counts.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
79
diff
changeset
|
209 fos.close(); |
d4b456623d43
Updated XML export. Saves relation source-type and target-type. Expanded statistics with per-entity-type relation counts.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
79
diff
changeset
|
210 } catch (IOException e1) { |
d4b456623d43
Updated XML export. Saves relation source-type and target-type. Expanded statistics with per-entity-type relation counts.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
79
diff
changeset
|
211 e1.printStackTrace(); |
d4b456623d43
Updated XML export. Saves relation source-type and target-type. Expanded statistics with per-entity-type relation counts.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
79
diff
changeset
|
212 } |
1 | 213 return false; |
214 } | |
215 DOMSource source = new DOMSource(doc); | |
216 StreamResult result = new StreamResult(fos); | |
217 // transform source into result will do save | |
218 try { | |
219 transformer.transform(source, result); | |
220 } | |
221 catch (TransformerException e) { | |
222 System.out.println("Error transform: " + e.getMessage()); | |
223 } | |
224 System.out.println("XML file saved."); | |
225 return true; | |
226 } | |
227 | |
228 public static String transformateDocumentToString(Document doc){ | |
229 try{ | |
230 DOMSource domSource = new DOMSource(doc); | |
231 StringWriter writer = new StringWriter(); | |
232 StreamResult result = new StreamResult(writer); | |
233 | |
234 TransformerFactory tf = TransformerFactory.newInstance(); | |
235 Transformer transformer = tf.newTransformer(); | |
236 transformer.transform(domSource, result); | |
237 return writer.toString(); | |
238 }catch(Exception e){ | |
239 e.printStackTrace(); | |
240 } | |
241 return null; | |
242 } | |
243 | |
244 public String ptransformateDocumentToString(Document doc){ | |
245 try{ | |
246 DOMSource domSource = new DOMSource(doc); | |
247 StringWriter writer = new StringWriter(); | |
248 StreamResult result = new StreamResult(writer); | |
249 | |
250 TransformerFactory tf = TransformerFactory.newInstance(); | |
251 Transformer transformer = tf.newTransformer(); | |
252 transformer.transform(domSource, result); | |
253 return writer.toString(); | |
254 }catch(Exception e){ | |
255 e.printStackTrace(); | |
256 } | |
257 return null; | |
258 } | |
259 | |
260 public static Document transformateStringToDocument(String text){ | |
261 try{ | |
262 DocumentBuilder db = dbf.newDocumentBuilder(); | |
263 InputStream stream = new ByteArrayInputStream(text.getBytes("UTF-8")); | |
264 return db.parse(stream); | |
265 }catch(Exception e){ | |
266 e.printStackTrace(); | |
267 } | |
268 return null; | |
269 } | |
270 | |
271 /** | |
272 * <p>This method transforms the file | |
273 * input using the a stylesheet giving as input</p> | |
274 * | |
275 * @param xsl is the stylesheet file used to transformate the input document. | |
276 * @param input is the document, which will be transformated. | |
277 * @return the document transformed by the xsl stylesheet. | |
278 */ | |
279 public static Document xmlToXmlTransformation(Document xsl, Document input){ | |
280 try{ | |
281 TransformerFactory factory = TransformerFactory.newInstance(); | |
282 | |
283 InputStream stream = new ByteArrayInputStream(XMLUtil.transformateDocumentToString(xsl).getBytes("UTF-8")); | |
284 //Transformer transformer = factory.newTransformer(new DOMSource(xsl)); | |
285 //Transformer transformer = factory.newTransformer(new StreamSource("transformatorAxel1.xsl")); | |
286 Transformer transformer = factory.newTransformer(new StreamSource(stream)); | |
287 DOMResult result = new DOMResult(); | |
288 //transformer.transform(new DOMSource(input), new StreamResult(new FileOutputStream("mi.xml"))); | |
289 transformer.transform(new DOMSource(input), result); | |
290 | |
291 return (Document)result.getNode(); | |
292 }catch(Exception e){ | |
293 e.printStackTrace(); | |
294 } | |
295 return null; | |
296 } | |
297 } |