Example usage for org.xml.sax InputSource InputSource

List of usage examples for org.xml.sax InputSource InputSource

Introduction

In this page you can find the example usage for org.xml.sax InputSource InputSource.

Prototype

public InputSource(Reader characterStream) 

Source Link

Document

Create a new input source with a character stream.

Usage

From source file:knop.psfj.exporter.HTMLDataSetExporter.java

/**
 * Load xml from string./*from  w w w  .  j  a  v a2 s .  c  o  m*/
 *
 * @param xml the xml
 * @return the document
 * @throws Exception the exception
 */
public static Document loadXMLFromString(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));

    return builder.parse(is);
}

From source file:Main.java

public static Document createDocument(File file) throws IOException, SAXException {
    return createDocument(new InputSource(new FileReader(file)));
}

From source file:gov.nih.nci.cabig.caaers.utils.XmlValidator.java

public static boolean validateAgainstSchema(String xmlContent, String xsdUrl, StringBuffer validationResult) {
    boolean validXml = false;
    try {//from   ww  w . j  a v a2  s. c o  m
        // parse an XML document into a DOM tree
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setValidating(false);
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
        Document document = parser.parse(new InputSource(new StringReader(xmlContent)));

        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(getResources(xsdUrl)[0].getFile());

        Schema schema = schemaFactory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
        validXml = true;
    } catch (FileNotFoundException ex) {
        throw new CaaersSystemException("File Not found Exception", ex);
    } catch (IOException ioe) {
        validationResult.append(ioe.getMessage());
        logger.error(ioe.getMessage());
    } catch (SAXParseException spe) {
        validationResult.append("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
        logger.error("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
    } catch (SAXException e) {
        validationResult.append(e.toString());
        logger.error(e.toString());
    } catch (ParserConfigurationException pce) {
        validationResult.append(pce.getMessage());
    }
    return validXml;
}

From source file:Utils.java

public static Document newDocumentFromInputStream(InputStream in) {
    DocumentBuilderFactory factory = null;
    DocumentBuilder builder = null;
    Document ret = null;//from  w  ww  . ja  v  a 2s.  c o m

    try {
        factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    try {
        ret = builder.parse(new InputSource(in));
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:elaborate.util.XmlUtil.java

public static boolean isWellFormed(String body) {
    try {/*from  w  ww .ja v  a 2s.  com*/
        SAXParser parser;
        parser = SAXParserFactory.newInstance().newSAXParser();
        DefaultHandler dh = new DefaultHandler();
        parser.parse(new InputSource(new StringReader(body)), dh);
    } catch (ParserConfigurationException e1) {
        e1.printStackTrace();
        return false;
    } catch (SAXException e1) {
        e1.printStackTrace();
        Log.error("body={}", body);
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:de.nava.informa.parsers.FeedParser.java

/**
 * Parser feed behind the given URL and build channel.
 *
 * @param cBuilder specific channel builder to use.
 * @param url      URL to use as data source.
 * @return parsed channel.//  w  ww  . jav a 2  s  .c om
 * @throws IOException    if IO errors occur.
 * @throws ParseException if parsing is not possible.
 */
public static ChannelIF parse(ChannelBuilderIF cBuilder, String url) throws IOException, ParseException {
    URL aURL = null;
    try {
        aURL = new URL(url);
    } catch (java.net.MalformedURLException e) {
        LOGGER.warn("Could not create URL for " + url);
    }
    return parse(cBuilder, new InputSource(url), aURL);
}

From source file:Main.java

public static boolean validateWithDTDUsingSAX(String xml) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);/* w  ww  .jav a 2 s .com*/
    factory.setNamespaceAware(true);

    SAXParser parser = factory.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    reader.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException e) throws SAXException {
            System.out.println("WARNING : " + e.getMessage()); // do nothing
        }

        public void error(SAXParseException e) throws SAXException {
            System.out.println("ERROR : " + e.getMessage());
            throw e;
        }

        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("FATAL : " + e.getMessage());
            throw e;
        }
    });
    reader.parse(new InputSource(xml));
    return true;
}

From source file:Main.java

/**
 * Utility to get the bytes uri/*www.j  a  v  a 2 s .com*/
 *
 * @param source the resource to get
 */
public static InputSource sourceToInputSource(Source source) {
    if (source instanceof SAXSource) {
        return ((SAXSource) source).getInputSource();
    } else if (source instanceof DOMSource) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Node node = ((DOMSource) source).getNode();
        if (node instanceof Document) {
            node = ((Document) node).getDocumentElement();
        }
        Element domElement = (Element) node;
        ElementToStream(domElement, baos);
        InputSource isource = new InputSource(source.getSystemId());
        isource.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
        return isource;
    } else if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        InputSource isource = new InputSource(ss.getSystemId());
        isource.setByteStream(ss.getInputStream());
        isource.setCharacterStream(ss.getReader());
        isource.setPublicId(ss.getPublicId());
        return isource;
    } else {
        return getInputSourceFromURI(source.getSystemId());
    }
}

From source file:no.kantega.commons.util.XMLHelper.java

public static Document getDocument(String input) throws SystemException {
    Document doc = null;//from   w  ww  . j a v  a  2 s .c om
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = docFactory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(input)));
    } catch (Exception e) {
        log.error("Error converting String to Document", e);
        throw new SystemException("Error converting String to Document", e);
    }

    return doc;
}

From source file:com.tonbeller.jpivot.core.ModelFactory.java

/**
 * creates a model from an xml configuration file
 * @param url url of model configuration file
 * @return Model//from w  w  w .ja  v a 2s  . com
 * @throws SAXException
 * @throws IOException
 */
public static Model instance(URL url) throws SAXException, IOException {
    Digester digester = new Digester();
    digester.setValidating(false);

    ModelHolder root = new ModelHolder();
    digester.push(root);

    digester.addObjectCreate("model", "missing \"class\" attribute", "class");
    digester.addSetProperties("model");
    digester.addSetNext("model", "setModel");

    digester.addObjectCreate("model/extension", "missing \"class\" attribute", "class");
    digester.addSetProperties("model/extension");
    digester.addSetNext("model/extension", "addExtension");

    InputSource is = new InputSource(url.toExternalForm());
    digester.parse(is);
    return root.getModel();
}