Search in sources :

Example 36 with SAXBuilder

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);
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) JDOMException(org.jdom.JDOMException)

Example 37 with SAXBuilder

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;
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StringReader(java.io.StringReader)

Example 38 with SAXBuilder

use of org.jdom.input.SAXBuilder in project gate-core by GateNLP.

the class PackageGappTask method getJars.

/**
 * Extract the text values from any &lt;JAR&gt; elements contained in the
 * referenced creole.xml file.
 *
 * @return a set with one element for each unique &lt;JAR&gt; 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());
    }
}
Also used : XPath(org.jdom.xpath.XPath) SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) HashSet(java.util.HashSet)

Example 39 with SAXBuilder

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;
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) QueryTest(org.teiid.test.client.QueryTest) Element(org.jdom.Element) Document(org.jdom.Document) QuerySQL(org.teiid.test.client.QuerySQL)

Example 40 with SAXBuilder

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;
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) Document(org.jdom.Document)

Aggregations

SAXBuilder (org.jdom.input.SAXBuilder)145 Document (org.jdom.Document)109 Element (org.jdom.Element)84 IOException (java.io.IOException)60 JDOMException (org.jdom.JDOMException)50 StringReader (java.io.StringReader)35 InputStream (java.io.InputStream)29 File (java.io.File)18 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)13 ArrayList (java.util.ArrayList)12 XPath (org.jdom.xpath.XPath)11 HttpMethod (org.apache.commons.httpclient.HttpMethod)10 XMLOutputter (org.jdom.output.XMLOutputter)10 URL (java.net.URL)9 List (java.util.List)8 GoraException (org.apache.gora.util.GoraException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileInputStream (java.io.FileInputStream)7 Namespace (org.jdom.Namespace)6 StringWriter (java.io.StringWriter)5