Example usage for org.xml.sax InputSource setCharacterStream

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

Introduction

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

Prototype

public void setCharacterStream(Reader characterStream) 

Source Link

Document

Set the character stream for this input source.

Usage

From source file:com.legstar.cob2xsd.AbstractXsdTester.java

/**
 * Pick an XSD from the file system and return it as a DOM.
 * //from w  ww.  j  av a 2s. c  o m
 * @param xsdFile the XSD file
 * @return an XML DOM
 * @throws Exception if loaf fails
 */
public Document getXMLSchemaAsDoc(final File xsdFile) throws Exception {
    InputSource is = new InputSource();
    is.setCharacterStream(new FileReader(xsdFile));
    return _docBuilder.parse(is);
}

From source file:edu.ku.brc.specify.utilapps.UIControlTOHTML.java

/**
 * @throws TransformerException//w  w w .  ja v a  2 s. c  om
 * @throws TransformerConfigurationException
 * @throws FileNotFoundException
 * @throws IOException
 */
protected void process()
        throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {
    boolean doUIControls = true;
    boolean doForeign = false;
    String outFileName = "UIControls.html";

    TransformerFactory tFactory = TransformerFactory.newInstance();
    if (doUIControls) {

        Transformer transformer = tFactory
                .newTransformer(new StreamSource("src/edu/ku/brc/specify/utilapps/uicontrols.xslt"));
        transformer.transform(new StreamSource("UIControls.xml"),
                new StreamResult(new FileOutputStream("UIControls.html")));

    } else {
        String xsltFileName = doForeign ? foreignXSLT : englishXSLT;

        outFileName = doForeign ? foreignOUT : englishOUT;

        String filePath = "src/edu/ku/brc/specify/utilapps/" + xsltFileName;
        File transFile = new File(filePath);
        if (!transFile.exists()) {
            System.err.println("File path[" + filePath + "] doesn't exist!");
            System.exit(1);
        }
        System.out.println(filePath);

        Transformer transformer = tFactory.newTransformer(new StreamSource(filePath));
        try {
            // Need to read it in as a string because of the embedded German characters
            String xmlStr = FileUtils.readFileToString(new File("config/specify_datamodel.xml"));
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(xmlStr));
            Document doc1 = db.parse(inStream);

            transformer.transform(new DOMSource(doc1), new StreamResult(new FileOutputStream(outFileName)));

        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(UIControlTOHTML.class, ex);
            ex.printStackTrace();
        }
    }
    System.out.println("** The output file[" + outFileName + "] is written.");

}

From source file:net.frontlinesms.plugins.forms.FormsPluginController.java

/**
 * Fabaris_a.zanchi Counts the actual repetitions of a repeatable section
 *
 * @param xmlContent xml message/*from   w w  w. ja  v a2  s  . c  om*/
 * @param repContainerName the name of the repeatables container
 * @return count of repetitions
 * @throws Exception
 */
public static int countRepetitionOfRepeatable(String xmlContent, String repContainerName) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader(xmlContent));
    org.w3c.dom.Document xmlDoc = docBuilder.parse(inStream);
    xmlDoc.getDocumentElement().normalize();
    NodeList repetitionList = xmlDoc.getElementsByTagName(repContainerName);
    return repetitionList.getLength();
}

From source file:net.frontlinesms.plugins.forms.FormsPluginController.java

/**
 * Fabaris_a.zanchi This method extracts the Form Id from the "id" attribute
 * of <data> tag//from  w ww  .j  a  v  a 2 s  .c  om
 *
 * @param xmlContent String who contains the xml
 * @return a String representing the form ID
 * @throws Exception
 */
public static String getFormIdFromXml(String xmlContent) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader(xmlContent));
    org.w3c.dom.Document xmlDoc = docBuilder.parse(inStream);
    xmlDoc.getDocumentElement().normalize();
    NodeList nodeList = xmlDoc.getElementsByTagName("id");
    Node dataNode = nodeList.item(0);
    Element elem = (Element) dataNode;
    // String id = elem.getAttribute("id");
    String id = elem.getFirstChild().getNodeValue();
    return id;

}

From source file:dk.ciid.android.infobooth.xml.XMLParser.java

/**
 * Getting XML DOM element/*from  www  . j  a  v a  2 s.c  o m*/
 * @param XML string
 * */
public Document getDomElement(String xml) {
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }

    return doc;
}

From source file:fr.eyal.lib.data.parser.GenericParser.java

public void parseSheet(final Object content, final int parseType) throws ParseException {

    //Sax method used for XML
    if (parseType == PARSE_TYPE_SAX) {

        //we convert the content to String
        String xml = new String((byte[]) content);
        final SAXParserFactory factory = SAXParserFactory.newInstance();
        try {// ww w . j a va 2 s  .com
            final SAXParser sp = factory.newSAXParser();
            final XMLReader xr = sp.getXMLReader();

            final InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));

            //we set the SAX DefaultHandler
            xr.setContentHandler((DefaultHandler) mHandler);

            Out.v(TAG, "start parsing SAX");

            xr.parse(is);

            Out.v(TAG, "end parsing SAX");

        } catch (final Exception e) {
            e.printStackTrace();
            throw new ParseException("Parsing error");
        }

    } else if (parseType == PARSE_TYPE_JSON) {

        mHandler.parse(content);

    } else if (parseType == PARSE_TYPE_IMAGE) {

        mHandler.parse(content);

    }

}

From source file:net.frontlinesms.plugins.forms.FormsPluginController.java

/**
 * Fabaris_a.zanchi Static utility method to extract tag contents from a xml
 *
 * @param xmlContent The xml conent//from  www.  j a va2  s  .  c  om
 * @param tag the specified xml tag we want to fetch contents from
 * @return a List of Strings with the tags' content
 */
public static List<String> getTagContentFromXml(String xmlContent, String tag) throws Exception {
    List<String> ret = new ArrayList<String>();
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader(xmlContent));
    org.w3c.dom.Document xmlDoc = docBuilder.parse(inStream);
    xmlDoc.getDocumentElement().normalize();
    if (!tag.equals("client_version")) {
        NodeList nodeList = xmlDoc.getElementsByTagName(tag);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node dataNode = nodeList.item(i);
            Element elem = (Element) dataNode;
            ret.add(elem.getFirstChild().getNodeValue());
        }
    } else {
        // Search for client version not knowing its index
        NodeList dataNode = xmlDoc.getElementsByTagName("data");
        Element dataElem = (Element) dataNode.item(0);
        NodeList allNodes = dataElem.getChildNodes();
        for (int i = 0; i < allNodes.getLength(); i++) {
            if (allNodes.item(i).getNodeName().contains("client_version")) {
                ret.add(allNodes.item(i).getTextContent());
            }
        }
    }
    return ret;
}

From source file:net.phamngochai.beefnoodle.ui.DownloadDictionaryActivity.java

public Document stringToXMLDoc(String xml) {
    Document doc = null;/*from  w ww . ja v a 2 s. co  m*/
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        Log.d(TAG, "XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.d(TAG, "Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        Log.d(TAG, "I/O exeption: " + e.getMessage());
        return null;
    }
    return doc;
}

From source file:mupomat.utility.LongLatService.java

public void getLongitudeLatitude(String address) {
    try {//from w ww .j a  va  2  s. c  om
        StringBuilder urlBuilder = new StringBuilder(GEOCODE_REQUEST_URL);
        if (StringUtils.isNotBlank(address)) {
            urlBuilder.append("&address=").append(URLEncoder.encode(address, "UTF-8"));
        }

        final GetMethod getMethod = new GetMethod(urlBuilder.toString());
        try {
            httpClient.executeMethod(getMethod);
            Reader reader = new InputStreamReader(getMethod.getResponseBodyAsStream(),
                    getMethod.getResponseCharSet());

            int data = reader.read();
            char[] buffer = new char[1024];
            Writer writer = new StringWriter();
            while ((data = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, data);
            }

            String result = writer.toString();
            System.out.println(result.toString());

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader("<" + writer.toString().trim()));
            Document doc = db.parse(is);

            strLatitude = getXpathValue(doc, "//GeocodeResponse/result/geometry/location/lat/text()");
            System.out.println("Latitude:" + strLatitude);

            strLongtitude = getXpathValue(doc, "//GeocodeResponse/result/geometry/location/lng/text()");
            System.out.println("Longitude:" + strLongtitude);

        } finally {
            getMethod.releaseConnection();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.jereksel.rommanager.XMLParser.java

/**
 * Getting XML DOM element/*from  ww w .j  a  va  2  s.  com*/
 *
 * @param XML string
 */
public Document getDomElement(String xml) {
    Document doc;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }

    return doc;
}