Example usage for org.dom4j.io SAXContentHandler SAXContentHandler

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

Introduction

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

Prototype

public SAXContentHandler() 

Source Link

Usage

From source file:de.tudarmstadt.ukp.csniper.webapp.analysis.uima.HTMLColorMarkerConsumer.java

License:Apache License

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);

    // TODO notify user that the number of arguments is not equal
    // if (markedAnnotations.length != markerColors.length) {
    // throw new IllegalArgumentException(
    // "The amount of colors and annotation types have to be equal.");
    // }//  w w  w  . ja va 2 s.  c  om
    typeCount = Math.min(markerColors.length, markedTypes.length);

    // build map: type->color
    colors = new HashMap<String, String>();
    for (int i = 0; i < typeCount; i++) {
        colors.put(markedTypes[i], markerColors[i]);
    }

    handler = new SAXContentHandler();
    try {
        // add stylesheet information
        handler.startDocument();
        AttributesImpl attr = new AttributesImpl();
        attr.addAttribute("", "", "id", "CDATA", outputClass);
        handler.startElement("", "div", "", attr);

        AttributesImpl style = new AttributesImpl();
        style.addAttribute("", "", "type", "CDATA", "text/css");
        handler.startElement("", "style", "", style);

        StringBuffer headerCss = new StringBuffer();
        headerCss.append("div#");
        headerCss.append(outputClass);
        headerCss.append(" span {");
        headerCss.append(" display:inline-block; padding:0 1 0 1;");
        headerCss.append(" margin:1px; border:solid 1px #FFFFFF; }");

        handler.characters(headerCss.toString().toCharArray(), 0, headerCss.toString().length());
        handler.endElement("", "style", "");
    } catch (SAXException e) {
        throw new ResourceInitializationException(e);
    }
}

From source file:org.apache.commons.jelly.tags.xml.ParseTagSupport.java

License:Apache License

/**
 * Parses the body of this tag and returns the parsed document
 *///from w w  w  . ja  v  a2s .co m
protected Document parseBody(XMLOutput output) throws JellyTagException {
    SAXContentHandler handler = new SAXContentHandler();
    XMLOutput newOutput = new XMLOutput(handler);

    try {
        handler.startDocument();
        invokeBody(newOutput);
        handler.endDocument();
        return handler.getDocument();
    } catch (SAXException e) {
        throw new JellyTagException(e);
    }
}

From source file:org.apache.commons.jelly.tags.xmlunit.XMLUnitTagSupport.java

License:Apache License

/**
 * Parses the body of this tag and returns the parsed document
 *//*www. j a v  a2  s. com*/
protected Document parseBody() throws JellyTagException {
    SAXContentHandler handler = new SAXContentHandler();
    XMLOutput newOutput = new XMLOutput(handler);
    try {
        handler.startDocument();
        invokeBody(newOutput);
        handler.endDocument();
    } catch (SAXException e) {
        throw new JellyTagException(e);
    }
    return handler.getDocument();
}

From source file:org.dcm4chee.web.service.hl7.v2.HL7SendV2Service.java

License:LGPL

private Document readMessage(InputStream mllpIn) throws IOException, SAXException {
    InputSource in = new InputSource(mllpIn);
    in.setEncoding(charsetName);/*from ww  w  .  ja v  a  2s.  c o  m*/
    XMLReader xmlReader = new HL7XMLReader();
    SAXContentHandler hl7in = new SAXContentHandler();
    xmlReader.setContentHandler(hl7in);
    xmlReader.parse(in);
    Document msg = hl7in.getDocument();
    return msg;
}

From source file:org.dcm4chex.archive.hl7.HL7SendService.java

License:LGPL

public int forward(byte[] hl7msg) {
    XMLReader xmlReader = new HL7XMLReader();
    SAXContentHandler hl7in = new SAXContentHandler();
    xmlReader.setContentHandler(hl7in);/*w  ww  . j a  v a 2  s.  c o m*/
    try {
        InputSource in = new InputSource(
                new InputStreamReader(new ByteArrayInputStream(hl7msg), getCharsetName()));
        xmlReader.parse(in);
    } catch (Exception e) {
        log.error("Failed to parse HL7 message", e);
        return -1;
    }
    return forward(hl7msg, hl7in.getDocument());
}

From source file:org.dcm4chex.archive.hl7.HL7SendService.java

License:LGPL

private Document readMessage(InputStream mllpIn) throws IOException, SAXException {
    InputSource in = new InputSource(mllpIn);
    in.setEncoding(getCharsetName());/*from w  w  w  .ja v  a 2s  .  c  o  m*/
    XMLReader xmlReader = new HL7XMLReader();
    SAXContentHandler hl7in = new SAXContentHandler();
    xmlReader.setContentHandler(hl7in);
    xmlReader.parse(in);
    Document msg = hl7in.getDocument();
    return msg;
}

From source file:org.dcm4chex.archive.hl7.HL7ServerService.java

License:LGPL

public void handle(Socket s) throws IOException {
    if (soTimeout > 0) {
        s.setSoTimeout(soTimeout);/*from  w w  w . j  ava 2 s . c om*/
    }
    InetSocketAddress inetAddr = (InetSocketAddress) s.getRemoteSocketAddress();
    MLLPDriver mllpDriver = new MLLPDriver(s.getInputStream(), new BufferedOutputStream(s.getOutputStream()),
            false);
    InputStream mllpIn = mllpDriver.getInputStream();
    XMLReader xmlReader = new HL7XMLReader();
    XMLWriter xmlWriter = new HL7XMLWriter(new OutputStreamWriter(mllpDriver.getOutputStream(), charsetName));
    ContentHandler hl7out = xmlWriter.getContentHandler();
    SAXContentHandler hl7in = new SAXContentHandler();
    xmlReader.setContentHandler(hl7in);
    byte[] bb = new byte[1024];
    while (mllpDriver.hasMoreInput()) {
        ++numberOfReceivedMessages;
        int msglen = 0;
        int read = 0;
        long msgNo = System.currentTimeMillis();
        if (log.isDebugEnabled()) {
            log.debug("Receiving message #" + msgNo + " from " + s);
        }
        do {
            msglen += read;
            if (msglen == bb.length) {
                bb = realloc(bb, bb.length, bb.length * 2);
            }
            read = mllpIn.read(bb, msglen, bb.length - msglen);
        } while (read > 0);
        if (log.isDebugEnabled()) {
            log.debug("Received message  #" + msgNo + "of" + msglen + " bytes from " + s);
        }
        if (fileReceivedHL7) {
            fileReceivedHL7(bb, msglen, new File(logDir, new DecimalFormat("'hl7-'#'.hl7'").format(msgNo)));
        }
        try {
            try {
                ByteArrayInputStream bbin = new ByteArrayInputStream(bb, 0, msglen);
                InputSource in = new InputSource(new InputStreamReader(bbin, charsetName));
                xmlReader.parse(in);
                Document msg = hl7in.getDocument();
                log.info("Received HL7 message:");
                logMessage(msg);
                if (fileReceivedHL7AsXML) {
                    fileReceivedHL7AsXML(msg,
                            new File(logDir, new DecimalFormat("'hl7-'000000'.xml'").format(msgNo)));
                }
                Document newMsg = preprocessHL7(msg, inetAddr);
                if (newMsg != null) {
                    msg = newMsg;
                    log.info("HL7 message changed by preprocess.xsl!");
                    logMessage(msg);
                    if (fileReceivedHL7AsXML) {
                        fileReceivedHL7AsXML(msg, new File(logDir,
                                new DecimalFormat("'hl7-'000000'.preprocessed.xml'").format(msgNo)));
                    }
                    ByteArrayOutputStream bos = new ByteArrayOutputStream(msglen);
                    XMLWriter xmlWriter1 = new HL7XMLWriter(new OutputStreamWriter(bos, getCharsetName()));
                    SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
                    try {
                        Transformer t = tf.newTransformer();
                        t.transform(new DocumentSource(msg), new SAXResult(xmlWriter1.getContentHandler()));
                        bb = bos.toByteArray();
                        msglen = bb.length;
                    } catch (Exception x) {
                        log.error("Failed to preprocess HL7 message!", x);
                    }
                }
                process(bb, msglen, msg, hl7out, inetAddr);
            } catch (SAXException e) {
                throw new HL7Exception("AE", "Failed to parse message ", e);
            }
        } catch (HL7Exception e) {
            if (fileReceivedHL7OnError) {
                fileReceivedHL7(bb, msglen,
                        new File(errLogDir, new DecimalFormat("'hl7-'000000'.hl7'").format(msgNo)));
            }
            log.warn("Processing HL7 failed:", e);
            mllpDriver.discardPendingOutput();
            ack(hl7in.getDocument(), hl7out, suppressErrorResponse ? null : e);
        }
        if (log.isDebugEnabled()) {
            log.debug("Sending response message #" + msgNo + " to " + s);
        }
        mllpDriver.turn();
        if (log.isDebugEnabled()) {
            log.debug("Sent response message #" + msgNo + " to " + s);
        }
    }
}

From source file:org.dcm4chex.archive.hl7.PrefetchService.java

License:LGPL

public void processFile(String filename) throws DocumentException, IOException, SAXException {
    Dataset findRQ = DcmObjectFactory.getInstance().newDataset();
    HL7XMLReader reader = new HL7XMLReader();
    File file = new File(filename);
    SAXContentHandler hl7in = new SAXContentHandler();
    reader.setContentHandler(hl7in);/*from  w  w  w  .java 2  s  .c o m*/
    reader.parse(new InputSource(new FileInputStream(file)));
    Document doc = hl7in.getDocument();
    try {
        File xslFile = FileUtils.toExistingFile(xslPath);
        Transformer t = templates.getTemplates(xslFile).newTransformer();
        t.transform(new DocumentSource(doc), new SAXResult(findRQ.getSAXHandler2(null)));
    } catch (TransformerException e) {
        log.error("Failed to transform into prefetch request", e);
        return;
    }
    this.prepareFindReqDS(findRQ);
    PrefetchOrder order = new PrefetchOrder(findRQ, doc);
    try {
        log.info("Scheduling Test PrefetchOrder:" + order);
        jmsDelegate.queue(queueName, order, Message.DEFAULT_PRIORITY, 0L);
    } catch (Exception e) {
        log.error("Failed to schedule Test Order" + order, e);
    }
}

From source file:org.dom4j.samples.dom.SAXDOMDemo.java

License:Open Source License

protected Document parse(String url) throws Exception {
    SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(url);

    println("Parsed to DOM4J tree using SAX: " + document);

    // now lets make a DOM object
    DOMWriter domWriter = new DOMWriter();
    org.w3c.dom.Document domDocument = domWriter.write(document);

    println("Converted to DOM tree: " + domDocument);

    // now lets read it back as a DOM4J object
    DOMReader domReader = new DOMReader();
    document = domReader.read(domDocument);

    println("Converted to DOM4J tree using DOM: " + document);

    // now lets write it back as SAX events to
    // a SAX ContentHandler which should build up a new document
    SAXContentHandler contentHandler = new SAXContentHandler();
    SAXWriter saxWriter = new SAXWriter(contentHandler, null, contentHandler);

    saxWriter.write(document);/* ww  w.  j  av a2s.c o m*/
    document = contentHandler.getDocument();

    println("Converted DOM4J to SAX events then back to DOM4J: " + document);

    return document;
}

From source file:org.kohsuke.stapler.jelly.groovy.JellyBuilder.java

License:Open Source License

/**
 * Captures the XML fragment generated by the given closure into dom4j DOM tree
 * and return the root element.//from   w  ww .  ja  va  2s . c  om
 *
 * @return null
 *      if nothing was generated.
 */
public Element redirectToDom(Closure c) {
    SAXContentHandler sc = new SAXContentHandler();
    with(new XMLOutput(sc), c);
    return sc.getDocument().getRootElement();
}