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:Main.java

/**
 * Loads a string into a xml document object.
 * //from   w  w  w .j  a  v a2  s . c o m
 * @param xml
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document loadXMLFromString(String xml)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    Document doc = builder.parse(is);
    return doc;
}

From source file:com.easyvalidation.xml.config.EasyValidationXmlConfiguration.java

@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException {

    if (systemId.contains("easy-validation.dtd")) {
        InputStream is = EasyValidationXmlConfiguration.class.getResourceAsStream("/easy-validation.dtd");
        return new InputSource(is);
    } else {// w  ww .j  a v  a  2s . com
        return null; //super.resolveEntity(publicId, systemId);
    }
}

From source file:Main.java

public static Document parse(Reader input, Properties props) throws SAXException, ParserConfigurationException {
    return parse(new InputSource(input), props);
}

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

public static Collection parse(InputStream stream) throws IOException, ParseException {
    return parse(new InputSource(stream), null);
}

From source file:curam.molsa.test.customfunctions.MOLSADOMReader.java

/**
 * Get DOM Document from XML string./*from w w  w. ja va  2 s .  c  o  m*/
 * 
 * @param xml
 * @return MOLSADOMParserCache
 */
public static Document document(final String xml) {

    try {
        return MOLSADOMParserCache.getDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    } catch (final Exception e) {

        throw new RuntimeException(e);
    }
}

From source file:eu.optimis.mi.aggregator.util.XmlUtil.java

@SuppressWarnings("unused")
private static Document getDocument(String xml) {
    try {//from  ww w  .j a v a 2s. c om
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
        return null;
    } catch (ParserConfigurationException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

/**
 * Evaluates the XPath expression against the <code>xml</code> and returns the selected nodes.
 * //from   w  w w . jav  a 2  s . com
 * @param expression Expression to evaluate.
 * @param xml The xml to query.
 * @return The selected nodes.
 * @throws XPathExpressionException If an error occurs evaluating the expression.
 */
public static NodeList getNodes(final String expression, final String xml) throws XPathExpressionException {
    final InputSource source = new InputSource(new StringReader(xml));
    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    return (NodeList) xpath.evaluate(expression, source, XPathConstants.NODESET);
}

From source file:SimpleApp3.java

public JComponent build(String xmlDocument) {
    parser.setContentHandler(this);
    try {//from   w w w.  j av  a 2s .  co m
        parser.parse(new InputSource(new FileInputStream(xmlDocument)));
    } catch (Exception ex) {
        System.out.println(ex);
    }

    return primaryContainer;
}

From source file:com.asual.summer.core.faces.FacesResourceProcessor.java

public static byte[] execute(URL url, InputStream input, String encoding) throws IOException {

    byte[] bytes;

    try {//from  w w  w . j  a  va 2s .c  om

        StringBuilder sb = new StringBuilder();
        UnicodeReader reader = new UnicodeReader(input, encoding);

        try {

            char[] cbuf = new char[32];
            int r;
            while ((r = reader.read(cbuf, 0, 32)) != -1) {
                sb.append(cbuf, 0, r);
            }

            String str = sb.toString();

            if (!str.contains("ui:component")) {

                try {

                    String fileEncoding = reader.getEncoding();
                    InputSource is = new InputSource(new StringReader(str));
                    is.setEncoding(fileEncoding);

                    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

                    XMLDocumentFilter[] filters = { new Writer(baos, fileEncoding) {

                        protected void printStartElement(QName element, XMLAttributes attributes) {
                            fPrinter.print('<');
                            fPrinter.print(element.rawname);
                            int attrCount = attributes != null ? attributes.getLength() : 0;
                            for (int i = 0; i < attrCount; i++) {
                                String aname = attributes.getQName(i);
                                String avalue = attributes.getValue(i);
                                fPrinter.print(' ');
                                fPrinter.print(aname);
                                fPrinter.print("=\"");
                                printAttributeValue(avalue);
                                fPrinter.print('"');
                            }
                            if (HTMLElements.getElement(element.rawname).isEmpty()) {
                                fPrinter.print(' ');
                                fPrinter.print('/');
                            }
                            fPrinter.print('>');
                            fPrinter.flush();
                        }

                        protected void printAttributeValue(String text) {
                            fPrinter.print(StringEscapeUtils.escapeHtml(text));
                            fPrinter.flush();
                        }

                        protected void printEntity(String name) {
                            fPrinter.print('&');
                            fPrinter.print('#');
                            fPrinter.print(HTMLEntities.get(name));
                            fPrinter.print(';');
                            fPrinter.flush();
                        }

                    } };

                    DOMParser parser = new DOMParser();
                    parser.setFeature("http://cyberneko.org/html/features/balance-tags", false);
                    parser.setFeature("http://cyberneko.org/html/features/scanner/notify-builtin-refs", true);
                    parser.setProperty("http://cyberneko.org/html/properties/default-encoding", fileEncoding);
                    parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
                    parser.setProperty("http://cyberneko.org/html/properties/filters", filters);
                    parser.parse(is);

                    str = "<!DOCTYPE html>" + baos.toString(fileEncoding);

                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }

                if (url.getFile().contains("META-INF/templates")) {
                    str = "<ui:component xmlns:ui=\"http://java.sun.com/jsf/facelets\">"
                            + Pattern
                                    .compile("(<\\!DOCTYPE html>)|(</?html[^>]*>)|(<title>[^<]*</title>)",
                                            Pattern.CASE_INSENSITIVE)
                                    .matcher(str).replaceAll("").replaceAll("\\$\\{template\\.body\\}",
                                            "<ui:insert />")
                            + "</ui:component>";
                }

            }

            bytes = str.getBytes(reader.getEncoding());

        } finally {
            reader.close();
        }
    } catch (IOException e) {
        throw e;
    }

    return bytes;
}

From source file:io.milton.http.LockInfoSaxHandler.java

public static LockInfo parseLockInfo(Request request) throws IOException, FileNotFoundException, SAXException {
    InputStream in = request.getInputStream();

    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

    LockInfoSaxHandler handler = new LockInfoSaxHandler();
    reader.setContentHandler(handler);/*from w ww  . ja v a2s .co m*/
    if (log.isDebugEnabled()) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        IOUtils.copy(in, bout);
        byte[] bytes = bout.toByteArray();
        in = new ByteArrayInputStream(bytes);
        log.debug("LockInfo: " + bout.toString());
    }
    reader.parse(new InputSource(in));
    LockInfo info = handler.getInfo();
    info.depth = LockDepth.INFINITY; // todo
    if (info.lockedByUser == null) {
        if (request.getAuthorization() != null) {
            if (request.getAuthorization().getUser() != null) {
                info.lockedByUser = request.getAuthorization().getUser();
            } else {
                Object user = request.getAuthorization().getTag();
                if (user instanceof DiscretePrincipal) {
                    DiscretePrincipal dp = (DiscretePrincipal) user;
                    info.lockedByUser = dp.getPrincipalURL();
                }
            }

        }
    }
    if (info.lockedByUser == null) {
        log.warn("resource is being locked with a null user. This won't really be locked at all...");
    }
    return info;
}