Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/repository/utils/XMLUtil.java @ 79:b0aebac0780a
put statistics about number of entities, relations and attributes in xml dump.
tags: entity-statistics, relation-statistics.
author | casties |
---|---|
date | Fri, 03 Mar 2017 18:59:20 +0100 |
parents | a59984fd3c3f |
children | d4b456623d43 |
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"; |
1 | 71 |
79
b0aebac0780a
put statistics about number of entities, relations and attributes in xml dump.
casties
parents:
77
diff
changeset
|
72 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
|
73 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
|
74 public static String COUNT = "count"; |
b0aebac0780a
put statistics about number of entities, relations and attributes in xml dump.
casties
parents:
77
diff
changeset
|
75 |
1 | 76 public static String META_DATA = "openmind-meta"; |
77 public static String DEFINITIONS = "definitions"; | |
78 public static String DEFINITION = "definition"; | |
79 public static String LABEL = "label"; | |
80 public static String MAIN_LABEL = "main-label"; | |
81 public static String OBJECT_CLASS = "object-class"; | |
82 public static String OWN_VALUE = "own-value"; | |
83 public static String LABEL_NAME = "name"; | |
84 public static String RELATIONS = "relations"; | |
85 public static String RELATION = "relation"; | |
86 public static String RELATION_LABEL = "label"; | |
87 public static String RELATION_SOURCE = "source-class"; | |
88 public static String RELATION_TARGET = "target-class"; | |
89 public static String CONTENT_TYPE = "content-type"; | |
90 | |
91 public static String INSERTION_MODE = "insertion-mode"; | |
92 public static String MERGE = "merge"; | |
93 public static String REPLACE = "replace"; | |
94 public static String INSERT = "insert"; | |
95 | |
96 public static Node getNodeByName(NodeList nodeList, String name){ | |
97 for(int i=0; i < nodeList.getLength(); i++){ | |
98 Node tmp = nodeList.item(i); | |
99 if(tmp.getNodeName().equals(name)) | |
100 return tmp; | |
101 } | |
102 return null; | |
103 } | |
104 | |
105 /** Parses XML file and returns XML document. | |
106 * @param fileName XML file to parse | |
107 * @return XML document or <B>null</B> if error occured | |
108 */ | |
109 public static Document getDocument(String fileName) { | |
110 //System.out.println("Parsing XML file... " + fileName); | |
111 DocumentBuilder docBuilder; | |
112 Document doc = null; | |
113 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); | |
114 docBuilderFactory.setIgnoringElementContentWhitespace(true); | |
115 try { | |
116 docBuilder = docBuilderFactory.newDocumentBuilder(); | |
117 } | |
118 catch (ParserConfigurationException e) { | |
119 System.out.println("Wrong parser configuration: " + e.getMessage()); | |
120 e.printStackTrace(); | |
121 return null; | |
122 } | |
123 File sourceFile = new File(fileName); | |
124 try { | |
125 doc = docBuilder.parse(sourceFile); | |
126 } | |
127 catch (SAXException e) { | |
128 System.out.println("Wrong XML file structure: " + e.getMessage()); | |
129 e.printStackTrace(); | |
130 return null; | |
131 } | |
132 catch (IOException e) { | |
133 System.out.println("Could not read source file: " + e.getMessage()); | |
134 e.printStackTrace(); | |
135 } | |
136 //System.out.println("XML file parsed"); | |
137 return doc; | |
138 } | |
139 | |
140 /** | |
141 * This method looks in the class-path for a file with the name given in input. | |
142 * @param fileName | |
143 * @return | |
144 */ | |
145 public static Document getDocumentFromPathContext(String fileName) { | |
146 //System.out.println("Parsing XML file... " + fileName); | |
147 DocumentBuilder docBuilder; | |
148 Document doc = null; | |
149 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); | |
150 docBuilderFactory.setIgnoringElementContentWhitespace(true); | |
151 try { | |
152 docBuilder = docBuilderFactory.newDocumentBuilder(); | |
153 } | |
154 catch (ParserConfigurationException e) { | |
155 System.out.println("Wrong parser configuration: " + e.getMessage()); | |
156 e.printStackTrace(); | |
157 return null; | |
158 } | |
159 | |
160 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | |
161 InputStream stream = classLoader.getResourceAsStream( fileName ); | |
162 try { | |
163 doc = docBuilder.parse(stream); | |
164 } | |
165 catch (SAXException e) { | |
166 System.out.println("Wrong XML file structure: " + e.getMessage()); | |
167 e.printStackTrace(); | |
168 return null; | |
169 } | |
170 catch (IOException e) { | |
171 System.out.println("Could not read source file: " + e.getMessage()); | |
172 e.printStackTrace(); | |
173 } | |
174 //System.out.println("XML file parsed"); | |
175 return doc; | |
176 } | |
177 | |
178 /** Saves XML Document into XML file. | |
179 * @param fileName XML file name | |
180 * @param doc XML document to save | |
181 * @return <B>true</B> if method success <B>false</B> otherwise | |
182 */ | |
183 static public boolean saveXMLDocument(String fileName, Document doc) { | |
184 | |
185 File xmlOutputFile = new File(fileName); | |
186 FileOutputStream fos; | |
187 Transformer transformer; | |
188 try { | |
189 fos = new FileOutputStream(xmlOutputFile); | |
190 } | |
191 catch (FileNotFoundException e) { | |
192 System.out.println("Error occured: " + e.getMessage()); | |
193 return false; | |
194 } | |
195 // Use a Transformer for output | |
196 TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |
197 try { | |
198 transformer = transformerFactory.newTransformer(); | |
199 } | |
200 catch (TransformerConfigurationException e) { | |
201 System.out.println("Transformer configuration error: " + e.getMessage()); | |
202 return false; | |
203 } | |
204 DOMSource source = new DOMSource(doc); | |
205 StreamResult result = new StreamResult(fos); | |
206 // transform source into result will do save | |
207 try { | |
208 transformer.transform(source, result); | |
209 } | |
210 catch (TransformerException e) { | |
211 System.out.println("Error transform: " + e.getMessage()); | |
212 } | |
213 System.out.println("XML file saved."); | |
214 return true; | |
215 } | |
216 | |
217 public static String transformateDocumentToString(Document doc){ | |
218 try{ | |
219 DOMSource domSource = new DOMSource(doc); | |
220 StringWriter writer = new StringWriter(); | |
221 StreamResult result = new StreamResult(writer); | |
222 | |
223 TransformerFactory tf = TransformerFactory.newInstance(); | |
224 Transformer transformer = tf.newTransformer(); | |
225 transformer.transform(domSource, result); | |
226 return writer.toString(); | |
227 }catch(Exception e){ | |
228 e.printStackTrace(); | |
229 } | |
230 return null; | |
231 } | |
232 | |
233 public String ptransformateDocumentToString(Document doc){ | |
234 try{ | |
235 DOMSource domSource = new DOMSource(doc); | |
236 StringWriter writer = new StringWriter(); | |
237 StreamResult result = new StreamResult(writer); | |
238 | |
239 TransformerFactory tf = TransformerFactory.newInstance(); | |
240 Transformer transformer = tf.newTransformer(); | |
241 transformer.transform(domSource, result); | |
242 return writer.toString(); | |
243 }catch(Exception e){ | |
244 e.printStackTrace(); | |
245 } | |
246 return null; | |
247 } | |
248 | |
249 public static Document transformateStringToDocument(String text){ | |
250 try{ | |
251 DocumentBuilder db = dbf.newDocumentBuilder(); | |
252 InputStream stream = new ByteArrayInputStream(text.getBytes("UTF-8")); | |
253 return db.parse(stream); | |
254 }catch(Exception e){ | |
255 e.printStackTrace(); | |
256 } | |
257 return null; | |
258 } | |
259 | |
260 /** | |
261 * <p>This method transforms the file | |
262 * input using the a stylesheet giving as input</p> | |
263 * | |
264 * @param xsl is the stylesheet file used to transformate the input document. | |
265 * @param input is the document, which will be transformated. | |
266 * @return the document transformed by the xsl stylesheet. | |
267 */ | |
268 public static Document xmlToXmlTransformation(Document xsl, Document input){ | |
269 try{ | |
270 TransformerFactory factory = TransformerFactory.newInstance(); | |
271 | |
272 InputStream stream = new ByteArrayInputStream(XMLUtil.transformateDocumentToString(xsl).getBytes("UTF-8")); | |
273 //Transformer transformer = factory.newTransformer(new DOMSource(xsl)); | |
274 //Transformer transformer = factory.newTransformer(new StreamSource("transformatorAxel1.xsl")); | |
275 Transformer transformer = factory.newTransformer(new StreamSource(stream)); | |
276 DOMResult result = new DOMResult(); | |
277 //transformer.transform(new DOMSource(input), new StreamResult(new FileOutputStream("mi.xml"))); | |
278 transformer.transform(new DOMSource(input), result); | |
279 | |
280 return (Document)result.getNode(); | |
281 }catch(Exception e){ | |
282 e.printStackTrace(); | |
283 } | |
284 return null; | |
285 } | |
286 } |