0
|
1 package de.mpiwg.anteater.xml.impl;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.FileNotFoundException;
|
|
5 import java.io.FileOutputStream;
|
|
6 import java.io.IOException;
|
|
7 import java.io.InputStream;
|
|
8 import java.io.OutputStreamWriter;
|
|
9 import java.io.StringReader;
|
|
10 import java.nio.charset.Charset;
|
|
11 import java.util.ArrayList;
|
|
12 import java.util.List;
|
|
13
|
|
14 import org.jdom2.Content;
|
|
15 import org.jdom2.Document;
|
|
16 import org.jdom2.Element;
|
|
17 import org.jdom2.JDOMException;
|
|
18 import org.jdom2.Namespace;
|
|
19 import org.jdom2.Text;
|
|
20 import org.jdom2.filter.ElementFilter;
|
|
21 import org.jdom2.input.SAXBuilder;
|
|
22 import org.jdom2.output.Format;
|
|
23 import org.jdom2.output.XMLOutputter;
|
|
24 import org.jdom2.xpath.XPathExpression;
|
|
25 import org.jdom2.xpath.XPathFactory;
|
|
26
|
|
27 import de.mpiwg.anteater.xml.IXMLParser;
|
|
28
|
|
29 public class JDOMParser implements IXMLParser {
|
|
30
|
|
31 private Document doc;
|
|
32
|
|
33 public JDOMParser(String pathOrContent, boolean loadFromFile) {
|
|
34 if (loadFromFile) {
|
|
35 init(pathOrContent);
|
|
36 } else {
|
|
37 try {
|
|
38 SAXBuilder builder = new SAXBuilder();
|
|
39 doc = builder.build(new StringReader(pathOrContent));
|
|
40 } catch (JDOMException e) {
|
|
41 // TODO Auto-generated catch block
|
|
42 e.printStackTrace();
|
|
43 } catch (IOException e) {
|
|
44 // TODO Auto-generated catch block
|
|
45 e.printStackTrace();
|
|
46 }
|
|
47 }
|
|
48
|
|
49 }
|
|
50
|
|
51 public JDOMParser(InputStream stream) {
|
|
52 init(stream);
|
|
53 }
|
|
54
|
|
55 public JDOMParser() {
|
|
56 doc = new Document();
|
|
57 }
|
|
58
|
|
59 protected void init(String filepath) {
|
|
60 SAXBuilder builder = new SAXBuilder();
|
|
61 File file = new File(filepath);
|
|
62 if (file.exists()) {
|
|
63 try {
|
|
64 doc = builder.build(file);
|
|
65 } catch (JDOMException e) {
|
|
66 // TODO Auto-generated catch block
|
|
67 e.printStackTrace();
|
|
68 } catch (IOException e) {
|
|
69 // TODO Auto-generated catch block
|
|
70 e.printStackTrace();
|
|
71 }
|
|
72 }
|
|
73 }
|
|
74
|
|
75 protected void init(InputStream stream) {
|
|
76 SAXBuilder builder = new SAXBuilder();
|
|
77 try {
|
|
78 doc = builder.build(stream);
|
|
79 } catch (JDOMException e) {
|
|
80 // TODO Auto-generated catch block
|
|
81 e.printStackTrace();
|
|
82 } catch (IOException e) {
|
|
83 // TODO Auto-generated catch block
|
|
84 e.printStackTrace();
|
|
85 }
|
|
86 }
|
|
87
|
|
88 @Override
|
|
89 public Element getRoot() {
|
|
90 return doc.getRootElement();
|
|
91 }
|
|
92
|
|
93 @Override
|
|
94 public List<Element> executeXPath(String xpath, String defaultNSPrefix) {
|
|
95 if (doc == null)
|
|
96 return null;
|
|
97
|
|
98 XPathFactory factory = XPathFactory.instance();
|
|
99 // get namespaces but remove empty prefix to prevent xpath exception
|
|
100 List<Namespace> namespaces = doc.getRootElement()
|
|
101 .getNamespacesInScope();
|
|
102 List<Namespace> finalNamespaces = new ArrayList<Namespace>();
|
|
103 for (Namespace ns : namespaces) {
|
|
104 if (!ns.getPrefix().isEmpty())
|
|
105 finalNamespaces.add(ns);
|
|
106 else if (!ns.getURI().trim().isEmpty() && defaultNSPrefix != null && !defaultNSPrefix.trim().isEmpty()) {
|
|
107 finalNamespaces.add(Namespace.getNamespace(defaultNSPrefix, ns.getURI()));
|
|
108 }
|
|
109 }
|
|
110 List<Element> results;
|
|
111 // evaluate xpath
|
|
112 XPathExpression<Element> expression = factory.compile(xpath,
|
|
113 new ElementFilter(), null, finalNamespaces);
|
|
114 results = expression.evaluate(doc.getRootElement());
|
|
115
|
|
116 return results;
|
|
117 }
|
|
118
|
|
119 protected String stripText(Element node) {
|
|
120 StringBuffer text = new StringBuffer();
|
|
121 stripTextRecursivie(node, text);
|
|
122 return text.toString().trim();
|
|
123 }
|
|
124
|
|
125 protected void stripTextRecursivie(Element node, StringBuffer text) {
|
|
126 for (Content child : node.getContent()) {
|
|
127 if (child instanceof Text) {
|
|
128 text.append(((Text) child).getText() != null ? ((Text) child)
|
|
129 .getTextNormalize() + " " : "");
|
|
130 } else if (child instanceof Element) {
|
|
131 stripTextRecursivie((Element) child, text);
|
|
132 }
|
|
133 }
|
|
134 }
|
|
135
|
|
136 public boolean save(String filepath, boolean prettyprint) {
|
|
137 XMLOutputter out = null;
|
|
138 if (prettyprint)
|
|
139 out = new XMLOutputter(Format.getPrettyFormat());
|
|
140 else
|
|
141 out = new XMLOutputter();
|
|
142 //FileWriter writer;
|
|
143 FileOutputStream stream;
|
|
144 try {
|
|
145 stream = new FileOutputStream(new File(filepath));
|
|
146 } catch (FileNotFoundException e1) {
|
|
147 // TODO Auto-generated catch block
|
|
148 e1.printStackTrace();
|
|
149 return false;
|
|
150 }
|
|
151 OutputStreamWriter osWriter = new OutputStreamWriter(stream, Charset.forName("UTF-8"));
|
|
152 try {
|
|
153 //writer = new FileWriter(filepath);
|
|
154 out.output(doc, osWriter);
|
|
155 osWriter.flush();
|
|
156 osWriter.close();
|
|
157 } catch (IOException e) {
|
|
158 e.printStackTrace();
|
|
159 return false;
|
|
160 }
|
|
161 return true;
|
|
162 }
|
|
163 }
|