org.jdom2.input.sax.XMLReaders Java Examples

The following examples show how to use org.jdom2.input.sax.XMLReaders. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JnlpLauncher.java    From weasis-pacs-connector with Eclipse Public License 2.0 6 votes vote down vote up
protected void parseLauncherTemplate(JnlpTemplate launcher) throws ServletErrorException, IOException {

        // Parse JNLP launcher as JDOM
        Element rootElt = null;

        // Assume the template has UTF-8 encoding
        try (BufferedReader reader =
            new BufferedReader(new InputStreamReader(launcher.realPathURL.toURL().openConnection().getInputStream(),
                StandardCharsets.UTF_8))) {

            rootElt = new SAXBuilder(XMLReaders.NONVALIDATING, null, null).build(reader).getRootElement();
        } catch (JDOMException e) {
            throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE, "Can't parse launcher template", e);
        }

        if (!rootElt.getName().equals(JNLP_TAG_ELT_ROOT)) {
            throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE, "Invalid JNLP launcher template");
        }

        launcher.rootElt = rootElt;
    }
 
Example #2
Source File: MCRMODSCommands.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@MCRCommand(syntax = "load mods document from file {0} for project {1}",
    help = "Load MODS document {0} as MyCoRe Object for project {1}",
    order = 20)
public static void loadFromFile(String modsFileName, String projectID) throws JDOMException, IOException,
    MCRActiveLinkException, SAXException, MCRPersistenceException, MCRAccessException {
    File modsFile = new File(modsFileName);
    if (!modsFile.isFile()) {
        throw new MCRException(String.format(Locale.ENGLISH, "File %s is not a file.", modsFile.getAbsolutePath()));
    }
    SAXBuilder s = new SAXBuilder(XMLReaders.NONVALIDATING, null, null);
    Document modsDoc = s.build(modsFile);
    //force validation against MODS XSD
    MCRXMLHelper.validate(modsDoc, MODS_V3_XSD_URI);
    Element modsRoot = modsDoc.getRootElement();
    if (!modsRoot.getNamespace().equals(MCRConstants.MODS_NAMESPACE)) {
        throw new MCRException(
            String.format(Locale.ENGLISH, "File %s is not a MODS document.", modsFile.getAbsolutePath()));
    }
    if (modsRoot.getName().equals("modsCollection")) {
        List<Element> modsElements = modsRoot.getChildren("mods", MCRConstants.MODS_NAMESPACE);
        for (Element mods : modsElements) {
            saveAsMyCoReObject(projectID, mods);
        }
    } else {
        saveAsMyCoReObject(projectID, modsRoot);
    }
}
 
Example #3
Source File: MCRMODSCommands.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@MCRCommand(syntax = "load mods document from file {0} with files from directory {1} for project {2}",
    help = "Load MODS document {0} as MyCoRe Object with files from direcory {1} for project {2}",
    order = 10)
public static void loadFromFileWithFiles(String modsFileName, String fileDirName, String projectID)
    throws JDOMException, IOException,
    MCRActiveLinkException, SAXException, MCRPersistenceException, MCRAccessException {
    File modsFile = new File(modsFileName);
    if (!modsFile.isFile()) {
        throw new MCRException(String.format(Locale.ENGLISH, "File %s is not a file.", modsFile.getAbsolutePath()));
    }

    File fileDir = new File(fileDirName);
    if (!fileDir.isDirectory()) {
        throw new MCRException(
            String.format(Locale.ENGLISH, "Directory %s is not a directory.", fileDir.getAbsolutePath()));
    }

    SAXBuilder s = new SAXBuilder(XMLReaders.NONVALIDATING, null, null);
    Document modsDoc = s.build(modsFile);
    //force validation against MODS XSD
    MCRXMLHelper.validate(modsDoc, MODS_V3_XSD_URI);
    Element modsRoot = modsDoc.getRootElement();
    if (!modsRoot.getNamespace().equals(MCRConstants.MODS_NAMESPACE)) {
        throw new MCRException(
            String.format(Locale.ENGLISH, "File %s is not a MODS document.", modsFile.getAbsolutePath()));
    }
    if (modsRoot.getName().equals("modsCollection")) {
        throw new MCRException(String.format(Locale.ENGLISH,
            "File %s contains a mods collection witch not supported by this command.", modsFile.getAbsolutePath()));
    } else {
        createDerivate(saveAsMyCoReObject(projectID, modsRoot), fileDir);
    }
}
 
Example #4
Source File: JDOMHandle.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
protected SAXBuilder makeBuilder() {
  SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
  // default to best practices for conservative security including recommendations per
  // https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.md
  builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
  builder.setFeature("http://xml.org/sax/features/external-general-entities", false);
  builder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
  return builder;
}
 
Example #5
Source File: MCRLayoutUtilities.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
private void parseDocument() throws JDOMException, IOException {
    lastModified = getLastModified();
    LOGGER.info("Parsing: {}", docURL);
    parsedDocument = new SAXBuilder(XMLReaders.NONVALIDATING).build(docURL);
}
 
Example #6
Source File: JDOMHandleExample.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
public static void runShortcut(ExampleProperties props)
  throws JDOMException, IOException
{
  String filename = "flipper.xml";

  // register the handle from the extra library
  DatabaseClientFactory.getHandleRegistry().register(
    JDOMHandle.newFactory()
  );

  // create the client
  DatabaseClient client = DatabaseClientFactory.newClient(
    props.host, props.port, props.writerUser, props.writerPassword,
    props.authType);

  // create a manager for documents of any format
  XMLDocumentManager docMgr = client.newXMLDocumentManager();

  // read the example file
  InputStream docStream = Util.openStream("data"+File.separator+filename);
  if (docStream == null)
    throw new IOException("Could not read document example");

  // create an identifier for the document
  String docId = "/example/"+filename;

  // parse the example file with JDOM
  Document writeDocument = new SAXBuilder(XMLReaders.NONVALIDATING).build(
    new InputStreamReader(docStream, "UTF-8"));

  // write the document
  docMgr.writeAs(docId, writeDocument);

  // ... at some other time ...

  // read the document content
  Document readDocument = docMgr.readAs(docId, Document.class);

  String rootName = readDocument.getRootElement().getName();

  // delete the document
  docMgr.delete(docId);

  System.out.println("(Shortcut) Wrote and read /example/"+filename+
    " content with the <"+rootName+"/> root element using JDOM");

  // release the client
  client.release();
}
 
Example #7
Source File: WireFeedInput.java    From rome with Apache License 2.0 4 votes vote down vote up
/**
 * Creates and sets up a org.jdom2.input.SAXBuilder for parsing.
 *
 * @return a new org.jdom2.input.SAXBuilder object
 */
protected SAXBuilder createSAXBuilder() {
    SAXBuilder saxBuilder;
    if (validate) {
        saxBuilder = new SAXBuilder(XMLReaders.DTDVALIDATING);
    } else {
        saxBuilder = new SAXBuilder(XMLReaders.NONVALIDATING);
    }
    saxBuilder.setEntityResolver(RESOLVER);

    //
    // This code is needed to fix the security problem outlined in
    // http://www.securityfocus.com/archive/1/297714
    //
    // Unfortunately there isn't an easy way to check if an XML parser
    // supports a particular feature, so
    // we need to set it and catch the exception if it fails. We also need
    // to subclass the JDom SAXBuilder
    // class in order to get access to the underlying SAX parser - otherwise
    // the features don't get set until
    // we are already building the document, by which time it's too late to
    // fix the problem.
    //
    // Crimson is one parser which is known not to support these features.
    try {
        
        final XMLReader parser = saxBuilder.createParser();
        
        setFeature(saxBuilder, parser, "http://xml.org/sax/features/external-general-entities", false);
        setFeature(saxBuilder, parser, "http://xml.org/sax/features/external-parameter-entities", false);
        setFeature(saxBuilder, parser, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

        if(!allowDoctypes) {
            setFeature(saxBuilder, parser, "http://apache.org/xml/features/disallow-doctype-decl", true);
        }

    } catch (final JDOMException e) {
        throw new IllegalStateException("JDOM could not create a SAX parser", e);
    }
    
    saxBuilder.setExpandEntities(false);

    return saxBuilder;

}
 
Example #8
Source File: FeedTest.java    From rome with Apache License 2.0 4 votes vote down vote up
protected Document getJDomDoc() throws Exception {
    final SAXBuilder saxBuilder = new SAXBuilder(XMLReaders.NONVALIDATING);
    return saxBuilder.build(getFeedReader());
}
 
Example #9
Source File: FeedTest.java    From rome with Apache License 2.0 4 votes vote down vote up
protected Document getJDomDoc() throws Exception {
    final SAXBuilder saxBuilder = new SAXBuilder(XMLReaders.NONVALIDATING);
    return saxBuilder.build(getFeedReader());
}
 
Example #10
Source File: MCRURIResolver.java    From mycore with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Reads xml from an InputStream and returns the parsed root element.
 *
 * @param in
 *            the InputStream that contains the XML document
 * @return the root element of the parsed input stream
 */
protected Element parseStream(InputStream in) throws JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
    builder.setEntityResolver(MCREntityResolver.instance());

    return builder.build(in).getRootElement();
}
 
Example #11
Source File: SAXBuilder.java    From rome with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @deprecated use SAXBuilder(XMLReaderJDOMFactory) with either XMLReaders.DTDVALIDATING or
 *             XMLReaders.NONVALIDATING
 */
@Deprecated
public SAXBuilder(final boolean validate) {
    super(validate ? XMLReaders.DTDVALIDATING : XMLReaders.NONVALIDATING);
}