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:org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesAppsModification.java

protected String validateGetNewApplicationXMLResponse(String response)
        throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(response));
    Document dom = db.parse(is);/* w  w w.  ja v  a 2  s.c om*/
    NodeList nodes = dom.getElementsByTagName("NewApplication");
    assertEquals("incorrect number of elements", 1, nodes.getLength());
    Element element = (Element) nodes.item(0);
    String appId = WebServicesTestUtils.getXmlString(element, "application-id");
    assertTrue(!appId.isEmpty());
    NodeList maxResourceNodes = element.getElementsByTagName("maximum-resource-capability");
    assertEquals(1, maxResourceNodes.getLength());
    Element maxResourceCapability = (Element) maxResourceNodes.item(0);
    long memory = WebServicesTestUtils.getXmlLong(maxResourceCapability, "memory");
    long vCores = WebServicesTestUtils.getXmlLong(maxResourceCapability, "vCores");
    assertTrue(memory != 0);
    assertTrue(vCores != 0);
    return appId;
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesAppsModification.java

protected static void verifyAppPriorityXML(ClientResponse response, int expectedPriority)
        throws ParserConfigurationException, IOException, SAXException {
    assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
    String xml = response.getEntity(String.class);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    Document dom = db.parse(is);/*  w  ww. ja  v a2 s  .  c  om*/
    NodeList nodes = dom.getElementsByTagName("applicationpriority");
    assertEquals("incorrect number of elements", 1, nodes.getLength());
    Element element = (Element) nodes.item(0);
    int responsePriority = WebServicesTestUtils.getXmlInt(element, "priority");
    assertEquals(expectedPriority, responsePriority);
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesAppsModification.java

protected static void verifyAppQueueXML(ClientResponse response, String queue)
        throws ParserConfigurationException, IOException, SAXException {
    assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
    String xml = response.getEntity(String.class);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    Document dom = db.parse(is);/*from ww  w .  j  a va  2s .  co  m*/
    NodeList nodes = dom.getElementsByTagName("appqueue");
    assertEquals("incorrect number of elements", 1, nodes.getLength());
    Element element = (Element) nodes.item(0);
    String responseQueue = WebServicesTestUtils.getXmlString(element, "queue");
    assertEquals(queue, responseQueue);
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesNodes.java

@Test
public void testNonexistNodeXML() throws JSONException, Exception {
    getNewRMNode("h1", 1234, 5120);
    getNewRMNode("h2", 1235, 5121);
    WebResource r = resource();//w  w  w  .ja v a 2s .com
    try {
        r.path("ws").path("v1").path("cluster").path("nodes").path("node_invalid:99")
                .accept(MediaType.APPLICATION_XML).get(JSONObject.class);

        fail("should have thrown exception on non-existent nodeid");
    } catch (UniformInterfaceException ue) {
        ClientResponse response = ue.getResponse();
        assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
        assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
        String msg = response.getEntity(String.class);
        System.out.println(msg);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(msg));
        Document dom = db.parse(is);
        NodeList nodes = dom.getElementsByTagName("RemoteException");
        Element element = (Element) nodes.item(0);
        String message = WebServicesTestUtils.getXmlString(element, "message");
        String type = WebServicesTestUtils.getXmlString(element, "exception");
        String classname = WebServicesTestUtils.getXmlString(element, "javaClassName");
        verifyNonexistNodeException(message, type, classname);
    }
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesNodes.java

@Test
public void testNodesXML() throws JSONException, Exception {
    WebResource r = resource();//  w ww.jav  a  2  s .  com
    RMNodeImpl rmnode1 = getNewRMNode("h1", 1234, 5120);
    // MockNM nm2 = rm.registerNode("h2:1235", 5121);
    ClientResponse response = r.path("ws").path("v1").path("cluster").path("nodes")
            .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
    assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
    String xml = response.getEntity(String.class);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    Document dom = db.parse(is);
    NodeList nodesApps = dom.getElementsByTagName("nodes");
    assertEquals("incorrect number of elements", 1, nodesApps.getLength());
    NodeList nodes = dom.getElementsByTagName("node");
    assertEquals("incorrect number of elements", 1, nodes.getLength());
    verifyNodesXML(nodes, rmnode1);
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesNodes.java

@Test
public void testSingleNodesXML() throws JSONException, Exception {
    WebResource r = resource();//w  ww .j  a v  a2s.c  o m
    // add h2 node in NEW state
    RMNodeImpl rmnode1 = getNewRMNode("h1", 1234, 5120);
    // MockNM nm2 = rm.registerNode("h2:1235", 5121);
    ClientResponse response = r.path("ws").path("v1").path("cluster").path("nodes").path("h1:1234")
            .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);

    assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
    String xml = response.getEntity(String.class);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    Document dom = db.parse(is);
    NodeList nodes = dom.getElementsByTagName("node");
    assertEquals("incorrect number of elements", 1, nodes.getLength());
    verifyNodesXML(nodes, rmnode1);
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesNodes.java

@Test
public void testNodes2XML() throws JSONException, Exception {
    WebResource r = resource();/*from   w w w.ja v  a  2  s. c o m*/
    getNewRMNode("h1", 1234, 5120);
    getNewRMNode("h2", 1235, 5121);
    ClientResponse response = r.path("ws").path("v1").path("cluster").path("nodes")
            .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
    assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
    String xml = response.getEntity(String.class);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    Document dom = db.parse(is);
    NodeList nodesApps = dom.getElementsByTagName("nodes");
    assertEquals("incorrect number of elements", 1, nodesApps.getLength());
    NodeList nodes = dom.getElementsByTagName("node");
    assertEquals("incorrect number of elements", 2, nodes.getLength());
}

From source file:org.apache.xmlgraphics.image.loader.util.ImageUtil.java

/**
 * Removes any references to InputStreams or Readers from the given Source to prohibit
 * accidental/unwanted use by a component further downstream.
 * @param src the Source object//  www . j  a  v a2 s .c  o  m
 */
public static void removeStreams(Source src) {
    if (src instanceof ImageSource) {
        ImageSource isrc = (ImageSource) src;
        isrc.setImageInputStream(null);
    } else if (src instanceof StreamSource) {
        StreamSource ssrc = (StreamSource) src;
        ssrc.setInputStream(null);
        ssrc.setReader(null);
    } else if (src instanceof SAXSource) {
        InputSource is = ((SAXSource) src).getInputSource();
        if (is != null) {
            is.setByteStream(null);
            is.setCharacterStream(null);
        }
    }
}

From source file:org.apache.xmlgraphics.image.loader.util.ImageUtil.java

/**
 * Closes the InputStreams or ImageInputStreams of Source objects. Any exception occurring
 * while closing the stream is ignored./*from  ww w  .  j a  v  a 2  s  . co m*/
 * @param src the Source object
 */
public static void closeQuietly(Source src) {
    if (src == null) {
        return;
    } else if (src instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) src;
        IOUtils.closeQuietly(streamSource.getInputStream());
        streamSource.setInputStream(null);
        IOUtils.closeQuietly(streamSource.getReader());
        streamSource.setReader(null);
    } else if (src instanceof ImageSource) {
        ImageSource imageSource = (ImageSource) src;
        if (imageSource.getImageInputStream() != null) {
            try {
                imageSource.getImageInputStream().close();
            } catch (IOException ioe) {
                //ignore
            }
            imageSource.setImageInputStream(null);
        }
    } else if (src instanceof SAXSource) {
        InputSource is = ((SAXSource) src).getInputSource();
        if (is != null) {
            IOUtils.closeQuietly(is.getByteStream());
            is.setByteStream(null);
            IOUtils.closeQuietly(is.getCharacterStream());
            is.setCharacterStream(null);
        }
    }
}

From source file:org.archiviststoolkit.plugin.utils.WebServiceHelper.java

/**
 * Method used to extract the created handle from the xml text
 * return by the handle service.//from w  w w  .j a va 2  s  .  c o  m
 *
 * @param returnedXML The returned xml
 * @return The handle that was returned
 */
private static String getHandleFromXML(String returnedXML) {
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(returnedXML));

        Document doc = db.parse(is);

        // get the root element then the url this handle should be bound to
        Element root = doc.getDocumentElement();
        Element handleElement = (Element) root.getElementsByTagName("hs:location").item(0);

        return handleElement.getTextContent();
    } catch (Exception e) {
        return "";
    }
}