Package org.w3c.dom

Examples of org.w3c.dom.CharacterData


    if (parentEl == null) {
      return null;
    }
    Node          tempNode = parentEl.getFirstChild();
    StringBuffer  strBuf   = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
      switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE :
        case Node.CDATA_SECTION_NODE : charData = (CharacterData)tempNode;
                                       strBuf.append(charData.getData());
                                       break;
      }
      tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
View Full Code Here


    }
    return element;
  }

  private void setElementValue(Element element, Object value) {
    CharacterData data = null;

    Element prop = element;

    if (value instanceof Collection) {
      Iterator items = ((Collection) value).iterator();
      while (items.hasNext()) {
        Document valdoc = (Document) items.next();
        NodeList list = valdoc.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
          Node newNode = element.getOwnerDocument().importNode(list.item(i), true);
          element.appendChild(newNode);
        }
      }
    } else if (value instanceof Document) {
      Document valdoc = (Document) value;
      Node lastChild = valdoc.getLastChild();
      NodeList list = lastChild.getChildNodes();
      for (int i = 0; i < list.getLength(); i++) {
        Node newNode = element.getOwnerDocument().importNode(list.item(i), true);
        element.appendChild(newNode);
      }
    } else if (value instanceof Element) {
      Node newNode = element.getOwnerDocument().importNode((Element) value, true);
      element.appendChild(newNode);
    } else {
      // Find text child element
      NodeList texts = prop.getChildNodes();
      if (texts.getLength() == 1) {
        Node child = texts.item(0);
        if (child instanceof CharacterData) {
          // Use existing text.
          data = (CharacterData) child;
        } else {
          // Remove non-text, add text.
          prop.removeChild(child);
          Text text = prop.getOwnerDocument().createTextNode(String.valueOf(value));
          prop.appendChild(text);
          data = text;
        }
      } else if (texts.getLength() > 1) {
        // Remove all, add text.
        for (int i = texts.getLength() - 1; i >= 0; i--) {
          prop.removeChild(texts.item(i));
        }
        Text text = prop.getOwnerDocument().createTextNode(String.valueOf(value));
        prop.appendChild(text);
        data = text;
      } else {
        // Add text.
        Text text = prop.getOwnerDocument().createTextNode(String.valueOf(value));
        prop.appendChild(text);
        data = text;
      }
      data.setData(String.valueOf(value));
    }

    // Set type attribute
    // prop.setAttribute("type", value == null ? "null" :
    // value.getClass().getName());
View Full Code Here

*
* @author Philip W. Davis
*/
public void testCharacterData(org.w3c.dom.Document document)
{
  CharacterData charData;
  String compareData, newData, resetData;
  boolean OK = true;
// For debugging*****  println("\n          testCharacterData's outputs:\n");
  charData = (CharacterData) document.getDocumentElement().getElementsByTagName("dBodyLevel31").item(0).getFirstChild(); // charData gets textNode11
  compareData = "dBodyLevel31'sChildTextNode11";
  if (!compareData.equals(charData.getData()))
  {
    System.out.println("Warning!!! CharacterData's 'getData' failed to work properly!\n This may corrupt other CharacterData tests!!!*****");
    OK = false;
 
 
  resetData = charData.getData();
  //  println("This node's original data is: " + charData.getData());

  newData = " This is new data for this node";
  compareData = charData.getData() + newData;
  charData.appendData(newData);
  if (!compareData.equals(charData.getData()))
  {
    System.out.println("Warning!!! CharacterData's 'appendData' failed to work properly!");
    OK = false;
  }
  //  println("This node's appended data is: " + charData.getData());

  compareData = "dBodyLevel";
  charData.deleteData(10, 100);
  if (!compareData.equals(charData.getData()))
  {
    System.out.println("Warning!!! CharacterData's 'deleteData' failed to work properly!");
    OK = false;
  }
  //  println("This node's partially deleted data is: " + charData.getData());

  int length = 10;
  if (!(length == charData.getLength()))
  {
    System.out.println("Warning!!! CharacterData's 'getLength' failed to work properly!");
    OK = false;
  }
  //  println("This node's data length is: " + charData.getLength());

  compareData = "dBody' This is data inserted into this node'Level";
  charData.insertData(5, "' This is data inserted into this node'");
  if (!compareData.equals(charData.getData()))
  {
    System.out.println("Warning!!! CharacterData's 'insertData' failed to work properly!");
    OK = false;
  }
  //  println("This node's updated with insert data is: " + charData.getData());

  compareData = "dBody' This is ' replacement data'ted into this node'Level";
  charData.replaceData(15, 10, "' replacement data'");
  if (!compareData.equals(charData.getData()))
  {
    System.out.println("Warning!!! CharacterData's 'replaceData' failed to work properly!");
    OK = false;
  }
  //  println("This node's updated with replacement data is: " +charData.getData());

  compareData = "New data A123456789B123456789C123456789D123456789E123456789";
  charData.setData("New data A123456789B123456789C123456789D123456789E123456789");
  if (!compareData.equals(charData.getData()))
  {
    System.out.println("Warning!!! CharacterData's 'setData' failed to work properly!");
    OK = false;
  }
  //  println("This node's new data via setData: " + charData.getData());

  compareData = "123456789D123456789E123456789";
  if (!compareData.equals(charData.substringData(30, 30)))
  {
    System.out.println("Warning!!! CharacterData's 'substringData' failed to work properly!");
    OK = false;
  }
  //  println("Using subString 30,30 you get:" + charData.substringData(30,30));

  compareData = "New data A123456789B12345";
  if (!compareData.equals(charData.substringData(0, 25)))
  {
    System.out.println("Warning!!! CharacterData's 'substringData' failed to work properly!");
    OK = false;
  }
  //  println("Using subString 0,25 you get:" + charData.substringData(0,25));

//************************************************* ERROR TESTS
  DTest tests = new DTest();

//!! Throws INDEX_SIZE_ERR ********************
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "deleteData", new Class[]{int.class, int.class},
      new Object[]{new Integer(-1),new Integer(5) }, DOMException.INDEX_SIZE_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "deleteData", new Class[]{int.class, int.class},
      new Object[]{new Integer(2),new Integer(-1) }, DOMException.INDEX_SIZE_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "deleteData", new Class[]{int.class, int.class},
      new Object[]{new Integer(100),new Integer(5) }, DOMException.INDEX_SIZE_ERR ));
 
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "insertData", new Class[]{int.class, String.class},
      new Object[]{new Integer(-1),"Stuff inserted" }, DOMException.INDEX_SIZE_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "insertData", new Class[]{int.class, String.class},
      new Object[]{new Integer(100),"Stuff inserted" }, DOMException.INDEX_SIZE_ERR ));
 
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "replaceData", new Class[]{int.class, int.class, String.class},
      new Object[]{new Integer(-1),new Integer(5),"Replacement stuff" }, DOMException.INDEX_SIZE_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "replaceData", new Class[]{int.class, int.class, String.class},
      new Object[]{new Integer(100),new Integer(5),"Replacement stuff" }, DOMException.INDEX_SIZE_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "replaceData", new Class[]{int.class, int.class, String.class},
      new Object[]{new Integer(2),new Integer(-1),"Replacement stuff" }, DOMException.INDEX_SIZE_ERR ));
 
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "substringData", new Class[]{int.class, int.class},
      new Object[]{new Integer(-1),new Integer(5) }, DOMException.INDEX_SIZE_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "substringData", new Class[]{int.class, int.class},
      new Object[]{new Integer(100),new Integer(5) }, DOMException.INDEX_SIZE_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(charData, "substringData", new Class[]{int.class, int.class},
      new Object[]{new Integer(2),new Integer(-1) }, DOMException.INDEX_SIZE_ERR ));
 

//!! Throws NO_MODIFICATION_ALLOWED_ERR ********
  Node node = document.getDocumentElement().getElementsByTagName("dBodyLevel24").item(0).getFirstChild().getChildNodes().item(0); // node gets ourEntityReference node's child text

  OK &= Assertion.verify(DTest.DOMExceptionsTest(node, "appendData", new Class[]{String.class},
      new Object[]{"new data" }, DOMException.NO_MODIFICATION_ALLOWED_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(node, "deleteData", new Class[]{int.class, int.class},
      new Object[]{new Integer(5),new Integer(10) }, DOMException.NO_MODIFICATION_ALLOWED_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(node, "insertData", new Class[]{int.class, String.class},
      new Object[]{new Integer(5),"Stuff inserted" }, DOMException.NO_MODIFICATION_ALLOWED_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(node, "replaceData", new Class[]{int.class, int.class, String.class},
      new Object[]{new Integer(5),new Integer(10),"Replacementstuff" }, DOMException.NO_MODIFICATION_ALLOWED_ERR ));
  OK &= Assertion.verify(DTest.DOMExceptionsTest(node, "setData", new Class[]{String.class},
      new Object[]{"New setdata stuff"}, DOMException.NO_MODIFICATION_ALLOWED_ERR ));
 
   
// For debugging*****    println("All CharacterData method calls worked correctly.");
  if (!OK)
    System.out.println("\n*****The CharacterData method calls listed above failed, all others worked correctly.*****");
  charData.setData(resetData); // reset node to original data
//  println("");
}
View Full Code Here

  public static String getCharacterDataFromElement(Node e) {

    List<String> values = new ArrayList<String>();
    Node firstChild = e.getFirstChild();
    if (firstChild instanceof CharacterData) {
      CharacterData cd = (CharacterData) firstChild;
      return cd.getData();
    }
    return "?";
  }
View Full Code Here

    if (parentEl == null) {
      return null;
    }
    Node          tempNode = parentEl.getFirstChild();
    StringBuffer  strBuf   = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
      switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE :
        case Node.CDATA_SECTION_NODE : charData = (CharacterData)tempNode;
                                       strBuf.append(charData.getData());
                                       break;
      }
      tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
View Full Code Here

    if (parentEl == null) {
      return null;
    }
    Node          tempNode = parentEl.getFirstChild();
    StringBuffer  strBuf   = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
      switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE :
        case Node.CDATA_SECTION_NODE : charData = (CharacterData)tempNode;
                                       strBuf.append(charData.getData());
                                       break;
      }
      tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
View Full Code Here

     */
    static public String getChildCharacterData(Element parentEl) {
        if (parentEl == null) { return null; }
        Node tempNode = parentEl.getFirstChild();
        StringBuffer strBuf = new StringBuffer();
        CharacterData charData;
        while (tempNode != null) {
            switch (tempNode.getNodeType()) {
                case Node.TEXT_NODE:
                case Node.CDATA_SECTION_NODE:
                    charData = (CharacterData) tempNode;
                    strBuf.append(charData.getData());
                    break;
            }
            tempNode = tempNode.getNextSibling();
        }
        return strBuf.toString();
View Full Code Here

  public static String getCdata(Element elem) {
    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
      Node node = nodes.item(i);
      if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
        CharacterData cdataNode = (CharacterData) node;
        return cdataNode.getData();
      }
    }
    return null;
  }
View Full Code Here

                    w.dataElement (uri, localname, prefix, attrs, content);
                }
                break;
               
            case Node.CDATA_SECTION_NODE:
                CharacterData cd = (CharacterData) node;
                w.characters(cd.getData());
                break;
               
            case Node.COMMENT_NODE:
                CharacterData comment = (CharacterData) node;
                w.comment(comment.getData());
                break;
               
            case Node.PROCESSING_INSTRUCTION_NODE:
                ProcessingInstruction pi = (ProcessingInstruction) node;
                w.processingInstruction(pi.getTarget(), pi.getData());
View Full Code Here

        }
        return element;
    }

    private void setElementValue(Element element, Object value) {
        CharacterData data = null;

        Element prop = element;

        if (value instanceof Collection) {
            Iterator items = ((Collection) value).iterator();
            while (items.hasNext()) {
                Document valdoc = (Document) items.next();
                NodeList list = valdoc.getChildNodes();
                for (int i = 0; i < list.getLength(); i++) {
                    Node newNode = element.getOwnerDocument().importNode(list.item(i), true);
                    element.appendChild(newNode);
                }
            }
        } else if (value instanceof Document) {
            Document valdoc = (Document) value;
            Node lastChild = valdoc.getLastChild();
            NodeList list = lastChild.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                Node newNode = element.getOwnerDocument().importNode(list.item(i), true);
                element.appendChild(newNode);
            }
        } else if (value instanceof Element) {
            Node newNode = element.getOwnerDocument().importNode((Element) value, true);
            element.appendChild(newNode);
        } else {
            // Find text child element
            NodeList texts = prop.getChildNodes();
            if (texts.getLength() == 1) {
                Node child = texts.item(0);
                if (child instanceof CharacterData) {
                    // Use existing text.
                    data = (CharacterData) child;
                } else {
                    // Remove non-text, add text.
                    prop.removeChild(child);
                    Text text = prop.getOwnerDocument().createTextNode(String.valueOf(value));
                    prop.appendChild(text);
                    data = text;
                }
            } else if (texts.getLength() > 1) {
                // Remove all, add text.
                for (int i = texts.getLength() - 1; i >= 0; i--) {
                    prop.removeChild(texts.item(i));
                }
                Text text = prop.getOwnerDocument().createTextNode(String.valueOf(value));
                prop.appendChild(text);
                data = text;
            } else {
                // Add text.
                Text text = prop.getOwnerDocument().createTextNode(String.valueOf(value));
                prop.appendChild(text);
                data = text;
            }
            data.setData(String.valueOf(value));
        }

        // Set type attribute
        //prop.setAttribute("type", value == null ? "null" : value.getClass().getName());
View Full Code Here

TOP

Related Classes of org.w3c.dom.CharacterData

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.