Example usage for org.dom4j.io DOMReader DOMReader

List of usage examples for org.dom4j.io DOMReader DOMReader

Introduction

In this page you can find the example usage for org.dom4j.io DOMReader DOMReader.

Prototype

public DOMReader() 

Source Link

Usage

From source file:ServiceBES.java

License:Apache License

protected void jobRecover() throws SOAPException, MalformedURLException, ServiceException, RemoteException {

    status = 0;/*from ww  w.  ja  va  2s .c  o  m*/
    String serviceURL;
    String jobID;

    serviceURL = contact.substring(0, contact.indexOf("/urn:gridsam:"));
    jobID = contact.substring(contact.indexOf("urn:gridsam:"), contact.length());
    this.contact = serviceURL;
    EndpointReferenceDocument xActivityEPR = EndpointReferenceDocument.Factory.newInstance();

    if (jobID != null && jobID.startsWith("urn:gridsam:")) {
        JobIdentifierDocument xJobIdentifier = JobIdentifierDocument.Factory.newInstance();
        xJobIdentifier.addNewJobIdentifier().setID(jobID);
        EndpointReferenceType xActivityIdentifier = EndpointReferenceType.Factory.newInstance();
        xActivityIdentifier.addNewAddress().setStringValue(serviceURL);
        xActivityIdentifier.addNewReferenceParameters().set(xJobIdentifier);
        xActivityEPR.addNewEndpointReference();
        xActivityEPR.setEndpointReference(xActivityIdentifier);
    } else {
        status = 1;
        return;
    }
    setEPR(xActivityEPR);
    this.contact = serviceURL;

    //refreshStatus
    GetActivityStatusesDocument xActivities = GetActivityStatusesDocument.Factory.newInstance();
    try {
        EndpointReferenceDocument xActivityIdentifier = EndpointReferenceDocument.Factory
                .parse((new DOMReader())
                        .read(xActivityEPR.getEndpointReference().getDomNode().getOwnerDocument()).asXML());
        xActivities.addNewGetActivityStatuses().addNewActivityIdentifier()
                .set(xActivityIdentifier.getEndpointReference());
    } catch (Throwable exc) {
        info = exc.getMessage().replace('\n', ' ');
        status = 1;
    }

    GetActivityStatusesResponseDocument xResult;
    try {
        SOAPElement xResponse = invokeRemoteOperation("BESFactoryPort/GetActivityStatuses", xActivities);
        xResult = GetActivityStatusesResponseDocument.Factory.parse(xResponse);
    } catch (Throwable exc) {
        if (exc instanceof RemoteException) {
            throw ((RemoteException) exc);
        }
        throw new ServiceException(exc);
    }
    if (xResult == null) {
        info = "No response available";
        status = 1;
        return;
    }
    if (xResult.getGetActivityStatusesResponse().getResponseArray(0).getActivityStatus() != null)
        this.state = xResult.getGetActivityStatusesResponse().getResponseArray(0).getActivityStatus()
                .getState();
    else {
        info = "Bad contact";
        status = 1;
    }
}

From source file:ServiceBES.java

License:Apache License

protected void refreshStatus() throws SOAPException, MalformedURLException, ServiceException, RemoteException {
    status = 0;/*from  www .j av  a 2 s  . com*/

    String xActivityEPR = (new DOMReader())
            .read(this.job.getEPR().getEndpointReference().getDomNode().getOwnerDocument()).asXML();
    GetActivityStatusesDocument xActivities = GetActivityStatusesDocument.Factory.newInstance();
    try {
        EndpointReferenceDocument xActivityIdentifier = EndpointReferenceDocument.Factory.parse(xActivityEPR);
        xActivities.addNewGetActivityStatuses().addNewActivityIdentifier()
                .set(xActivityIdentifier.getEndpointReference());
    } catch (Throwable exc) {
        info = exc.getMessage().replace('\n', ' ');
        status = 1;
    }

    GetActivityStatusesResponseDocument xResult;
    try {
        SOAPElement xResponse = invokeRemoteOperation("BESFactoryPort/GetActivityStatuses", xActivities);
        xResult = GetActivityStatusesResponseDocument.Factory.parse(xResponse);
    } catch (Throwable exc) {
        if (exc instanceof RemoteException) {
            throw ((RemoteException) exc);
        }
        throw new ServiceException(exc);
    }

    if (xResult == null) {
        info = "No response available";
        status = 1;
        return;
    }
    if (xResult.getGetActivityStatusesResponse().getResponseArray(0).getActivityStatus() != null)
        this.state = xResult.getGetActivityStatusesResponse().getResponseArray(0).getActivityStatus()
                .getState();
    else {
        status = 1;
    }
}

From source file:ServiceBES.java

License:Apache License

protected void jobCancel() throws SOAPException, MalformedURLException, ServiceException, RemoteException {

    status = 0;/*from ww w.  j  a  v a2  s.  co  m*/

    String xActivityEPR = (new DOMReader())
            .read(this.job.getEPR().getEndpointReference().getDomNode().getOwnerDocument()).asXML();
    TerminateActivitiesDocument xActivities = TerminateActivitiesDocument.Factory.newInstance();
    try {
        EndpointReferenceDocument xActivityIdentifier = EndpointReferenceDocument.Factory.parse(xActivityEPR);
        xActivities.addNewTerminateActivities().addNewActivityIdentifier()
                .set(xActivityIdentifier.getEndpointReference());
    } catch (Throwable exc) {
        info = exc.getMessage().replace('\n', ' ');
        status = 1;
    }

    TerminateActivitiesResponseDocument xResult;
    try {
        SOAPElement xResponse = invokeRemoteOperation("BESFactoryPort/TerminateActivities", xActivities);
        xResult = TerminateActivitiesResponseDocument.Factory.parse(xResponse);
    } catch (Throwable exc) {
        if (exc instanceof RemoteException) {
            throw ((RemoteException) exc);
        }
        throw new ServiceException(exc);
    }

    if (xResult == null) {
        info = "Not cancelled";
        status = 1;
    }
    if (xResult.getTerminateActivitiesResponse().getResponseArray(0).getTerminated())
        info = "Cancelled";
}

From source file:com.alibaba.antx.config.resource.util.TextBasedPageParser.java

License:Open Source License

/** ?html */
protected Document getHtmlDocument(Resource resource) {
    String contentType = resource.getContentType();

    if (contentType != null && contentType.startsWith("text/html")) {
        try {//from   w  ww.j a v  a  2s .c  o m
            Tidy tidy = new Tidy();
            tidy.setQuiet(true);
            tidy.setXmlOut(true);
            tidy.setErrout(new PrintWriter(new ByteArrayOutputStream()));

            org.w3c.dom.Document dom = tidy.parseDOM(resource.getInputStream(), null);

            return new DOMReader().read(dom);
        } catch (Exception e) {
        }
    }

    return null;
}

From source file:com.alibaba.citrus.springext.util.DomUtil.java

License:Open Source License

/** W3C element??DOM4j element */
public static org.dom4j.Element convertElement(Element element) {
    Document doc;/*from www.  java2  s  . co m*/

    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        throw new IllegalArgumentException("Failed to create dom4j document", e);
    }

    Element clonedElement = (Element) doc.importNode(element, true);

    doc.appendChild(clonedElement);

    DOMReader reader = new DOMReader();
    org.dom4j.Document dom4jDoc = reader.read(doc);

    return dom4jDoc.getRootElement();
}

From source file:com.benasmussen.tools.testeditor.IdExtractor.java

License:Apache License

public Vector<Vector> parse() throws Exception {
    Tidy tidy = new Tidy();
    tidy.setXmlOut(true);/*  w  ww . j  ava  2 s  . c om*/
    tidy.setPrintBodyOnly(true);

    Vector<Vector> data = null;
    try {
        is = new FileInputStream(file);

        // jtidy parse to odm
        org.w3c.dom.Document parseDOM = tidy.parseDOM(is, null);

        // w3c to dom4j
        DOMReader domReader = new DOMReader();
        Document document = domReader.read(parseDOM);

        if (logger.isDebugEnabled()) {
            logger.debug("XML: " + document.asXML());
        }

        data = new Vector<Vector>();

        // select all elements with attribute @id
        List<DefaultElement> elements = document.selectNodes("//*[@id]");
        for (DefaultElement element : elements) {
            Vector<String> row = new Vector<String>();

            row.addElement(element.attributeValue("id"));
            row.addElement(element.getTextTrim());
            data.addElement(row);
        }

    } catch (Exception e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(is);
    }

    return data;
}

From source file:com.cladonia.xml.ExchangerDocument.java

License:Open Source License

/**
 * Sets the W3C Document.// w  w w .  ja  va  2s .c o  m
 *
 * @param the w3c document.
 * @deprecated this does not work!
 */
public void setW3CDocument(Document document) throws DocumentException {
    DOMReader reader = new DOMReader();
    this.document = (XDocument) reader.read(document);

    writeText(); // write the text out of the dom directly

    fireDocumentUpdated(getRoot(), ExchangerDocumentEvent.CONTENT_UPDATED);
}

From source file:com.cnd.greencube.server.util.dom4j.XmlUtils.java

License:Open Source License

/**
 * Create a dom4j DOMReader//from  w  w w.  j  a va  2s .  c  o  m
 */
public DOMReader createDOMReader() {
    if (domReader == null)
        domReader = new DOMReader();
    return domReader;
}

From source file:com.flaptor.hounder.util.HtmlParser.java

License:Apache License

/**
 * Parse the given html document./*w w  w .ja  v  a 2  s .c o m*/
 * @param content the html document to parse.
 * @return the parsed string.
 */
public Output parse(String url, String content) throws Exception {
    // <html xmlns=...>  ==>   <html>
    content = REGEXP_HTML.matcher(content).replaceFirst("<html>");
    // Parser keeps state, synchronize in case its used in a multi-threaded setting.
    Output out = new Output(url);
    synchronized (this) {
        try {
            // use cyberneko to parse the html documents (even broken ones)
            org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(
                    new java.io.ByteArrayInputStream(content.getBytes()));
            parser.parse(inputSource);
        } catch (Exception e) {
            logger.warn("Exception while trying to parse [" + content + "]");
            throw e;
        }
        DOMReader reader = new DOMReader();
        Document htmlDoc;
        try {
            // get the doc that resulted from parsing the text                
            org.w3c.dom.Document document = parser.getDocument();
            htmlDoc = reader.read(document);
        } catch (java.lang.StackOverflowError e) {
            logger.warn("Out of stack memory trying to parse [" + content + "]");
            throw new Exception();
        }
        // this 2 must be before the ignoreXPath, else an ignoreXPath that
        // includes the //TITLE will imply that the title is not indexed
        // extract the links
        extractLinks(htmlDoc, out);

        // extact the title
        extractTitle(htmlDoc, out);

        ignoreXpath(htmlDoc);

        replaceSeparatorTags(htmlDoc);

        // extract the text from the html tags
        extractText(htmlDoc.getRootElement(), out, HTMLPARSER_CONTENT);

        // extract special fields
        extractFields(htmlDoc, out);
    }
    out.close();
    return out;
}

From source file:com.flaptor.util.parser.HtmlParser.java

License:Apache License

/**
 * Parses and fixes an html byte array using Cybernecko.
 * /* w  ww  .  j  ava  2s . co  m*/
 * @param url The base URL for relative links
 * @param bytes The content
 * @return a dom4j Document 
 * @throws InterruptedException
 * @throws Exception
 */
public Document getHtmlDocument(String url, byte[] bytes) throws InterruptedException, Exception {
    Document htmlDoc;
    DOMParser parser = parsers.take();
    try {
        try {
            // use cyberneko to parse the html documents (even broken ones)
            org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(
                    new java.io.ByteArrayInputStream(bytes));
            parser.parse(inputSource);
        } catch (Exception e) {
            logger.warn("Exception while trying to parse " + url);
            throw e;
        }
        DOMReader reader = new DOMReader();
        try {
            // get the doc that resulted from parsing the text                
            org.w3c.dom.Document document = parser.getDocument();
            htmlDoc = reader.read(document);
        } catch (java.lang.StackOverflowError e) {
            logger.warn("Out of stack memory trying to parse " + url);
            throw new Exception(e);
        }

    } finally {
        parsers.add(parser);
    }
    return htmlDoc;
}