Example usage for org.jdom2.input SAXBuilder build

List of usage examples for org.jdom2.input SAXBuilder build

Introduction

In this page you can find the example usage for org.jdom2.input SAXBuilder build.

Prototype

@Override
public Document build(final String systemId) throws JDOMException, IOException 

Source Link

Document

This builds a document from the supplied URI.

Usage

From source file:com.rometools.rome.io.impl.Atom03Generator.java

License:Open Source License

protected void fillContentElement(final Element contentElement, final Content content) throws FeedException {

    final String type = content.getType();
    if (type != null) {
        final Attribute typeAttribute = new Attribute("type", type);
        contentElement.setAttribute(typeAttribute);
    }//w  w w.j  a  va 2 s . c  o m

    final String mode = content.getMode();
    if (mode != null) {
        final Attribute modeAttribute = new Attribute("mode", mode.toString());
        contentElement.setAttribute(modeAttribute);
    }

    final String value = content.getValue();
    if (value != null) {

        if (mode == null || mode.equals(Content.ESCAPED)) {

            contentElement.addContent(value);

        } else if (mode.equals(Content.BASE64)) {

            contentElement.addContent(Base64.encode(value));

        } else if (mode.equals(Content.XML)) {

            final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
            tmpDocString.append(value);
            tmpDocString.append("</tmpdoc>");
            final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
            Document tmpDoc;

            try {
                final SAXBuilder saxBuilder = new SAXBuilder();
                tmpDoc = saxBuilder.build(tmpDocReader);
            } catch (final Exception ex) {
                throw new FeedException("Invalid XML", ex);
            }

            final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent();
            contentElement.addContent(children);
        }

    }
}

From source file:com.rometools.rome.io.impl.Atom10Generator.java

License:Open Source License

protected void fillContentElement(final Element contentElement, final Content content) throws FeedException {

    final String type = content.getType();

    String atomType = type;/* w  ww  .  jav  a 2  s.c o m*/

    if (type != null) {
        // Fix for issue #39 "Atom 1.0 Text Types Not Set Correctly"
        // we're not sure who set this value, so ensure Atom types are used
        if ("text/plain".equals(type)) {
            atomType = Content.TEXT;
        } else if ("text/html".equals(type)) {
            atomType = Content.HTML;
        } else if ("application/xhtml+xml".equals(type)) {
            atomType = Content.XHTML;
        }

        final Attribute typeAttribute = new Attribute("type", atomType);
        contentElement.setAttribute(typeAttribute);
    }

    final String href = content.getSrc();
    if (href != null) {
        final Attribute srcAttribute = new Attribute("src", href);
        contentElement.setAttribute(srcAttribute);
    }

    final String value = content.getValue();
    if (value != null) {

        if (atomType != null && (atomType.equals(Content.XHTML) || atomType.indexOf("/xml") != -1
                || atomType.indexOf("+xml") != -1)) {

            final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
            tmpDocString.append(value);
            tmpDocString.append("</tmpdoc>");
            final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
            Document tmpDoc;
            try {
                final SAXBuilder saxBuilder = new SAXBuilder();
                tmpDoc = saxBuilder.build(tmpDocReader);
            } catch (final Exception ex) {
                throw new FeedException("Invalid XML", ex);
            }
            final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent();
            contentElement.addContent(children);

        } else {

            // must be type html, text or some other non-XML format
            // JDOM will escape property for XML
            contentElement.addContent(value);

        }

    }
}

From source file:com.rometools.rome.io.impl.Atom10Parser.java

License:Open Source License

/**
 * Parse entry from reader./*  www  .j a v  a 2 s  .c o  m*/
 */
public static Entry parseEntry(final Reader rd, final String baseURI, final Locale locale)
        throws JDOMException, IOException, IllegalArgumentException, FeedException {

    // Parse entry into JDOM tree
    final SAXBuilder builder = new SAXBuilder();
    final Document entryDoc = builder.build(rd);
    final Element fetchedEntryElement = entryDoc.getRootElement();
    fetchedEntryElement.detach();

    // Put entry into a JDOM document with 'feed' root so that Rome can
    // handle it
    final Feed feed = new Feed();
    feed.setFeedType("atom_1.0");
    final WireFeedOutput wireFeedOutput = new WireFeedOutput();
    final Document feedDoc = wireFeedOutput.outputJDom(feed);
    feedDoc.getRootElement().addContent(fetchedEntryElement);

    if (baseURI != null) {
        feedDoc.getRootElement().setAttribute("base", baseURI, Namespace.XML_NAMESPACE);
    }

    final WireFeedInput input = new WireFeedInput(false, locale);
    final Feed parsedFeed = (Feed) input.build(feedDoc);
    return parsedFeed.getEntries().get(0);
}

From source file:com.rometools.rome.io.WireFeedInput.java

License:Open Source License

/**
 * Builds an WireFeed (RSS or Atom) from an Reader.
 * <p>/*from  w w w  .  j  av a 2 s  . com*/
 * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom2.Document)'.
 * <p>
 *
 * @param reader Reader to read to create the WireFeed.
 * @return the WireFeed read from the Reader.
 * @throws IllegalArgumentException thrown if feed type could not be understood by any of the
 *             underlying parsers.
 * @throws FeedException if the feed could not be parsed
 *
 */
public WireFeed build(Reader reader) throws IllegalArgumentException, FeedException {
    final SAXBuilder saxBuilder = createSAXBuilder();
    try {
        if (xmlHealerOn) {
            reader = new XmlFixerReader(reader);
        }
        final Document document = saxBuilder.build(reader);
        return this.build(document);
    } catch (final JDOMParseException ex) {
        throw new ParsingFeedException("Invalid XML: " + ex.getMessage(), ex);
    } catch (final IllegalArgumentException ex) {
        throw ex;
    } catch (final Exception ex) {
        throw new ParsingFeedException("Invalid XML", ex);
    }
}

From source file:com.rometools.rome.io.WireFeedInput.java

License:Open Source License

/**
 * Builds an WireFeed (RSS or Atom) from an W3C SAX InputSource.
 * <p>/*ww  w. j a v a 2s .c  o  m*/
 * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom2.Document)'.
 * <p>
 *
 * @param is W3C SAX InputSource to read to create the WireFeed.
 * @return the WireFeed read from the W3C SAX InputSource.
 * @throws IllegalArgumentException thrown if feed type could not be understood by any of the
 *             underlying parsers.
 * @throws FeedException if the feed could not be parsed
 *
 */
public WireFeed build(final InputSource is) throws IllegalArgumentException, FeedException {
    final SAXBuilder saxBuilder = createSAXBuilder();
    try {
        final Document document = saxBuilder.build(is);
        return this.build(document);
    } catch (final JDOMParseException ex) {
        throw new ParsingFeedException("Invalid XML: " + ex.getMessage(), ex);
    } catch (final IllegalArgumentException ex) {
        throw ex;
    } catch (final Exception ex) {
        throw new ParsingFeedException("Invalid XML", ex);
    }
}

From source file:com.seleniumtests.util.squashta.TaScriptGenerator.java

License:Apache License

/**
 * Search for tests in TestNG files/*from   www  . j a v a  2  s . c  o  m*/
 * @param path
 * @param application
 * @return
 */
public List<SquashTaTestDef> parseTestNgXml() {

    // look for feature file into data folder
    File dir = Paths.get(srcPath, "data", application, "testng").toFile();
    if (!dir.exists()) {
        return new ArrayList<>();
    }

    File[] testngFiles = dir.listFiles((d, filename) -> filename.endsWith(".xml"));

    List<SquashTaTestDef> testDefs = new ArrayList<>();

    for (File testngFile : testngFiles) {

        Document doc;
        SAXBuilder sxb = new SAXBuilder();
        sxb.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        try {

            doc = sxb.build(testngFile);
        } catch (Exception e) {
            logger.error(String.format("Fichier %s illisible: %s", testngFile, e.getMessage()));
            return testDefs;
        }

        Element suite = doc.getRootElement();
        if (!"suite".equalsIgnoreCase(suite.getName())) {
            continue;
        }

        for (Element test : suite.getChildren("test")) {
            readTestTag(test, testDefs, testngFile);

        }
    }

    return testDefs;
}

From source file:com.soulgalore.velocity.MergeXMLWithVelocity.java

License:Apache License

String merge(String xml, String template, String[] extraXMLs) throws JDOMException, IOException {

    final File xmlFile = new File(xml);
    final SAXBuilder b = new SAXBuilder(new XMLReaderSAX2Factory(false));
    final Document doc = b.build(xmlFile);
    context.put(CONTEXT_DOCUMENT, doc);//ww w.j ava2 s .c o m

    // TODO Old legacy naming, make this cleaner in the future
    int name = 2;
    for (String extraXML : extraXMLs) {
        final File xmlFileExtra = new File(extraXML);
        final Document doc2 = b.build(xmlFileExtra);
        context.put(CONTEXT_DOCUMENT + name, doc2);
        name++;
    }

    final StringWriter writer = new StringWriter();
    final Template fromTemplate = ve.getTemplate(template);
    fromTemplate.merge(context, writer);
    return writer.toString();

}

From source file:com.speedment.codgen.example.uml.Generate.java

License:Open Source License

public static void main(String... params) {
    final Generator gen = new JavaGenerator(new JavaTransformFactory(), new UMLTransformFactory());

    final URL umlPath = Generate.class.getResource(PATH + "ExampleUML.cdg");

    try {/*  ww  w .ja  va  2s  . co m*/
        final SAXBuilder jdomBuilder = new SAXBuilder();
        final Document doc = jdomBuilder.build(umlPath);
        final Element classDiagram = doc.getRootElement();

        //gen.metaOn(classDiagram.getChild("ClassDiagramRelations").getChildren()

        System.out.println(gen.metaOn(classDiagram.getChild("ClassDiagramComponents").getChildren(), File.class)
                .map(Meta::getResult).flatMap(gen::metaOn).map(Meta::getResult)
                .collect(joining("\n----------------------------------\n")));

    } catch (JDOMException ex) {
        Logger.getLogger(Generate.class.getName()).log(Level.SEVERE, "Failed to parse XML structure.", ex);
    } catch (IOException ex) {
        Logger.getLogger(Generate.class.getName()).log(Level.SEVERE,
                "Could not load file '" + umlPath.toExternalForm() + "'.", ex);
    }

}

From source file:com.sun.syndication.io.impl.Atom03Generator.java

License:Open Source License

protected void fillContentElement(Element contentElement, Content content) throws FeedException {

    if (content.getType() != null) {
        Attribute typeAttribute = new Attribute("type", content.getType());
        contentElement.setAttribute(typeAttribute);
    }/*from   w w w .  j a v a 2 s  .  c o m*/

    String mode = content.getMode();
    if (mode != null) {
        Attribute modeAttribute = new Attribute("mode", content.getMode().toString());
        contentElement.setAttribute(modeAttribute);
    }

    if (content.getValue() != null) {

        if (mode == null || mode.equals(Content.ESCAPED)) {
            contentElement.addContent(content.getValue());
        } else if (mode.equals(Content.BASE64)) {
            contentElement.addContent(Base64.encode(content.getValue()));
        } else if (mode.equals(Content.XML)) {

            StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
            tmpDocString.append(content.getValue());
            tmpDocString.append("</tmpdoc>");
            StringReader tmpDocReader = new StringReader(tmpDocString.toString());
            Document tmpDoc;

            try {
                SAXBuilder saxBuilder = new SAXBuilder();
                tmpDoc = saxBuilder.build(tmpDocReader);
            } catch (Exception ex) {
                throw new FeedException("Invalid XML", ex);
            }

            List children = tmpDoc.getRootElement().removeContent();
            contentElement.addContent(children);
        }
    }
}

From source file:com.sun.syndication.io.impl.Atom10Generator.java

License:Open Source License

protected void fillContentElement(Element contentElement, Content content) throws FeedException {

    String type = content.getType();
    String atomType = type;//from w  w  w  .  java2  s.c  o m
    if (type != null) {
        // Fix for issue #39 "Atom 1.0 Text Types Not Set Correctly"
        // we're not sure who set this value, so ensure Atom types are used
        if ("text/plain".equals(type))
            atomType = Content.TEXT;
        else if ("text/html".equals(type))
            atomType = Content.HTML;
        else if ("application/xhtml+xml".equals(type))
            atomType = Content.XHTML;

        Attribute typeAttribute = new Attribute("type", atomType);
        contentElement.setAttribute(typeAttribute);
    }
    String href = content.getSrc();
    if (href != null) {
        Attribute srcAttribute = new Attribute("src", href);
        contentElement.setAttribute(srcAttribute);
    }
    if (content.getValue() != null) {
        if (atomType != null && (atomType.equals(Content.XHTML) || (atomType.indexOf("/xml")) != -1
                || (atomType.indexOf("+xml")) != -1)) {

            StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
            tmpDocString.append(content.getValue());
            tmpDocString.append("</tmpdoc>");
            StringReader tmpDocReader = new StringReader(tmpDocString.toString());
            Document tmpDoc;
            try {
                SAXBuilder saxBuilder = new SAXBuilder();
                tmpDoc = saxBuilder.build(tmpDocReader);
            } catch (Exception ex) {
                throw new FeedException("Invalid XML", ex);
            }
            List children = tmpDoc.getRootElement().removeContent();
            contentElement.addContent(children);
        } else {
            // must be type html, text or some other non-XML format
            // JDOM will escape property for XML
            contentElement.addContent(content.getValue());
        }
    }
}