Révision 3074
tmp/org.txm.core/src/java/org/txm/scripts/importer/XPathResult.groovy (revision 3074) | ||
---|---|---|
35 | 35 |
import org.w3c.dom.*; |
36 | 36 |
import org.xml.sax.SAXException; |
37 | 37 |
|
38 |
import javax.xml.bind.helpers.DefaultValidationEventHandler; |
|
39 | 38 |
import javax.xml.parsers.*; |
40 | 39 |
import javax.xml.xpath.*; |
41 | 40 |
import javax.xml.stream.*; |
... | ... | |
51 | 50 |
* return the id of a bfm tag <milestone/> |
52 | 51 |
*/ |
53 | 52 |
public class XPathResult { |
54 |
|
|
53 |
|
|
55 | 54 |
/** The doc. */ |
56 | 55 |
Document doc; |
57 | 56 |
XPath xpath; |
58 |
|
|
57 |
|
|
59 | 58 |
/** |
60 | 59 |
* Instantiates a new x path result. |
61 | 60 |
* |
... | ... | |
64 | 63 |
public XPathResult(File xmlfile) { |
65 | 64 |
this(xmlfile, true) |
66 | 65 |
} |
67 |
|
|
66 |
|
|
68 | 67 |
/** |
69 | 68 |
* Instantiates a new x path result. |
70 | 69 |
* |
... | ... | |
74 | 73 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
75 | 74 |
factory.setXIncludeAware(true); |
76 | 75 |
factory.setNamespaceAware(namespaceAware); // never forget this! |
77 |
|
|
76 |
|
|
78 | 77 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
79 | 78 |
doc = builder.parse(xmlfile); |
80 |
|
|
79 |
|
|
81 | 80 |
XPathFactory xfactory = XPathFactory.newInstance(); |
82 | 81 |
xpath = xfactory.newXPath(); |
83 | 82 |
xpath.setNamespaceContext(new PersonalNamespaceContext()); |
84 | 83 |
} |
85 |
|
|
84 |
|
|
86 | 85 |
public Document getDocument() { |
87 | 86 |
return doc; |
88 | 87 |
} |
89 |
|
|
88 |
|
|
90 | 89 |
public def getNodes(String query) { |
91 | 90 |
def rnodes = []; |
92 |
|
|
91 |
|
|
93 | 92 |
XPathExpression expr = xpath.compile(query); |
94 | 93 |
Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
95 | 94 |
if (result instanceof NodeList) { |
... | ... | |
103 | 102 |
} |
104 | 103 |
return rnodes; |
105 | 104 |
} |
106 |
|
|
105 |
|
|
107 | 106 |
public String getXpathResponse(String query) { |
108 | 107 |
XPathExpression expr = xpath.compile(query); |
109 | 108 |
Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
110 |
|
|
109 |
|
|
111 | 110 |
NodeList nodes = (NodeList) result; |
112 | 111 |
for (int i = 0; i < nodes.getLength(); i++) { |
113 | 112 |
//println nodes.item(i) |
114 | 113 |
return (nodes.item(i).getNodeValue()); |
115 | 114 |
} |
116 | 115 |
} |
117 |
|
|
116 |
|
|
118 | 117 |
public ArrayList<String> getXpathResponses(String query) { |
119 | 118 |
ArrayList<String> xresult = new ArrayList<String>(); |
120 | 119 |
XPathExpression expr = xpath.compile(query); |
121 | 120 |
Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
122 |
|
|
121 |
|
|
123 | 122 |
NodeList nodes = (NodeList) result; |
124 | 123 |
for (int i = 0; i < nodes.getLength(); i++) { |
125 | 124 |
//println nodes.item(i) |
... | ... | |
127 | 126 |
} |
128 | 127 |
return xresult |
129 | 128 |
} |
130 |
|
|
129 |
|
|
131 | 130 |
public String getXpathResponse(String query, String devaultValue) { |
132 | 131 |
String rez = getXpathResponse(query); |
133 | 132 |
if (rez == null) |
134 | 133 |
return devaultValue; |
135 | 134 |
return rez; |
136 | 135 |
} |
137 |
|
|
136 |
|
|
138 | 137 |
public void close() { |
139 | 138 |
xpath = null; |
140 | 139 |
doc = null; |
141 | 140 |
} |
142 |
|
|
141 |
|
|
143 | 142 |
/** |
144 | 143 |
* OBSOLETE VERSION FOR TXM return the node text content given a XPath |
145 | 144 |
* "//path.../.../@attr" |
... | ... | |
170 | 169 |
* |
171 | 170 |
* } return ""; } |
172 | 171 |
*/ |
173 |
|
|
172 |
|
|
174 | 173 |
static public String getXpathResponse(File xmlfile, String query, String devaultValue) { |
175 | 174 |
String rez = getXpathResponse(xmlfile, query); |
176 | 175 |
if (rez == null) |
177 | 176 |
return devaultValue; |
178 | 177 |
return rez; |
179 | 178 |
} |
180 |
|
|
179 |
|
|
181 | 180 |
static public String getXpathResponse(File xmlfile, String query, String devaultValue, boolean namespaceAware) { |
182 | 181 |
String rez = getXpathResponse(xmlfile, query, namespaceAware); |
183 | 182 |
if (rez == null) |
184 | 183 |
return devaultValue; |
185 | 184 |
return rez; |
186 | 185 |
} |
187 |
|
|
186 |
|
|
188 | 187 |
/** |
189 | 188 |
* Gets the xpath response. |
190 | 189 |
* |
... | ... | |
195 | 194 |
static public String getXpathResponse(File xmlfile, String query) { |
196 | 195 |
return getXpathResponse(xmlfile, query, true); |
197 | 196 |
} |
198 |
|
|
197 |
|
|
199 | 198 |
/** |
200 | 199 |
* Gets the xpath response. |
201 | 200 |
* |
... | ... | |
225 | 224 |
// return (nodes.item(i).getNodeValue()); |
226 | 225 |
// } |
227 | 226 |
} |
228 |
|
|
227 |
|
|
229 | 228 |
/** |
230 | 229 |
* The main method. |
231 | 230 |
* |
Formats disponibles : Unified diff