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

/**
 * Uses the current DocumentBuilderFactory to converts a String
 * representation of XML into a Document.
 *
 * @param xml XML serialized as a String
 * @param nameSpaceAware determines whether the constructed Document
 * is name-space aware/*from w  w w .ja v  a 2 s .co m*/
 * @return Document
 */
public static Document stringToDocument(String xml, boolean nameSpaceAware) {
    Document document = null;
    try {
        if (xml != null) {
            StringReader reader = new StringReader(xml);
            InputSource input = new InputSource(reader);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(nameSpaceAware);
            factory.setValidating(false);
            DocumentBuilder builder = factory.newDocumentBuilder();

            document = builder.parse(input);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return document;
}

From source file:Main.java

/**
 * Converts the xml into a document//from  w  w w .j a va 2 s  . c  o m
 * 
 * @param xml   the xml formated string
 * @return      a new document
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
public static Document xmlToDoc(String xml) throws SAXException, IOException, ParserConfigurationException {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(new StringReader(xml)));

    return doc;
}

From source file:Main.java

/**
 * Utility to get the bytes uri.//from  ww  w  . j a v a  2 s  . c o  m
 * Does NOT handle authenticated URLs,
 * use getInputSourceFromURI(uri, username, password)
 *
 * @param uri the resource to get
 */
public static InputSource getInputSourceFromURI(String uri) {
    return new InputSource(uri);
}

From source file:Main.java

public static Vector<HashMap> xmlToVector222(InputStream is, String xpath) {
    try {//from  ww w.  j  ava  2  s.c o  m
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        InputSource inputSource = new InputSource(is);

        NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET);
        Vector<HashMap> vector = new Vector<HashMap>();

        for (int x = 0; x < nodes.getLength(); x++) {
            NodeList nodeList = nodes.item(x).getChildNodes();
            HashMap hashmap = new HashMap();
            for (int y = 0; y < nodeList.getLength(); y++) {
                Node node = nodeList.item(y);
                if (!node.getNodeName().equals("#text")) {
                    hashmap.put(node.getNodeName(), node.getTextContent());
                }
            }
            vector.add(hashmap);
        }
        return vector;
    } catch (Exception ex) {
        ex.printStackTrace();
        return new Vector();
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T readXMLFromString(Class<?> class1, String content)
        throws JAXBException, FileNotFoundException, SAXException, ParserConfigurationException {
    JAXBContext context = JAXBContext.newInstance(class1);
    Unmarshaller um = context.createUnmarshaller();

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    spf.setFeature("http://xml.org/sax/features/validation", false);

    XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader();
    try (StringReader reader = new StringReader(content)) {
        SAXSource source = new SAXSource(xr, new InputSource(reader));

        T obj = (T) um.unmarshal(source);
        return obj;
    }/* w w  w.  j a  va 2 s  . c  o  m*/
}

From source file:net.sf.joost.Main.java

/**
 * Entry point//from www. j  a  va  2s  .c  o  m
 * @param args array of strings containing the parameter for Joost and
 * at least two URLs addressing xml-source and stx-sheet
 */
public static void main(String[] args) {
    // input filename
    String xmlFile = null;

    // the currently last processor (as XMLFilter)
    Processor processor = null;

    // output filename (optional)
    String outFile = null;

    // custom message emitter class name (optional)
    String meClassname = null;

    // log4j properties filename (optional)
    String log4jProperties = null;

    // log4j message level (this is an object of the class Level)
    Level log4jLevel = null;

    // set to true if a command line parameter was wrong
    boolean wrongParameter = false;

    // set to true if -help was specified on the command line
    boolean printHelp = false;

    // set to true if -pdf was specified on the command line
    boolean doFOP = false;

    // set to true if -nodecl was specified on the command line
    boolean nodecl = false;

    // set to true if -noext was specified on the command line
    boolean noext = false;

    // set to true if -doe was specified on the command line
    boolean doe = false;

    // debugging
    boolean dontexit = false;

    // timings
    boolean measureTime = false;
    long timeStart = 0, timeEnd = 0;

    // needed for evaluating parameter assignments
    int index;

    // serializer SAX -> XML text
    StreamEmitter emitter = null;

    // filenames for the usage and version info
    final String USAGE = "usage.txt", VERSION = "version.txt";

    try {

        // parse command line argument list
        for (int i = 0; i < args.length; i++) {
            if (args[i].trim().length() == 0) {
                // empty parameter? ingore
            }
            // all options start with a '-', but a single '-' means stdin
            else if (args[i].charAt(0) == '-' && args[i].length() > 1) {
                if ("-help".equals(args[i])) {
                    printHelp = true;
                    continue;
                } else if ("-version".equals(args[i])) {
                    printResource(VERSION);
                    logInfoAndExit();
                } else if ("-pdf".equals(args[i])) {
                    doFOP = true;
                    continue;
                } else if ("-nodecl".equals(args[i])) {
                    nodecl = true;
                    continue;
                } else if ("-noext".equals(args[i])) {
                    noext = true;
                    continue;
                } else if ("-doe".equals(args[i])) {
                    doe = true;
                    continue;
                } else if ("-wait".equals(args[i])) {
                    dontexit = true; // undocumented
                    continue;
                } else if ("-time".equals(args[i])) {
                    measureTime = true;
                    continue;
                } else if ("-o".equals(args[i])) {
                    // this option needs a parameter
                    if (++i < args.length && args[i].charAt(0) != '-') {
                        if (outFile != null) {
                            System.err.println("Option -o already specified with " + outFile);
                            wrongParameter = true;
                        } else
                            outFile = args[i];
                        continue;
                    } else {
                        if (outFile != null)
                            System.err.println("Option -o already specified with " + outFile);
                        else
                            System.err.println("Option -o requires a filename");
                        i--;
                        wrongParameter = true;
                    }
                } else if ("-m".equals(args[i])) {
                    // this option needs a parameter
                    if (++i < args.length && args[i].charAt(0) != '-') {
                        if (meClassname != null) {
                            System.err.println("Option -m already specified with " + meClassname);
                            wrongParameter = true;
                        } else
                            meClassname = args[i];
                        continue;
                    } else {
                        if (meClassname != null)
                            System.err.println("Option -m already specified with " + meClassname);
                        else
                            System.err.println("Option -m requires a classname");
                        i--;
                        wrongParameter = true;
                    }
                } else if (DEBUG && "-log-properties".equals(args[i])) {
                    // this option needs a parameter
                    if (++i < args.length && args[i].charAt(0) != '-') {
                        log4jProperties = args[i];
                        continue;
                    } else {
                        System.err.println("Option -log-properties requires " + "a filename");
                        wrongParameter = true;
                    }
                } else if (DEBUG && "-log-level".equals(args[i])) {
                    // this option needs a parameter
                    if (++i < args.length && args[i].charAt(0) != '-') {
                        if ("off".equals(args[i])) {
                            log4jLevel = Level.OFF;
                            continue;
                        } else if ("debug".equals(args[i])) {
                            log4jLevel = Level.DEBUG;
                            continue;
                        } else if ("info".equals(args[i])) {
                            log4jLevel = Level.INFO;
                            continue;
                        } else if ("warn".equals(args[i])) {
                            log4jLevel = Level.WARN;
                            continue;
                        } else if ("error".equals(args[i])) {
                            log4jLevel = Level.ERROR;
                            continue;
                        } else if ("fatal".equals(args[i])) {
                            log4jLevel = Level.FATAL;
                            continue;
                        } else if ("all".equals(args[i])) {
                            log4jLevel = Level.ALL;
                            continue;
                        } else {
                            System.err.println("Unknown parameter for -log-level: " + args[i]);
                            wrongParameter = true;
                            continue;
                        }
                    } else {
                        System.err.println("Option -log-level requires a " + "parameter");
                        wrongParameter = true;
                    }
                } else {
                    System.err.println("Unknown option " + args[i]);
                    wrongParameter = true;
                }
            }
            // command line argument is not an option with a leading '-'
            else if ((index = args[i].indexOf('=')) != -1) {
                // parameter assignment
                if (processor != null)
                    processor.setParameter(args[i].substring(0, index), args[i].substring(index + 1));
                else {
                    System.err.println("Assignment " + args[i] + " must follow an stx-sheet parameter");
                    wrongParameter = true;
                }
                continue;
            } else if (xmlFile == null) {
                xmlFile = args[i];
                continue;
            } else {
                // xmlFile != null, i.e. this is an STX sheet
                ParseContext pContext = new ParseContext();
                pContext.allowExternalFunctions = !noext;
                if (measureTime)
                    timeStart = System.currentTimeMillis();
                Processor proc = new Processor(new InputSource(args[i]), pContext);
                if (nodecl)
                    proc.outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                if (measureTime) {
                    timeEnd = System.currentTimeMillis();
                    System.err.println("Parsing " + args[i] + ": " + (timeEnd - timeStart) + " ms");
                }

                if (processor != null)
                    proc.setParent(processor); // XMLFilter chain
                processor = proc;
            }
        }

        // PDF creation requested
        if (doFOP && outFile == null) {
            System.err.println("Option -pdf requires option -o");
            wrongParameter = true;
        }

        // missing filenames
        if (!printHelp && processor == null) {
            if (xmlFile == null)
                System.err.println("Missing filenames for XML source and " + "STX transformation sheet");
            else
                System.err.println("Missing filename for STX transformation " + "sheet");
            wrongParameter = true;
        }

        if (meClassname != null && !wrongParameter) {
            // create object
            StxEmitter messageEmitter = null;
            try {
                messageEmitter = (StxEmitter) Class.forName(meClassname).newInstance();
            } catch (ClassNotFoundException ex) {
                System.err.println("Class not found: " + ex.getMessage());
                wrongParameter = true;
            } catch (InstantiationException ex) {
                System.err.println("Instantiation failed: " + ex.getMessage());
                wrongParameter = true;
            } catch (IllegalAccessException ex) {
                System.err.println("Illegal access: " + ex.getMessage());
                wrongParameter = true;
            } catch (ClassCastException ex) {
                System.err.println(
                        "Wrong message emitter: " + meClassname + " doesn't implement the " + StxEmitter.class);
                wrongParameter = true;
            }
            if (messageEmitter != null) { // i.e. no exception occurred
                // set message emitter for all processors in the filter chain
                Processor p = processor;
                do {
                    p.setMessageEmitter(messageEmitter);
                    Object o = p.getParent();
                    if (o instanceof Processor)
                        p = (Processor) o;
                    else
                        p = null;
                } while (p != null);
            }
        }

        if (printHelp) {
            printResource(VERSION);
            printResource(USAGE);
            logInfoAndExit();
        }

        if (wrongParameter) {
            System.err.println("Specify -help to get a detailed help message");
            System.exit(1);
        }

        if (DEBUG) {
            // use specified log4j properties file
            if (log4jProperties != null)
                PropertyConfigurator.configure(log4jProperties);

            // set log level specified on the the command line
            if (log4jLevel != null)
                Logger.getRootLogger().setLevel(log4jLevel);
        }

        // The first processor re-uses its XMLReader for parsing the input
        // xmlFile.
        // For a real XMLFilter usage you have to call
        // processor.setParent(yourXMLReader)

        // Connect a SAX consumer
        if (doFOP) {
            // pass output events to FOP
            //              // Version 1: use a FOPEmitter object as XMLFilter
            //              processor.setContentHandler(
            //                 new FOPEmitter(
            //                    new java.io.FileOutputStream(outFile)));

            // Version 2: use a static method to retrieve FOP's content
            // handler and use it directly
            processor.setContentHandler(FOPEmitter.getFOPContentHandler(new java.io.FileOutputStream(outFile)));
        } else {
            // Create XML output
            if (outFile != null) {
                emitter = StreamEmitter.newEmitter(outFile, processor.outputProperties);
                emitter.setSystemId(new File(outFile).toURI().toString());
            } else
                emitter = StreamEmitter.newEmitter(System.out, processor.outputProperties);
            processor.setContentHandler(emitter);
            processor.setLexicalHandler(emitter);
            // the previous line is a short-cut for
            // processor.setProperty(
            //    "http://xml.org/sax/properties/lexical-handler", emitter);

            emitter.setSupportDisableOutputEscaping(doe);
        }

        InputSource is;
        if (xmlFile.equals("-")) {
            is = new InputSource(System.in);
            is.setSystemId("<stdin>");
            is.setPublicId("");
        } else
            is = new InputSource(xmlFile);

        // Ready for take-off
        if (measureTime)
            timeStart = System.currentTimeMillis();

        processor.parse(is);

        if (measureTime) {
            timeEnd = System.currentTimeMillis();
            System.err.println("Processing " + xmlFile + ": " + (timeEnd - timeStart) + " ms");
        }

        //           // check if the Processor copy constructor works
        //           Processor pr = new Processor(processor);
        //           java.util.Properties props = new java.util.Properties();
        //           props.put("encoding", "ISO-8859-2");
        //           StreamEmitter em =
        //              StreamEmitter.newEmitter(System.err, props);
        //           pr.setContentHandler(em);
        //           pr.setLexicalHandler(em);
        //           pr.parse(is);
        //           // end check

        // this is for debugging with the Java Memory Profiler
        if (dontexit) {
            System.err.println("Press Enter to exit");
            System.in.read();
        }

    } catch (java.io.IOException ex) {
        System.err.println(ex.toString());
        System.exit(1);
    } catch (SAXException ex) {
        if (emitter != null) {
            try {
                // flushes the internal BufferedWriter, i.e. outputs
                // the intermediate result
                emitter.endDocument();
            } catch (SAXException exx) {
                // ignore
            }
        }
        Exception embedded = ex.getException();
        if (embedded != null) {
            if (embedded instanceof TransformerException) {
                TransformerException te = (TransformerException) embedded;
                SourceLocator sl = te.getLocator();
                String systemId;
                // ensure that systemId is not null; is this a bug?
                if (sl != null && (systemId = sl.getSystemId()) != null) {
                    // remove the "file://" scheme prefix if it is present
                    if (systemId.startsWith("file://"))
                        systemId = systemId.substring(7);
                    else if (systemId.startsWith("file:"))
                        // bug in JDK 1.4 / Crimson? (see rfc1738)
                        systemId = systemId.substring(5);
                    System.err.println(systemId + ":" + sl.getLineNumber() + ":" + sl.getColumnNumber() + ": "
                            + te.getMessage());
                } else
                    System.err.println(te.getMessage());
            } else {
                // Fatal: this mustn't happen
                embedded.printStackTrace(System.err);
            }
        } else
            System.err.println(ex.toString());
        System.exit(1);
    }
}

From source file:Main.java

public static Document loadDocument(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    //skip DTD validation
    builder.setEntityResolver(new EntityResolver() {

        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }/*  w w  w. java 2 s  .c om*/
    });

    return builder.parse(is);
}

From source file:Main.java

/** Forms an <code>InputSource</code> for parsing XML documents
 *  from a file. Assumes a default character encoding. Returns
 *  <code>null</code> if any of the exceptions is thrown.
 *
 * @param file The file./* w ww . j a  v a2  s.co  m*/
 * @return An <code>InputSource</code> representation.
 */
public static InputSource getInputSource(File file) {
    InputSource retVal = null;
    try {
        retVal = new InputSource(new BufferedReader(new FileReader(file)));
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
    return retVal;
}

From source file:Main.java

/**
 * Formats an XML string for pretty printing. Requires Java 1.6.
 * //  ww  w .j  av a2  s  .co  m
 * Taken from http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java
 * 
 * @param xml
 * @return pretty-print formatted XML
 */
public static String prettyPrintXml(String xml) {
    try {
        final InputSource src = new InputSource(new StringReader(xml));
        final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src)
                .getDocumentElement();
        final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));

        //May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");

        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        final LSSerializer writer = impl.createLSSerializer();

        writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
        writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.

        return writer.writeToString(document);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Parses an XML document from an input stream.
 * @param in the input stream//  w  ww  .j a v  a  2s. c om
 * @return the parsed DOM
 * @throws SAXException if the XML is not valid
 * @throws IOException if there is a problem reading from the input stream
 */
public static Document toDocument(InputStream in) throws SAXException, IOException {
    return toDocument(new InputSource(in));
}