Example usage for org.w3c.dom Element getFirstChild

List of usage examples for org.w3c.dom Element getFirstChild

Introduction

In this page you can find the example usage for org.w3c.dom Element getFirstChild.

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:org.apache.servicemix.http.security.HttpSecurityTest.java

public void testWSSecBadCred() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    FileUtil.copyInputStream(getClass().getResourceAsStream("request-bc.xml"), out);
    String request = out.toString();
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod("http://localhost:8192/WSSec/");
    try {//www . j  a v  a 2 s  . c o m
        method.setDoAuthentication(true);
        method.setRequestEntity(new StringRequestEntity(request));
        int state = client.executeMethod(method);
        String str = method.getResponseBodyAsString();
        log.info(str);
        assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, state);
        Element e = new SourceTransformer().toDOMElement(new StringSource(str));
        assertEquals("Envelope", e.getLocalName());
        e = (Element) e.getFirstChild();
        assertEquals("Body", e.getLocalName());
        e = (Element) e.getFirstChild();
        assertEquals("Fault", e.getLocalName());
    } finally {
        method.releaseConnection();
    }
}

From source file:org.apache.servicemix.http.security.HttpSecurityTest.java

public void testWSSecUnkownUser() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    FileUtil.copyInputStream(getClass().getResourceAsStream("request-uu.xml"), out);
    String request = out.toString();
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod("http://localhost:8192/WSSec/");
    try {//from w w w  .j  a  va2  s. co m
        method.setDoAuthentication(true);
        method.setRequestEntity(new StringRequestEntity(request));
        int state = client.executeMethod(method);
        String str = method.getResponseBodyAsString();
        log.info(str);
        assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, state);
        Element e = new SourceTransformer().toDOMElement(new StringSource(str));
        assertEquals("Envelope", e.getLocalName());
        e = (Element) e.getFirstChild();
        assertEquals("Body", e.getLocalName());
        e = (Element) e.getFirstChild();
        assertEquals("Fault", e.getLocalName());
    } finally {
        method.releaseConnection();
    }
}

From source file:org.apromore.plugin.deployment.yawl.YAWLEngineClient.java

private Node getYAWLMessage(final String response) throws DeploymentException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {//from  w  ww.java  2 s  .co  m
        DocumentBuilder db = dbf.newDocumentBuilder();
        Element message = db.parse(new InputSource(new StringReader(response))).getDocumentElement();
        if (message.getNodeName().equals("response")) {
            return message.getFirstChild();
        } else {
            throw new DeploymentException("Could not talk to YAWL engine. Invalid respone: " + response);
        }
    } catch (SAXException | IOException | ParserConfigurationException e) {
        throw new DeploymentException("Could not talk to YAWL engine.", e);
    }

}

From source file:com.duroty.lucene.parser.HtmlParser.java

/**
 * DOCUMENT ME!//ww w  .  j  av  a 2  s  .c  o  m
 *
 * @return DOCUMENT ME!
 */
public String getTitle() {
    if (this.title != null) {
        return this.title;
    }

    if (element == null) {
        return null;
    }

    String title = null;

    NodeList nl = element.getElementsByTagName("title");

    if (nl.getLength() > 0) {
        Element titleElement = ((Element) nl.item(0));
        Text text = (Text) titleElement.getFirstChild();

        if (text != null) {
            title = text.getData();
        }
    }

    return title;
}

From source file:com.commonsware.android.internet.WeatherDemo.java

void buildForecasts(String raw) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(raw)));
    NodeList times = doc.getElementsByTagName("start-valid-time");

    for (int i = 0; i < times.getLength(); i++) {
        Element time = (Element) times.item(i);
        Forecast forecast = new Forecast();

        forecasts.add(forecast);/*from   w  w w .j  a  v a  2 s .  c  om*/
        forecast.setTime(time.getFirstChild().getNodeValue());
    }

    NodeList temps = doc.getElementsByTagName("value");

    for (int i = 0; i < temps.getLength(); i++) {
        Element temp = (Element) temps.item(i);
        Forecast forecast = forecasts.get(i);

        forecast.setTemp(new Integer(temp.getFirstChild().getNodeValue()));
    }

    NodeList icons = doc.getElementsByTagName("icon-link");

    for (int i = 0; i < icons.getLength(); i++) {
        Element icon = (Element) icons.item(i);
        Forecast forecast = forecasts.get(i);

        forecast.setIcon(icon.getFirstChild().getNodeValue());
    }
}

From source file:importer.handler.post.stages.Splitter.java

/**
 * Rule 1 states that the versions of every element are also 
 * present in its parents, up to the root.
 * @param elem the element to start from
 *//* w ww.jav  a2s . co m*/
void verifyRule1(Element elem) {
    String pVers = elem.getAttribute(VERSIONS);
    Node child = elem.getFirstChild();
    // descend depth-first
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            String cVers = ((Element) child).getAttribute(VERSIONS);
            if (cVers != null && pVers != null) {
                if (!verifyMembership(pVers, cVers))
                    System.out.println("Rule 1 broken for element " + elem.getNodeName() + " with child "
                            + child.getNodeName() + ": \"" + cVers + "\" not contained in \"" + pVers + "\"");
                // recurse
                verifyRule1((Element) child);
            } else
                System.out.println("Missing parent (" + pVers + ") or child versions (" + cVers + ")");
        }
        child = child.getNextSibling();
    }
}

From source file:gov.nist.healthcare.ttt.parsing.Parsing.java

public static DirectAddressing getDirectAddressing(String mtom) throws MessagingException, IOException {

    SOAPWithAttachment swa = Parsing.parseMtom(mtom);
    DirectAddressing directAddressing = new DirectAddressing();
    Envelope env = (Envelope) JAXB.unmarshal(new StringReader(swa.getSoap()), Envelope.class);
    List<Object> headers = env.getHeader().getAny();
    if (headers == null) {
        return directAddressing;
    }/*from  w  w  w  .  j a va  2 s .co m*/
    Iterator it = headers.iterator();
    boolean foundDirectAddressBlock = false;
    while (it.hasNext()) {
        Element header = (Element) it.next();
        if (header.getLocalName().equals(ELEMENT_NAME_DIRECT_ADDRESS_BLOCK)
                && header.getNamespaceURI().equals(NAMESPACE_DIRECT)) {
            foundDirectAddressBlock = true;
            NodeList directFrom = header.getElementsByTagNameNS(NAMESPACE_DIRECT, ELEMENT_NAME_DIRECT_FROM);
            NodeList directTo = header.getElementsByTagNameNS(NAMESPACE_DIRECT, ELEMENT_NAME_DIRECT_TO);

            directAddressing.setDirectFrom(directFrom.item(0).getFirstChild().getNodeValue());
            directAddressing.setDirectTo(directTo.item(0).getFirstChild().getNodeValue());
        } else if (header.getLocalName().equals(ELEMENT_NAME_WSA_MESSAGEID)
                && header.getNamespaceURI().equals(NAMESPACE_WSA)) {
            directAddressing.setMessageID(header.getFirstChild().getNodeValue());
        }
    }
    return directAddressing;
}

From source file:com.duroty.lucene.bookmark.BookmarkToLuceneBookmark.java

/**
 * DOCUMENT ME!//  w ww  .  j a  va  2s . co m
 *
 * @param mime DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 * @throws URISyntaxException
 * @throws IOException
 */
public LuceneBookmark parse(String idint, BookmarkObj bookmarkObj) throws URISyntaxException, IOException {
    if ((idint == null) || (bookmarkObj == null)) {
        return null;
    }

    LuceneBookmark luceneBookmark = new LuceneBookmark(idint);

    luceneBookmark.setCacheDate(new Date());

    String comments = null;

    try {
        comments = factory.parse(bookmarkObj.getComments(), "text/html",
                Charset.defaultCharset().displayName());
    } catch (Exception ex) {
        if (ex != null) {
            comments = ex.getMessage();
        }
    }

    luceneBookmark.setComments(comments);

    if (!StringUtils.isBlank(comments)) {
        luceneBookmark.setNotebook(true);
    }

    String url = bookmarkObj.getUrl();
    HttpContent httpContent = new HttpContent(new URL(url));
    MimeType mimeType = httpContent.getContentType();
    String contentType = "text/html";

    if (mimeType != null) {
        contentType = mimeType.getBaseType();
    }

    InputStream inputStream = httpContent.newInputStream();

    if (!StringUtils.isBlank(bookmarkObj.getTitle())) {
        luceneBookmark.setTitle(bookmarkObj.getTitle());
    } else {
        Vector elements = Extractor.getElements(httpContent.newInputStream(), null, "title");

        Text text = null;

        if ((elements != null) && (elements.size() == 1)) {
            Element element = (Element) elements.get(0);
            text = (Text) element.getFirstChild();
        }

        if (text != null) {
            luceneBookmark.setTitle(text.getData());
        } else {
            luceneBookmark.setTitle(url);
        }
    }

    String charset = httpContent.getCharset();

    if (charset == null) {
        charset = Charset.defaultCharset().displayName();
    }

    String contents = null;

    try {
        contents = factory.parse(inputStream, contentType, charset);
    } catch (Exception ex) {
        if (ex != null) {
            contents = ex.getMessage();
        }
    }

    luceneBookmark.setContents(contents);

    luceneBookmark.setDepth(bookmarkObj.getDepth());
    luceneBookmark.setFlagged(bookmarkObj.isFlagged());
    luceneBookmark.setInsertDate(new Date());
    luceneBookmark.setKeywords(bookmarkObj.getKeywords());

    //luceneBookmark.setNotebook(bookmarkObj.isNotebook());
    luceneBookmark.setParent(String.valueOf(bookmarkObj.getParent()));
    luceneBookmark.setUrl(url);
    luceneBookmark.setUrlStr(url);

    return luceneBookmark;
}

From source file:info.novatec.testit.livingdoc.report.XmlReport.java

private String getTextValue(String tagName, int index) {
    String textVal = null;//from w w  w. j  a  v a 2s . c o  m
    NodeList nl = root.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0 && index < nl.getLength()) {
        Element el = (Element) nl.item(index);
        Node node = el.getFirstChild();
        if (node != null) {
            textVal = node.getNodeValue();
        }
    }

    return textVal;
}