List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:net.sf.janos.model.xml.ResultParser.java
/** * @param controller/*from w ww . j a v a 2 s. c om*/ * @param xml * @return zone group state from the given xml * @throws IOException * @throws SAXException */ public static ZoneGroupState getGroupStateFromResult(String xml) throws SAXException { XMLReader reader = XMLReaderFactory.createXMLReader(); ZoneGroupStateHandler handler = new ZoneGroupStateHandler(); reader.setContentHandler(handler); try { reader.parse(new InputSource(new StringReader(xml))); } catch (IOException e) { // This should never happen - we're not performing I/O! LOG.error("Could not parse group state: ", e); } return new ZoneGroupState(handler.getGroups()); }
From source file:Main.java
/** * Classe utilitaria para converter Document em InputSource * @param documento/* w w w . ja v a 2s . c o m*/ * @return InputSource * @throws TransformerFactoryConfigurationError * @throws TransformerException * @throws TransformerConfigurationException */ private static InputSource converterDoc(Document documento) throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError { Document doc = documento; DOMSource source = new DOMSource(doc); StringWriter xmlAsWriter = new StringWriter(); StreamResult result = new StreamResult(xmlAsWriter); TransformerFactory.newInstance().newTransformer().transform(source, result); StringReader xmlReader = new StringReader(xmlAsWriter.toString()); InputSource iS = new InputSource(xmlReader); return iS; }
From source file:com.codebutler.farebot.mifare.Card.java
public static Card fromXml(String xml) throws Exception { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xml))); Element rootElement = doc.getDocumentElement(); CardType type = CardType.class.getEnumConstants()[Integer.parseInt(rootElement.getAttribute("type"))]; byte[] id = Utils.hexStringToByteArray(rootElement.getAttribute("id")); Date scannedAt = rootElement.hasAttribute("scanned_at") ? new Date(Long.valueOf(rootElement.getAttribute("scanned_at"))) : new Date(0); switch (type) { case MifareDesfire: return DesfireCard.fromXml(id, scannedAt, rootElement); case CEPAS:/*from ww w.java 2 s. com*/ return CEPASCard.fromXML(id, scannedAt, rootElement); case FeliCa: return FelicaCard.fromXml(id, scannedAt, rootElement); default: throw new UnsupportedOperationException("Unsupported card type: " + type); } }
From source file:it.geosolutions.geoserver.jms.impl.handlers.DocumentFile.java
/** * /*from ww w. j ava2s.c o m*/ * @param xmlString * @return * @throws JDOMException * @throws IOException */ protected static Document parser(String xmlString) throws JDOMException, IOException { InputSource source = null; StringReader reader = null; try { reader = new StringReader(xmlString); source = new InputSource(reader); final SAXBuilder builder = new SAXBuilder(); final Document doc = builder.build(source); return doc; } finally { IOUtils.closeQuietly(reader); } }
From source file:Main.java
/** Forms an <code>InputSource</code> for parsing XML documents * from a file. Returns/*from w w w . ja v a2 s. c om*/ * <code>null</code> if any of the exceptions is thrown. * * @param file The file. * @param enc Character encoding to use. * @return An <code>InputSource</code> representation. */ public static InputSource getInputSource(File file, String enc) { InputSource retVal = null; try { retVal = new InputSource(new BufferedReader(new InputStreamReader(new FileInputStream(file), enc))); } catch (Exception ex) { ex.printStackTrace(); return null; } return retVal; }
From source file:Main.java
public static Document getDoc(String xml) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db;// w w w . j a v a 2 s. c o m dbf.setValidating(false); try { dbf.setFeature("http://xml.org/sax/features/namespaces", false); dbf.setFeature("http://xml.org/sax/features/validation", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); db = dbf.newDocumentBuilder(); InputSource source = new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))); Document doc = db.parse(source); return doc; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** Parse an XML document using Saxon. * @param filename The file name of the xml file to be read in * The filename is passed to org.xml.sax.InputSource(String), * so it may be a file name or a URL./* w ww . ja v a2 s .c o m*/ * @return the parsed document. * @exception ParserConfigurationException If there is a problem * creating the DocumentBuilder. * @exception IOException If the filename could not be parsed. */ public static Document parse(String filename) throws ParserConfigurationException, IOException { // FIXME: Throw something other than Exception // System.setProperty("javax.xml.parsers.DocumentBuilderFactory", // "net.sf.saxon.om.DocumentBuilderFactoryImpl"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // We use InputSource here so that we can specify the filename // argument as a jar url so that HSIFToMoML works under Web Start. try { return builder.parse(new InputSource(filename)); } catch (SAXException ex) { // Rethrow this with the filename included. IOException exception = new IOException("Failed to parse '" + filename + "'"); exception.initCause(ex); throw exception; } }
From source file:Main.java
/** * Parses an input stream and generates an XML document. * @param is An input stream to an XML source. * @return An XML doucument.//from ww w . j av a2s . c o m */ public static Document parseXML(InputStream is) { return parseXML(new InputSource(is)); }
From source file:Main.java
/** * Parses the document.// w w w . j a v a 2 s .c o m * * @param xml * the xml * @return the document * @throws ParserConfigurationException * the parser configuration exception * @throws SAXException * the sAX exception * @throws IOException * Signals that an I/O exception has occurred. */ public static Document parseDocument(String xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return builder.parse(new InputSource(new StringReader(xml))); }
From source file:Main.java
public static Document createXML(String xml) { //Get the DOM Builder Factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //Get the DOM Builder DocumentBuilder builder = null; try {//from w w w.jav a 2 s .c om builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } //Load and Parse the XML document //document contains the complete XML as a Tree. Document document = null; InputSource source = new InputSource(new StringReader(xml)); try { document = builder.parse(source); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return document; }