Package org.dom4j

Examples of org.dom4j.Text


            if (object instanceof Element) {
                write((Element) object, namespaceStack);
            } else if (object instanceof CharacterData) {
                if (object instanceof Text) {
                    Text text = (Text) object;
                    write(text.getText());
                } else if (object instanceof CDATA) {
                    write((CDATA) object);
                } else if (object instanceof Comment) {
                    write((Comment) object);
                } else {
View Full Code Here


        if (content != null) {
            for (Iterator iter = content.iterator(); iter.hasNext();) {
                Object object = iter.next();

                if (object instanceof String) {
                    Text text = getDocumentFactory()
                            .createText((String) object);
                    visitor.visit(text);
                } else {
                    Node node = (Node) object;
                    node.accept(visitor);
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 {
                      // JHS
                      String str = lastTextNode.getText();
                      if (str.length() > 0)
                        firstChar = str.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

                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

        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

/*     */
/* 618 */       if ((object instanceof Element))
/* 619 */         write((Element)object, namespaceStack);
/* 620 */       else if ((object instanceof CharacterData)) {
/* 621 */         if ((object instanceof Text)) {
/* 622 */           Text text = (Text)object;
/* 623 */           write(text.getText());
/* 624 */         } else if ((object instanceof CDATA)) {
/* 625 */           write((CDATA)object);
/* 626 */         } else if ((object instanceof Comment)) {
/* 627 */           write((Comment)object);
/*     */         } else {
View Full Code Here

/*      */
/*  895 */     return this;
/*      */   }
/*      */
/*      */   public Element addText(String text) {
/*  899 */     Text node = getDocumentFactory().createText(text);
/*      */
/*  901 */     addNewNode(node);
/*      */
/*  903 */     return this;
/*      */   }
View Full Code Here

/*      */
/*      */   public void normalize()
/*      */   {
/* 1176 */     List content = contentList();
/*      */
/* 1178 */     Text previousText = null;
/*      */
/* 1180 */     int i = 0;
/*      */
/* 1182 */     while (i < content.size()) {
/* 1183 */       Node node = (Node)content.get(i);
/*      */
/* 1185 */       if ((node instanceof Text)) {
/* 1186 */         Text text = (Text)node;
/*      */
/* 1188 */         if (previousText != null) {
/* 1189 */           previousText.appendText(text.getText());
/*      */
/* 1191 */           remove(text);
/*      */         } else {
/* 1193 */           String value = text.getText();
/*      */
/* 1197 */           if ((value == null) || (value.length() <= 0)) {
/* 1198 */             remove(text);
/*      */           } else {
/* 1200 */             previousText = text;
View Full Code Here

/* 1019 */       trim = !this.preserve;
/*      */     }
/*      */
/* 1022 */     if (trim)
/*      */     {
/* 1025 */       Text lastTextNode = null;
/* 1026 */       StringBuffer buff = null;
/* 1027 */       boolean textOnly = true;
/*      */
/* 1029 */       int i = 0; for (int size = element.nodeCount(); i < size; i++) {
/* 1030 */         Node node = element.node(i);
/*      */
/* 1032 */         if ((node instanceof Text)) {
/* 1033 */           if (lastTextNode == null) {
/* 1034 */             lastTextNode = (Text)node;
/*      */           } else {
/* 1036 */             if (buff == null) {
/* 1037 */               buff = new StringBuffer(lastTextNode.getText());
/*      */             }
/*      */
/* 1040 */             buff.append(((Text)node).getText());
/*      */           }
/*      */         } else {
/* 1043 */           if ((!textOnly) && (this.format.isPadText()))
/*      */           {
/* 1046 */             char firstChar = 'a';
/* 1047 */             if (buff != null)
/* 1048 */               firstChar = buff.charAt(0);
/* 1049 */             else if (lastTextNode != null) {
/* 1050 */               firstChar = lastTextNode.getText().charAt(0);
/*      */             }
/*      */
/* 1053 */             if (Character.isWhitespace(firstChar)) {
/* 1054 */               this.writer.write(" ");
/*      */             }
/*      */           }
/*      */
/* 1058 */           if (lastTextNode != null) {
/* 1059 */             if (buff != null) {
/* 1060 */               writeString(buff.toString());
/* 1061 */               buff = null;
/*      */             } else {
/* 1063 */               writeString(lastTextNode.getText());
/*      */             }
/*      */
/* 1066 */             if (this.format.isPadText())
/*      */             {
/* 1069 */               char lastTextChar = 'a';
/* 1070 */               if (buff != null) {
/* 1071 */                 lastTextChar = buff.charAt(buff.length() - 1);
/* 1072 */               } else if (lastTextNode != null) {
/* 1073 */                 String txt = lastTextNode.getText();
/* 1074 */                 lastTextChar = txt.charAt(txt.length() - 1);
/*      */               }
/*      */
/* 1077 */               if (Character.isWhitespace(lastTextChar)) {
/* 1078 */                 this.writer.write(" ");
/*      */               }
/*      */             }
/*      */
/* 1082 */             lastTextNode = null;
/*      */           }
/*      */
/* 1085 */           textOnly = false;
/* 1086 */           writeNode(node);
/*      */         }
/*      */       }
/*      */
/* 1090 */       if (lastTextNode != null) {
/* 1091 */         if ((!textOnly) && (this.format.isPadText()))
/*      */         {
/* 1094 */           char firstChar = 'a';
/* 1095 */           if (buff != null)
/* 1096 */             firstChar = buff.charAt(0);
/*      */           else {
/* 1098 */             firstChar = lastTextNode.getText().charAt(0);
/*      */           }
/*      */
/* 1101 */           if (Character.isWhitespace(firstChar)) {
/* 1102 */             this.writer.write(" ");
/*      */           }
/*      */         }
/*      */
/* 1106 */         if (buff != null) {
/* 1107 */           writeString(buff.toString());
/* 1108 */           buff = null;
/*      */         } else {
/* 1110 */           writeString(lastTextNode.getText());
/*      */         }
/*      */
/* 1113 */         lastTextNode = null;
/*      */       }
/*      */     } else {
/* 1116 */       Node lastTextNode = null;
/*      */
/* 1118 */       int i = 0; for (int size = element.nodeCount(); i < size; i++) {
/* 1119 */         Node node = element.node(i);
/*      */
/* 1121 */         if ((node instanceof Text)) {
/* 1122 */           writeNode(node);
/* 1123 */           lastTextNode = node;
/*      */         } else {
/* 1125 */           if ((lastTextNode != null) && (this.format.isPadText()))
/*      */           {
/* 1128 */             String txt = lastTextNode.getText();
/* 1129 */             char lastTextChar = txt.charAt(txt.length() - 1);
/*      */
/* 1131 */             if (Character.isWhitespace(lastTextChar)) {
/* 1132 */               this.writer.write(" ");
/*      */             }
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.