Example usage for org.dom4j Text appendText

List of usage examples for org.dom4j Text appendText

Introduction

In this page you can find the example usage for org.dom4j Text appendText.

Prototype

void appendText(String text);

Source Link

Document

Appends the given text to this nodes text value.

Usage

From source file:org.orbeon.oxf.processor.sql.SQLProcessor.java

License:Open Source License

protected void execute(final PipelineContext context, XMLReceiver xmlReceiver) {
    try {// w w w .j  a v a 2 s.co  m
        // Cache, read and interpret the config input
        Config config = readCacheInputAsObject(context, getInputByName(INPUT_CONFIG),
                new CacheableInputReader<Config>() {
                    public Config read(PipelineContext context, ProcessorInput input) {
                        // Read the config input document
                        Node config = readInputAsDOM4J(context, input);
                        Document configDocument = config.getDocument();

                        // Extract XPath expressions and also check whether any XPath expression is used at all
                        // NOTE: This could be done through streaming below as well
                        // NOTE: For now, just match <sql:param select="/*" type="xs:base64Binary"/>
                        List xpathExpressions = new ArrayList();
                        boolean useXPathExpressions = false;
                        for (Iterator i = XPathUtils.selectIterator(configDocument,
                                "//*[namespace-uri() = '" + SQL_NAMESPACE_URI + "' and @select]"); i
                                        .hasNext();) {
                            Element element = (Element) i.next();
                            useXPathExpressions = true;
                            String typeAttribute = element.attributeValue("type");
                            if ("xs:base64Binary".equals(typeAttribute)) {
                                String selectAttribute = element.attributeValue("select");
                                xpathExpressions.add(selectAttribute);
                            }
                        }

                        // Normalize spaces. What this does is to coalesce adjacent text nodes, and to remove
                        // resulting empty text, unless the text is contained within a sql:text element.
                        configDocument.accept(new VisitorSupport() {
                            private boolean endTextSequence(Element element, Text previousText) {
                                if (previousText != null) {
                                    String value = previousText.getText();
                                    if (value == null || value.trim().equals("")) {
                                        element.remove(previousText);
                                        return true;
                                    }
                                }
                                return false;
                            }

                            @Override
                            public void visit(Element element) {
                                // Don't touch text within sql:text elements
                                if (!SQL_NAMESPACE_URI.equals(element.getNamespaceURI())
                                        || !"text".equals(element.getName())) {
                                    Text previousText = null;
                                    for (int i = 0, size = element.nodeCount(); i < size;) {
                                        Node node = element.node(i);
                                        if (node instanceof Text) {
                                            Text text = (Text) node;
                                            if (previousText != null) {
                                                previousText.appendText(text.getText());
                                                element.remove(text);
                                            } else {
                                                String value = text.getText();
                                                // Remove empty text nodes
                                                if (value == null || value.length() < 1) {
                                                    element.remove(text);
                                                } else {
                                                    previousText = text;
                                                    i++;
                                                }
                                            }
                                        } else {
                                            if (!endTextSequence(element, previousText))
                                                i++;
                                            previousText = null;
                                        }
                                    }
                                    endTextSequence(element, previousText);
                                }
                            }
                        });
                        // Create SAXStore
                        try {
                            final SAXStore store = new SAXStore();
                            final LocationSAXWriter locationSAXWriter = new LocationSAXWriter();
                            locationSAXWriter.setContentHandler(store);
                            locationSAXWriter.write(configDocument);
                            // Return the normalized document
                            return new Config(store, useXPathExpressions, xpathExpressions);
                        } catch (SAXException e) {
                            throw new OXFException(e);
                        }
                    }
                });

        // Either read the whole input as a DOM, or try to serialize
        Node data = null;
        XPathXMLReceiver xpathReceiver = null;

        // Check if the data input is connected
        boolean hasDataInput = getConnectedInputs().get(INPUT_DATA) != null;
        if (!hasDataInput && config.useXPathExpressions)
            throw new OXFException(
                    "The data input must be connected when the configuration uses XPath expressions.");
        if (!hasDataInput || !config.useXPathExpressions) {
            // Just use an empty document
            data = Dom4jUtils.NULL_DOCUMENT;
        } else {
            // There is a data input connected and there are some XPath expressions operating on it
            boolean useXPathContentHandler = false;
            if (config.xpathExpressions.size() > 0) {
                // Create XPath content handler
                final XPathXMLReceiver _xpathReceiver = new XPathXMLReceiver();
                // Add expressions and check whether we can try to stream
                useXPathContentHandler = true;
                for (Iterator i = config.xpathExpressions.iterator(); i.hasNext();) {
                    String expression = (String) i.next();
                    boolean canStream = _xpathReceiver.addExpresssion(expression, false);// FIXME: boolean nodeSet
                    if (!canStream) {
                        useXPathContentHandler = false;
                        break;
                    }
                }
                // Finish setting up the XPathContentHandler
                if (useXPathContentHandler) {
                    _xpathReceiver.setReadInputCallback(new Runnable() {
                        public void run() {
                            readInputAsSAX(context, INPUT_DATA, _xpathReceiver);
                        }
                    });
                    xpathReceiver = _xpathReceiver;
                }
            }
            // If we can't stream, read everything in
            if (!useXPathContentHandler)
                data = readInputAsDOM4J(context, INPUT_DATA);
        }

        // Try to read datasource input if any
        Datasource datasource = null;
        {
            List datasourceInputs = (List) getConnectedInputs().get(INPUT_DATASOURCE);
            if (datasourceInputs != null) {
                if (datasourceInputs.size() > 1)
                    throw new OXFException("At most one one datasource input can be connected.");
                ProcessorInput datasourceInput = (ProcessorInput) datasourceInputs.get(0);
                datasource = Datasource.getDatasource(context, this, datasourceInput);
            }
        }

        // Replay the config SAX store through the interpreter
        config.configInput.replay(
                new RootInterpreter(context, getPropertySet(), data, datasource, xpathReceiver, xmlReceiver));
    } catch (OXFException e) {
        throw e;
    } catch (Exception e) {
        throw new OXFException(e);
    }
}