use of org.jdom.input.SAXBuilder in project gate-core by GateNLP.
the class AnnotationSchema method fromXSchema.
/**
* Creates an AnnotationSchema object from an XSchema file
* @param anXSchemaURL the URL where to find the XSchema file
*/
public void fromXSchema(URL anXSchemaURL) throws ResourceInstantiationException {
org.jdom.Document jDom = null;
SAXBuilder saxBuilder = new SAXBuilder(false);
try {
try {
jDom = saxBuilder.build(anXSchemaURL);
} catch (JDOMException je) {
throw new ResourceInstantiationException(je);
}
} catch (java.io.IOException ex) {
throw new ResourceInstantiationException(ex);
}
workWithJDom(jDom);
}
use of org.jdom.input.SAXBuilder in project gate-core by GateNLP.
the class Parser method fromXML.
/**
* Given xml representation of HIT converts them into an array of hits
* @throws IOException
*/
public static Hit[] fromXML(String xml) throws IOException, JDOMException {
SAXBuilder saxBuilder = new SAXBuilder(false);
org.jdom.Document jdomDoc = saxBuilder.build(new StringReader(xml));
Element rootElement = jdomDoc.getRootElement();
if (!rootElement.getName().equalsIgnoreCase(HITS)) {
throw new IOException("Root element must be " + HITS);
}
// rootElement is HITS
// this will internally contains instances of HIT
List<?> hitsChildren = rootElement.getChildren(HIT);
Hit[] hits = new Hit[hitsChildren.size()];
for (int i = 0; i < hitsChildren.size(); i++) {
Element hitElem = (Element) hitsChildren.get(i);
int startOffset = Integer.parseInt(hitElem.getChildText(START_OFFSET));
int endOffset = Integer.parseInt(hitElem.getChildText(END_OFFSET));
String docID = hitElem.getChildText(DOC_ID);
String annotationSetName = hitElem.getChildText(ANNOTATION_SET_NAME);
String queryString = hitElem.getChildText(QUERY);
Element patternAnnotations = hitElem.getChild(PATTERN_ANNOTATIONS);
if (patternAnnotations == null) {
hits[i] = new Hit(docID, annotationSetName, startOffset, endOffset, queryString);
continue;
}
List<?> patAnnots = patternAnnotations.getChildren(PATTERN_ANNOTATION);
List<PatternAnnotation> patAnnotsList = new ArrayList<PatternAnnotation>();
for (int j = 0; j < patAnnots.size(); j++) {
Element patAnnot = (Element) patAnnots.get(j);
PatternAnnotation pa = new PatternAnnotation();
pa.setStOffset(Integer.parseInt(patAnnot.getChildText(START)));
pa.setEnOffset(Integer.parseInt(patAnnot.getChildText(END)));
pa.setPosition(Integer.parseInt(patAnnot.getChildText(POSITION)));
pa.setText(patAnnot.getChildText(TEXT));
pa.setType(patAnnot.getChildText(TYPE));
// we need to find out its features
Element featuresElem = patAnnot.getChild(FEATURES);
// more than one features possible
List<?> featuresElemsList = featuresElem.getChildren(FEATURE);
for (int k = 0; k < featuresElemsList.size(); k++) {
Element featureElem = (Element) featuresElemsList.get(k);
String key = featureElem.getChildText(KEY);
String value = featureElem.getChildText(VALUE);
pa.addFeature(key, value);
}
patAnnotsList.add(pa);
}
String patternText = hitElem.getChildText(PATTERN_TEXT);
int leftCSO = Integer.parseInt(hitElem.getChildText(LEFT_CONTEXT_START_OFFSET));
int rightCEO = Integer.parseInt(hitElem.getChildText(RIGHT_CONTEXT_END_OFFSET));
hits[i] = new Pattern(docID, annotationSetName, patternText, startOffset, endOffset, leftCSO, rightCEO, patAnnotsList, queryString);
}
return hits;
}
use of org.jdom.input.SAXBuilder in project gate-core by GateNLP.
the class PackageGappTask method getJars.
/**
* Extract the text values from any <JAR> elements contained in the
* referenced creole.xml file.
*
* @return a set with one element for each unique <JAR> entry in the
* given creole.xml.
*/
private Set<String> getJars(URL creoleXml) {
try {
Set<String> jars = new HashSet<String>();
// the XPath is a bit ugly, but needed to match the element name
// case-insensitively.
XPath jarXPath = XPath.newInstance("//*[translate(local-name(), 'jar', 'JAR') = 'JAR']");
SAXBuilder builder = new SAXBuilder();
Document creoleDoc = builder.build(creoleXml);
// technically unsafe, but we know that the above XPath expression
// can only match elements.
@SuppressWarnings("unchecked") List<Element> jarElts = jarXPath.selectNodes(creoleDoc);
for (Element e : jarElts) {
jars.add(e.getTextTrim());
}
return jars;
} catch (JDOMException e) {
throw new BuildException("Error extracting JAR elements from " + creoleXml, e, getLocation());
} catch (IOException e) {
throw new BuildException("Error loading " + creoleXml + " to extract JARs", e, getLocation());
}
}
use of org.jdom.input.SAXBuilder in project teiid by teiid.
the class XMLQueryVisitationStrategy method parseXMLQueryFile.
/**
* Consume an XML Query File and produce a Map containing queries, with
* queryNames/IDs as Keys.
* <br>
* @param queryFile the XML file object that is to be parsed
* @return the List containing quers.
* @exception JDOMException if there is an error consuming the message.
*/
public List parseXMLQueryFile(String queryScenarioID, File queryFile, String querySetID) throws IOException, JDOMException {
List<QueryTest> queries = new LinkedList();
// HashMap queryMap = new HashMap();
SAXBuilder builder = SAXBuilderHelper.createSAXBuilder(false);
Document queryDocument = builder.build(queryFile);
List queryElements = queryDocument.getRootElement().getChildren(TagNames.Elements.QUERY);
Iterator iter = queryElements.iterator();
while (iter.hasNext()) {
Element queryElement = (Element) iter.next();
String queryName = queryElement.getAttributeValue(TagNames.Attributes.NAME);
if (queryElement.getChild(TagNames.Elements.EXCEPTION) == null) {
String uniqueID = querySetID + "_" + queryName;
List<Element> parmChildren = queryElement.getChildren(TagNames.Elements.SQL);
if (parmChildren == null || parmChildren.isEmpty()) {
TestLogger.logDebug("======= Single QueryTest ");
QuerySQL sql = createQuerySQL(queryElement);
QueryTest q = new QueryTest(queryScenarioID, uniqueID, querySetID, new QuerySQL[] { sql }, false);
queries.add(q);
} else {
TestLogger.logDebug("======= QueryTest has multiple sql statements");
QuerySQL[] querysql = new QuerySQL[parmChildren.size()];
int c = 0;
final Iterator<Element> sqliter = parmChildren.iterator();
while (sqliter.hasNext()) {
final Element sqlElement = (Element) sqliter.next();
QuerySQL sql = createQuerySQL(sqlElement);
querysql[c] = sql;
c++;
}
QueryTest q = new QueryTest(queryScenarioID, uniqueID, querySetID, querysql, false);
queries.add(q);
}
// queryMap.put(queryName, query);
} else {
Element exceptionElement = queryElement.getChild(TagNames.Elements.EXCEPTION);
String exceptionType = exceptionElement.getChild(TagNames.Elements.CLASS).getTextTrim();
String uniqueID = querySetID + "_" + queryName;
QuerySQL sql = new QuerySQL(exceptionType, null);
QueryTest q = new QueryTest(queryScenarioID, uniqueID, querySetID, new QuerySQL[] { sql }, true);
queries.add(q);
// queryMap.put(queryName, exceptionType);
}
}
return queries;
}
use of org.jdom.input.SAXBuilder in project teiid by teiid.
the class XMLQueryVisitationStrategy method parseXMLResultsFile.
/**
* Consume an XML results File, produce results as JDOM and add results to the given parent.
* <br>
* @param resultsFile the XML file object that is to be parsed
* @param parent the parent Element to assign results to
* @return the modified parent
* @exception JDOMException if there is an error consuming the message.
*/
public Element parseXMLResultsFile(File resultsFile, Element parent) throws IOException, JDOMException {
SAXBuilder builder = SAXBuilderHelper.createSAXBuilder(false);
Document resultsDocument = builder.build(resultsFile);
List resultElements = resultsDocument.getRootElement().getChildren(TagNames.Elements.QUERY_RESULTS);
Iterator iter = resultElements.iterator();
while (iter.hasNext()) {
Element resultElement = (Element) iter.next();
if (resultElement.getChild(TagNames.Elements.SELECT) == null) {
// We've got an exception
Element exceptionElement = resultElement.getChild(TagNames.Elements.EXCEPTION);
if (exceptionElement != null) {
// ---------------------------------
// Add the ExceptionType element ...
// ---------------------------------
Element typeElement = new Element(TagNames.Elements.EXCEPTION_TYPE);
typeElement.setText(exceptionElement.getChild(TagNames.Elements.EXCEPTION_TYPE).getTextTrim());
parent.addContent(typeElement);
// ---------------------------
// Add the Message element ...
// ---------------------------
Element messageElement = new Element(TagNames.Elements.MESSAGE);
String msg = exceptionElement.getChild(TagNames.Elements.MESSAGE).getTextTrim();
messageElement.setText(StringUtil.removeChars(msg, new char[] { '\r' }));
parent.addContent(messageElement);
// -------------------------
// Add the Class element ...
// -------------------------
Element classElement = new Element(TagNames.Elements.CLASS);
classElement.setText(exceptionElement.getChild(TagNames.Elements.CLASS).getTextTrim());
parent.addContent(classElement);
}
} else {
// We've got results
// -------------------------------
// Read the SELECT elements
// -------------------------------
Element selectElement = resultElement.getChild(TagNames.Elements.SELECT);
resultElement.removeChild(TagNames.Elements.SELECT);
parent.addContent(selectElement);
// -------------------------------
// Read the TABLE of data
// -------------------------------
Element tableElement = resultElement.getChild(TagNames.Elements.TABLE);
resultElement.removeChild(TagNames.Elements.TABLE);
parent.addContent(tableElement);
}
}
return parent;
}
Aggregations