Package org.dom4j

Examples of org.dom4j.Text


        String start = text.substring(0, offset);
        String rest = text.substring(offset);
        setText(start);

        Element parent = getParent();
        Text newText = createText(rest);

        if (parent != null) {
          parent.add(newText);
        }
View Full Code Here


      buf.append(a.getValue());
      buf.append('"');
      return buf.toString();
    }
    if (node instanceof Text) {
      Text t = (Text)node;
      return t.getText();
    }
    if (node instanceof ProcessingInstruction) {
      ProcessingInstruction pi = (ProcessingInstruction)node;
      StringBuffer buf = new StringBuffer("<?");
      buf.append(pi.getName());
View Full Code Here

      buf.append(a.getValue());
      buf.append('"');
      return buf.toString();
    }
    if (node instanceof Text) {
      Text t = (Text)node;
      return t.getText();
    }
    if (node instanceof ProcessingInstruction) {
      ProcessingInstruction pi = (ProcessingInstruction)node;
      StringBuffer buf = new StringBuffer("<?");
      buf.append(pi.getName());
View Full Code Here

            log("Found Result: " + object);

            assertTrue("Results not Text objects", object instanceof Text);

            Text text = (Text) object;

            assertTrue("Results should support the parent relationship", text
                    .supportsParent());
            assertTrue(
                    "Results should contain reference to the parent element",
                    text.getParent() != null);
            assertTrue("Results should not reference to the owning document",
                    text.getDocument() != null);
        }
    }
View Full Code Here

                                        if (!otpt.equals(inpt)) {
                                            a.setValue(otpt);
                                        }
                                        break;
                                    case org.w3c.dom.Node.TEXT_NODE:
                                        Text t = (Text) n;
                                        inpt = t.getText();
                                        otpt = processText(inpt, ctx);
                                        if (!otpt.equals(inpt)) {
                                            t.setText(otpt);
                                        }
                                        break;
                                    default:
                                        String msg = "Unsupported node type:  " + n.getNodeTypeName();
                                        throw new RuntimeException(msg);
View Full Code Here

            if (object instanceof Element) {
                appendDOMTree(domDocument, domCurrent, (Element) object);
            } else if (object instanceof String) {
                appendDOMTree(domDocument, domCurrent, (String) object);
            } else if (object instanceof Text) {
                Text text = (Text) object;
                appendDOMTree(domDocument, domCurrent, text.getText());
            } else if (object instanceof CDATA) {
                appendDOMTree(domDocument, domCurrent, (CDATA) object);
            } else if (object instanceof Comment) {
                appendDOMTree(domDocument, domCurrent, (Comment) object);
            } else if (object instanceof Entity) {
View Full Code Here

                String start = text.substring(0, offset);
                String rest = text.substring(offset);
                setText(start);

                Element parent = getParent();
                Text newText = createText(rest);

                if (parent != null) {
                    parent.add(newText);
                }
View Full Code Here

        }

        if (trim) {
            // concatenate adjacent text nodes together
            // so that whitespace trimming works properly
            Text lastTextNode = null;
            StringBuffer buff = null;
            boolean textOnly = true;

            for (int i = 0, size = element.nodeCount(); i < size; i++) {
                Node node = element.node(i);

                if (node instanceof Text) {
                    if (lastTextNode == null) {
                        lastTextNode = (Text) node;
                    } else {
                        if (buff == null) {
                            buff = new StringBuffer(lastTextNode.getText());
                        }

                        buff.append(((Text) node).getText());
                    }
                } else {
                    if (!textOnly && format.isPadText()) {
                        // only add the PAD_TEXT if the text itself starts with
                        // whitespace
                        char firstChar = 'a';
                        if (buff != null) {
                            firstChar = buff.charAt(0);
                        } else if (lastTextNode != null) {
                            firstChar = lastTextNode.getText().charAt(0);
                        }

                        if (Character.isWhitespace(firstChar)) {
                            writer.write(PAD_TEXT);
                        }
                    }

                    if (lastTextNode != null) {
                        if (buff != null) {
                            writeString(buff.toString());
                            buff = null;
                        } else {
                            writeString(lastTextNode.getText());
                        }

                        if (format.isPadText()) {
                            // only add the PAD_TEXT if the text itself ends
                            // with whitespace
                            char lastTextChar = 'a';
                            if (buff != null) {
                                lastTextChar = buff.charAt(buff.length() - 1);
                            } else if (lastTextNode != null) {
                                String txt = lastTextNode.getText();
                                lastTextChar = txt.charAt(txt.length() - 1);
                            }

                            if (Character.isWhitespace(lastTextChar)) {
                                writer.write(PAD_TEXT);
                            }
                        }

                        lastTextNode = null;
                    }

                    textOnly = false;
                    writeNode(node);
                }
            }

            if (lastTextNode != null) {
                if (!textOnly && format.isPadText()) {
                    // only add the PAD_TEXT if the text itself starts with
                    // whitespace
                    char firstChar = 'a';
                    if (buff != null) {
                        firstChar = buff.charAt(0);
                    } else {
                        firstChar = lastTextNode.getText().charAt(0);
                    }

                    if (Character.isWhitespace(firstChar)) {
                        writer.write(PAD_TEXT);
                    }
                }

                if (buff != null) {
                    writeString(buff.toString());
                    buff = null;
                } else {
                    writeString(lastTextNode.getText());
                }

                lastTextNode = null;
            }
        } else {
            Node lastTextNode = null;

            for (int i = 0, size = element.nodeCount(); i < size; i++) {
                Node node = element.node(i);

                if (node instanceof Text) {
                    writeNode(node);
                    lastTextNode = node;
                } else {
                    if ((lastTextNode != null) && format.isPadText()) {
                        // only add the PAD_TEXT if the text itself ends with
                        // whitespace
                        String txt = lastTextNode.getText();
                        char lastTextChar = txt.charAt(txt.length() - 1);

                        if (Character.isWhitespace(lastTextChar)) {
                            writer.write(PAD_TEXT);
                        }
View Full Code Here

        return this;
    }

    public Element addText(String text) {
        Text node = getDocumentFactory().createText(text);

        addNewNode(node);

        return this;
    }
View Full Code Here

     * @since DOM Level 2
     */
    public void normalize() {
        List content = contentList();

        Text previousText = null;

        int i = 0;

        while (i < content.size()) {
            Node node = (Node) content.get(i);

            if (node instanceof Text) {
                Text text = (Text) node;

                if (previousText != null) {
                    previousText.appendText(text.getText());

                    remove(text);
                } else {
                    String value = text.getText();

                    // only remove empty Text nodes, not whitespace nodes
                    // if ( value == null || value.trim().length() <= 0 ) {
                    if ((value == null) || (value.length() <= 0)) {
                        remove(text);
View Full Code Here

TOP

Related Classes of org.dom4j.Text

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.