Example usage for org.dom4j.io SAXReader read

List of usage examples for org.dom4j.io SAXReader read

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader read.

Prototype

public Document read(InputSource in) throws DocumentException 

Source Link

Document

Reads a Document from the given InputSource using SAX

Usage

From source file:com.panet.imeta.trans.steps.getxmldata.GetXMLData.java

License:Open Source License

protected boolean setDocument(String StringXML, FileObject file, boolean IsInXMLField, boolean readurl)
        throws KettleException {

    try {//from   w w w .j  a  v a2s.c om
        SAXReader reader = new SAXReader();
        data.stopPruning = false;

        // Validate XML against specified schema?
        if (meta.isValidating()) {
            reader.setValidation(true);
            reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        }

        // Ignore comments?
        if (meta.isIgnoreComments())
            reader.setIgnoreComments(true);

        if (data.prunePath != null) {
            // when pruning is on: reader.read() below will wait until all
            // is processed in the handler
            if (log.isDetailed())
                logDetailed(Messages.getString("GetXMLData.Log.StreamingMode.Activated"));
            reader.addHandler(data.prunePath, new ElementHandler() {
                public void onStart(ElementPath path) {
                    // do nothing here...
                }

                public void onEnd(ElementPath path) {
                    if (isStopped()) {
                        // when a large file is processed and it should be
                        // stopped it is still reading the hole thing
                        // the only solution I see is to prune / detach the
                        // document and this will lead into a
                        // NPE or other errors depending on the parsing
                        // location - this will be treated in the catch part
                        // below
                        // any better idea is welcome
                        if (log.isBasic())
                            logBasic(Messages.getString("GetXMLData.Log.StreamingMode.Stopped"));
                        data.stopPruning = true;
                        path.getCurrent().getDocument().detach(); // trick
                        // to
                        // stop
                        // reader
                        return;
                    }

                    // process a ROW element
                    if (log.isDebug())
                        logDebug(Messages.getString("GetXMLData.Log.StreamingMode.StartProcessing"));
                    Element row = path.getCurrent();
                    try {
                        processStreaming(row.getDocument());
                    } catch (Exception e) {
                        // catch the KettleException or others and forward
                        // to caller, e.g. when applyXPath() has a problem
                        throw new RuntimeException(e);
                    }
                    // prune the tree
                    row.detach();
                    if (log.isDebug())
                        logDebug(Messages.getString("GetXMLData.Log.StreamingMode.EndProcessing"));
                }
            });
        }

        if (IsInXMLField) {
            // read string to parse
            data.document = reader.read(new StringReader(StringXML));
        } else if (readurl) {
            // read url as source
            data.document = reader.read(new URL(StringXML));
        } else {
            // get encoding. By default UTF-8
            String encoding = "UTF-8";
            if (!Const.isEmpty(meta.getEncoding()))
                encoding = meta.getEncoding();
            data.document = reader.read(KettleVFS.getInputStream(file), encoding);
        }

        if (meta.isNamespaceAware())
            prepareNSMap(data.document.getRootElement());
    } catch (Exception e) {
        if (data.stopPruning) {
            // ignore error when pruning
            return false;
        } else {
            throw new KettleException(e);
        }
    }
    return true;
}

From source file:com.partycommittee.service.PcLicenseService.java

public PcLicenseVo getLicense() throws UnsupportedEncodingException {

    SAXReader saxReader = new SAXReader();
    Document document = null;/* ww w .  java2 s. co  m*/
    File file = new File(PcLicenseService.class.getClassLoader().getResource("/").getPath() + "license.xml");
    String license_proc;
    String license_tips;
    try {
        document = saxReader.read(file);
        Element root = document.getRootElement();

        Element license_element = root.element("license");

        license_proc = license_element.getText();

        Element tips_element = root.element("tips");

        license_tips = tips_element.getText();

    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    }

    Des des = new Des();

    String xml = des.DecryPwdStr(license_proc, "hrpweb30");// String 

    if (xml.indexOf("root") <= 0) {
        xml = "<root>" + xml + "</root>";
    }

    PcLicenseVo licensevo = setLicenseVo(xml, license_tips);

    return licensevo;
}

From source file:com.partycommittee.service.PcLicenseService.java

private PcLicenseVo setLicenseVo(String xml, String tips) {

    if (xml == null || xml.equals(""))
        return null;

    PcLicenseVo licensevo = new PcLicenseVo();

    SAXReader saxReader = new SAXReader();
    Document document = null;/*w  w w  .  j  av  a 2  s  . co m*/
    try {
        document = saxReader.read(new ByteArrayInputStream(xml.getBytes()));

        Element root = document.getRootElement();

        Element productno_element = root.element("productno");

        licensevo.setProductno(productno_element.getText());

        Element locknodw_element = root.element("locknodw");

        licensevo.setLocknodw(Integer.parseInt(locknodw_element.getText()));

        Element locknozb_element = root.element("locknozb");

        licensevo.setLocknozb(Integer.parseInt(locknozb_element.getText()));

        Element firstdate_element = root.element("firstdate");

        licensevo.setFirstdate(firstdate_element.getText());

        Element leftdays_element = root.element("leftdays");

        licensevo.setLeftdays(Integer.parseInt(leftdays_element.getText()));

        licensevo.setTips(tips);
    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    }

    return licensevo;

}

From source file:com.partycommittee.service.PcLicenseService.java

public PcLicenseVo setLicenseInfo(String enstr, String tips) {
    if (enstr == null || enstr.equals(""))
        return null;
    if (tips == null || tips.equals(""))
        return null;
    PcLicenseVo licensevo = new PcLicenseVo();
    Des des = new Des();
    try {//  ww  w. ja v a2  s. co  m

        String xml = des.DecryPwdStr(enstr, "hrpweb30");// String 
        System.out.println("xml ==============" + xml);
        if (xml == "" || xml.equals("")) {
            return null;
        }

        if (xml.indexOf("locknodw") <= 0) {
            return null;
        }

        if (xml.indexOf("locknozb") <= 0) {
            return null;
        }

        if (xml.indexOf("root") <= 0) {
            xml = "<root>" + xml + "</root>";
        }
        System.out.println("xml +++++++++++++++++" + xml);
        licensevo = setLicenseVo(xml, tips);

        SAXReader saxReader = new SAXReader();
        Document document = null;
        File file = new File(
                PcLicenseService.class.getClassLoader().getResource("/").getPath() + "license.xml");

        document = saxReader.read(file);
        Element root = document.getRootElement();

        Element license_element = root.element("license");

        license_element.setText(enstr);

        Element tips_element = root.element("tips");

        tips_element.setText(tips);

        String path = PcLicenseService.class.getClassLoader().getResource("/").getPath();
        OutputFormat format = OutputFormat.createPrettyPrint();
        // ??  
        format.setEncoding("UTF8");
        FileOutputStream output = new FileOutputStream(new File(path + "license.xml"));
        XMLWriter writer = new XMLWriter(output, format);
        writer.write(document);
        writer.flush();
        writer.close();

    } catch (Exception e) {
        //e.printStackTrace();
        return null;
    }

    return licensevo;
}

From source file:com.petpet.c3po.utils.XMLUtils.java

License:Apache License

public static boolean validate(File f) {
    try {/*from   ww  w .jav a  2  s  . co  m*/
        SAXParser parser = factory.newSAXParser();
        parser.setProperty(Constants.XML_SCHEMA_PROPERTY, Constants.XML_SCHEMA_LANGUAGE);

        SimpleErrorHandler errorHandler = new SimpleErrorHandler();

        SAXReader reader = new SAXReader(parser.getXMLReader());
        reader.setValidation(true);

        reader.setErrorHandler(errorHandler);
        reader.read(f);

        return errorHandler.isValid();

    } catch (ParserConfigurationException e) {
        LOG.error("ParserConfigurationException: {}", e.getMessage());
    } catch (SAXException e) {
        LOG.error("SAXException: {}", e.getMessage());
    } catch (DocumentException e) {
        LOG.error("DocumentException: {}", e.getMessage());
    } catch (NullPointerException e) {
        LOG.warn("Factory is not initialized. Did you call init()");
    }

    return false;
}

From source file:com.poka.util.XmlSax.java

public Document load(String f, boolean isHiberConfig) {
    Document document = null;//from  w  w  w. j a va 2  s .c o m
    try {
        SAXReader saxReader = new SAXReader();
        //saxReader.setValidation(false);  
        //saxReader.setFeature("http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd", false);              
        //document = saxReader.read(f); //?XML,document
        if (isHiberConfig) {
            saxReader.setEntityResolver(new EntityResolver() {
                @Override
                public InputSource resolveEntity(String publicId, String systemId)
                        throws SAXException, IOException {
                    // TODO Auto-generated method stub
                    InputSource is = new InputSource(this.getClass().getClassLoader()
                            .getResourceAsStream("com/poka/images/hibernate-configuration-3.0.dtd"));
                    is.setPublicId(publicId);
                    is.setSystemId(systemId);
                    return is;
                }
            });
            saxReader.setValidation(true);
        }
        File tFile = new File(f);
        if (!tFile.exists() && !isHiberConfig) {
            document = DocumentHelper.createDocument();
            //    document.addDocType("hibernate-configuration", "-//Hibernate/Hibernate Configuration DTD 3.0//EN", "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd");

            // 
            Element root = DocumentHelper.createElement("root");
            document.setRootElement(root);
        } else {
            document = saxReader.read(tFile);
        }

    } catch (DocumentException ex) {
    }
    return document;
}

From source file:com.poka.util.XmlSax.java

public Document load(URL f) {
    Document document = null;/*w w  w. j a va 2s.c o m*/
    try {
        SAXReader saxReader = new SAXReader();
        String ff = f.getFile();
        File tem = new File(ff);
        if (!tem.exists()) {
            tem.createNewFile();
        }
        document = saxReader.read(tem);
    } catch (IOException | DocumentException ex) {
    }
    return document;
}

From source file:com.processpuzzle.application.domain.ApplicationRepository.java

License:Open Source License

private void readXmlDocument() throws DocumentException, IOException {
    SAXReader reader = new SAXReader();
    Resource xmlResource = resourceLoader.getResource(xmlStoragePath);
    xmlDocument = reader.read(xmlResource.getInputStream());
}

From source file:com.processpuzzle.application.resource.domain.XmlDataLoader.java

License:Open Source License

private void readXml() throws XmlDataLoaderException {
    SAXReader reader = new SAXReader();
    if (schemaPath != null) {
        validator = new JAXPValidator(resourcePath, schemaPath);
        try {//from   www  . java 2s  .com
            validator.validate();
        } catch (XmlSchemaValidationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    try {
        document = reader.read(resource.getInputStream());
    } catch (DocumentException e) {
        throw new XmlValidationException(resourcePath, e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.qagen.osfe.core.FeedDocumentReader.java

License:Apache License

public static Document parseDocument(String fileName) {
    try {//from  www  .  ja v  a  2 s .  c om
        final String homeDir = DirectoryHelper.getHomeDirectory();
        final SAXReader reader = new SAXReader();
        return reader.read(homeDir + fileName);
    } catch (DocumentException e) {
        throw new FeedErrorException(e);
    }
}