Java Code Examples for org.w3c.dom.Node#CDATA_SECTION_NODE

The following examples show how to use org.w3c.dom.Node#CDATA_SECTION_NODE . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
private List<TangYuanNode> parseDynamicTags(XmlNodeWrapper node) {
	List<TangYuanNode> contents = new ArrayList<TangYuanNode>();
	NodeList children = node.getNode().getChildNodes();
	for (int i = 0; i < children.getLength(); i++) {
		XmlNodeWrapper child = node.newXMlNode(children.item(i));
		if (child.getNode().getNodeType() == Node.CDATA_SECTION_NODE || child.getNode().getNodeType() == Node.TEXT_NODE) {
			String data = child.getStringBody("");
			if (isEmpty(data)) {
				continue;
			}
			// 使用新的sqlText节点
			contents.add(new SqlTextNode(data));
			// log.info("-----------data:" + data);
		} else if (child.getNode().getNodeType() == Node.ELEMENT_NODE) {
			String nodeName = child.getNode().getNodeName();
			// log.info("-----------name:" + nodeName);
			NodeHandler handler = nodeHandlers.get(nodeName);
			if (handler == null) {
				throw new XmlParseException("Unknown element <" + nodeName + "> in SQL statement.");
			}
			handler.handleNode(child, contents);
		}
	}
	return contents;
}
 
Example 2
public void print(Node node) throws XMLStreamException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        visitDocument((Document) node);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        visitDocumentFragment((DocumentFragment) node);
        break;
    case Node.ELEMENT_NODE:
        visitElement((Element) node);
        break;
    case Node.TEXT_NODE:
        visitText((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        visitCDATASection((CDATASection) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        visitReference((EntityReference) node);
        break;
    case Node.COMMENT_NODE:
        visitComment((Comment) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    default:
        throw new XMLStreamException("Unexpected DOM Node Type "
            + node.getNodeType()
        );
    }
}
 
Example 3
Source Project: hottub   File: DOM2SAX.java    License: GNU General Public License v2.0 5 votes vote down vote up
private String getNodeTypeFromCode(short code) {
    String retval = null;
    switch (code) {
    case Node.ATTRIBUTE_NODE :
        retval = "ATTRIBUTE_NODE"; break;
    case Node.CDATA_SECTION_NODE :
        retval = "CDATA_SECTION_NODE"; break;
    case Node.COMMENT_NODE :
        retval = "COMMENT_NODE"; break;
    case Node.DOCUMENT_FRAGMENT_NODE :
        retval = "DOCUMENT_FRAGMENT_NODE"; break;
    case Node.DOCUMENT_NODE :
        retval = "DOCUMENT_NODE"; break;
    case Node.DOCUMENT_TYPE_NODE :
        retval = "DOCUMENT_TYPE_NODE"; break;
    case Node.ELEMENT_NODE :
        retval = "ELEMENT_NODE"; break;
    case Node.ENTITY_NODE :
        retval = "ENTITY_NODE"; break;
    case Node.ENTITY_REFERENCE_NODE :
        retval = "ENTITY_REFERENCE_NODE"; break;
    case Node.NOTATION_NODE :
        retval = "NOTATION_NODE"; break;
    case Node.PROCESSING_INSTRUCTION_NODE :
        retval = "PROCESSING_INSTRUCTION_NODE"; break;
    case Node.TEXT_NODE:
        retval = "TEXT_NODE"; break;
    }
    return retval;
}
 
Example 4
Source Project: dacapobench   File: RuleSetFactory.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Process a rule descrtiprion node
 * @param rule the rule being constructed
 * @param descriptionNode must be a description element node
 */
private void parseDescriptionNode(Rule rule, Node descriptionNode) {
    NodeList nodeList = descriptionNode.getChildNodes();
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
            buffer.append(node.getNodeValue());
        } else if (node.getNodeType() == Node.TEXT_NODE) {
            buffer.append(node.getNodeValue());
        }
    }
    rule.setDescription(buffer.toString());
}
 
Example 5
Source Project: openjdk-8   File: TextImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Concatenates the text of all logically-adjacent text nodes to the left of
 * the node
 * @param node
 * @param buffer
 * @param parent
 * @return true - if execution was stopped because the type of node
 *         other than EntityRef, Text, CDATA is encountered, otherwise
 *         return false
 */
private boolean getWholeTextBackward(Node node, StringBuffer buffer, Node parent){

    // boolean to indicate whether node is a child of an entity reference
    boolean inEntRef = false;
    if (parent!=null) {
            inEntRef = parent.getNodeType()==Node.ENTITY_REFERENCE_NODE;
    }

    while (node != null) {
        short type = node.getNodeType();
        if (type == Node.ENTITY_REFERENCE_NODE) {
            if (getWholeTextBackward(node.getLastChild(), buffer, node)){
                return true;
            }
        }
        else if (type == Node.TEXT_NODE ||
                 type == Node.CDATA_SECTION_NODE) {
            ((TextImpl)node).insertTextContent(buffer);
        }
        else {
            return true;
        }

        node = node.getPreviousSibling();
    }

    // if the parent node is an entity reference node, must
    // check nodes to the left of the parent entity reference node for logically adjacent
    // text nodes
    if (inEntRef) {
            getWholeTextBackward(parent.getPreviousSibling(), buffer, parent.getParentNode());
        return true;
    }

    return false;
}
 
Example 6
Source Project: openjdk-8   File: DOM2TO.java    License: GNU General Public License v2.0 5 votes vote down vote up
private String getNodeTypeFromCode(short code) {
    String retval = null;
    switch (code) {
    case Node.ATTRIBUTE_NODE :
        retval = "ATTRIBUTE_NODE"; break;
    case Node.CDATA_SECTION_NODE :
        retval = "CDATA_SECTION_NODE"; break;
    case Node.COMMENT_NODE :
        retval = "COMMENT_NODE"; break;
    case Node.DOCUMENT_FRAGMENT_NODE :
        retval = "DOCUMENT_FRAGMENT_NODE"; break;
    case Node.DOCUMENT_NODE :
        retval = "DOCUMENT_NODE"; break;
    case Node.DOCUMENT_TYPE_NODE :
        retval = "DOCUMENT_TYPE_NODE"; break;
    case Node.ELEMENT_NODE :
        retval = "ELEMENT_NODE"; break;
    case Node.ENTITY_NODE :
        retval = "ENTITY_NODE"; break;
    case Node.ENTITY_REFERENCE_NODE :
        retval = "ENTITY_REFERENCE_NODE"; break;
    case Node.NOTATION_NODE :
        retval = "NOTATION_NODE"; break;
    case Node.PROCESSING_INSTRUCTION_NODE :
        retval = "PROCESSING_INSTRUCTION_NODE"; break;
    case Node.TEXT_NODE:
        retval = "TEXT_NODE"; break;
    }
    return retval;
}
 
Example 7
Source Project: hottub   File: DOMPrinter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void print(Node node) throws XMLStreamException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        visitDocument((Document) node);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        visitDocumentFragment((DocumentFragment) node);
        break;
    case Node.ELEMENT_NODE:
        visitElement((Element) node);
        break;
    case Node.TEXT_NODE:
        visitText((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        visitCDATASection((CDATASection) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        visitReference((EntityReference) node);
        break;
    case Node.COMMENT_NODE:
        visitComment((Comment) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    default:
        throw new XMLStreamException("Unexpected DOM Node Type "
            + node.getNodeType()
        );
    }
}
 
Example 8
private static boolean compareChildren(
        @NonNull NodeList children1,
        @NonNull NodeList children2) {
    // because this represents a resource values, we're going to be very strict about this
    // comparison.
    if (children1.getLength() != children2.getLength()) {
        return false;
    }

    for (int i = 0, n = children1.getLength(); i < n; i++) {
        Node child1 = children1.item(i);
        Node child2 = children2.item(i);

        short nodeType = child1.getNodeType();
        if (nodeType != child2.getNodeType()) {
            return false;
        }

        switch (nodeType) {
            case Node.ELEMENT_NODE:
                if (!compareElementNode(child1, child2, true)) {
                    return false;
                }
                break;
            case Node.CDATA_SECTION_NODE:
            case Node.TEXT_NODE:
            case Node.COMMENT_NODE:
                if (!child1.getNodeValue().equals(child2.getNodeValue())) {
                    return false;
                }
                break;
        }
    }

    return true;
}
 
Example 9
void checkIndex(Node refNode, int offset) throws DOMException
{
    if (offset < 0) {
        throw new DOMException(
            DOMException.INDEX_SIZE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null));
    }

    int type = refNode.getNodeType();

    // If the node contains text, ensure that the
    // offset of the range is <= to the length of the text
    if (type == Node.TEXT_NODE
        || type == Node.CDATA_SECTION_NODE
        || type == Node.COMMENT_NODE
        || type == Node.PROCESSING_INSTRUCTION_NODE) {
        if (offset > refNode.getNodeValue().length()) {
            throw new DOMException(DOMException.INDEX_SIZE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null));
        }
    }
    else {
        // Since the node is not text, ensure that the offset
        // is valid with respect to the number of child nodes
        if (offset > refNode.getChildNodes().getLength()) {
            throw new DOMException(DOMException.INDEX_SIZE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null));
        }
    }
}
 
Example 10
Source Project: sakai   File: XmlUtil.java    License: Educational Community License v2.0 4 votes vote down vote up
/**
 * Get a textual representation of a Node.
 * @param node The Node
 * @return the document in a text string
 */
public static String getDOMString(Node node)
{
  //String domString = "";
  
  StringBuilder domStringbuf = new StringBuilder();
  
  int type = node.getNodeType();
  switch (type)
  {
    // print the document element
    case Node.DOCUMENT_NODE:
    {
      domStringbuf.append("<?xml version=\"1.0\" ?>\n");
      domStringbuf.append(getDOMString(((Document)node).getDocumentElement()));
      break;
    }

    // print element with attributes
    case Node.ELEMENT_NODE:
    {
  	domStringbuf.append("<");
  	domStringbuf.append(node.getNodeName());
      NamedNodeMap attrs = node.getAttributes();
      
      
      
      for (int i = 0; i < attrs.getLength(); i++)
      {
        Node attr = attrs.item(i);
        //domString += (" " + attr.getNodeName().trim() +
        //              "=\"" + attr.getNodeValue().trim() +
        //              "\"");
        
        domStringbuf.append((" " + attr.getNodeName().trim() +
                      "=\"" + attr.getNodeValue().trim() +
                      "\""));
      }
      //domString = domStringbuf.toString();
      domStringbuf.append(">");

      NodeList children = node.getChildNodes();
      if (children != null)
      {
        int len = children.getLength();
        for (int i = 0; i < len; i++)
      	  domStringbuf.append(getDOMString(children.item(i)));
      }
      domStringbuf.append("</");
      domStringbuf.append(node.getNodeName());
      domStringbuf.append(">\n");

      break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE:
    {
  	  domStringbuf.append("&");
  	  domStringbuf.append(node.getNodeName().trim());
  	  domStringbuf.append(";");

      break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE:
    {
  	  domStringbuf.append("");
      break;
    }

    // print text
    case Node.TEXT_NODE:
    {
      String val = node.getNodeValue();
      if (val==null) val = "";
      domStringbuf.append(val);//rshastri .trim() removed SAK-1671 
      break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE:
    {
  	  domStringbuf.append("");
      break;
    }
  }

  if (type == Node.ELEMENT_NODE) {
  	domStringbuf.append("\n");
  }

  String domString = domStringbuf.toString();
  return domString;
}
 
Example 11
public String toString(){
    if( fDetach) {
            throw new DOMException(
            DOMException.INVALID_STATE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
    }

    Node node = fStartContainer;
    Node stopNode = fEndContainer;
    StringBuffer sb = new StringBuffer();
    if (fStartContainer.getNodeType() == Node.TEXT_NODE
     || fStartContainer.getNodeType() == Node.CDATA_SECTION_NODE
    ) {
        if (fStartContainer == fEndContainer) {
            sb.append(fStartContainer.getNodeValue().substring(fStartOffset, fEndOffset));
            return sb.toString();
        }
        sb.append(fStartContainer.getNodeValue().substring(fStartOffset));
        node=nextNode (node,true); //fEndContainer!=fStartContainer

    }
    else {  //fStartContainer is not a TextNode
        node=node.getFirstChild();
        if (fStartOffset>0) { //find a first node within a range, specified by fStartOffset
           int counter=0;
           while (counter<fStartOffset && node!=null) {
               node=node.getNextSibling();
               counter++;
           }
        }
        if (node == null) {
               node = nextNode(fStartContainer,false);
        }
    }
    if ( fEndContainer.getNodeType()!= Node.TEXT_NODE &&
         fEndContainer.getNodeType()!= Node.CDATA_SECTION_NODE ){
         int i=fEndOffset;
         stopNode = fEndContainer.getFirstChild();
         while( i>0 && stopNode!=null ){
             --i;
             stopNode = stopNode.getNextSibling();
         }
         if ( stopNode == null )
             stopNode = nextNode( fEndContainer, false );
     }
     while (node != stopNode) {  //look into all kids of the Range
         if (node == null) break;
         if (node.getNodeType() == Node.TEXT_NODE
         ||  node.getNodeType() == Node.CDATA_SECTION_NODE) {
             sb.append(node.getNodeValue());
         }

         node = nextNode(node, true);
     }

    if (fEndContainer.getNodeType() == Node.TEXT_NODE
     || fEndContainer.getNodeType() == Node.CDATA_SECTION_NODE) {
        sb.append(fEndContainer.getNodeValue().substring(0,fEndOffset));
    }
    return sb.toString();
}
 
Example 12
private boolean processNode( final Node node,
                             final LinkedHashMap<String, String> results,
                             final TypedTableModel typedTableModel ) throws ReportDataFactoryException {
  final LinkedHashMap<String, String> innerResults = new LinkedHashMap<String, String>( results );

  boolean isLeaf = true;
  //    System.out.println("<" + node.getQName() + ">");
  final NodeList childList = node.getChildNodes();
  for ( int i = 0; i < childList.getLength(); i++ ) {
    final Node nodeIf = childList.item( i );
    final short type = nodeIf.getNodeType();
    if ( type == Node.COMMENT_NODE ) {
      continue;
    }

    if ( type == Node.ELEMENT_NODE ) {
      final NodeList anIf = nodeIf.getChildNodes();
      final int size = anIf.getLength();
      // check if either a empty node or a
      if ( size == 0 ) {
        // a empty node ...
        innerResults.put( nodeIf.getNodeName(), null );
      } else if ( size == 1 ) {
        final Node subNode = anIf.item( 0 );
        if ( subNode.getNodeType() == Node.TEXT_NODE || subNode.getNodeType() == Node.CDATA_SECTION_NODE ) {
          // a single text node ..
          innerResults.put( nodeIf.getNodeName(), nodeIf.getTextContent() );
        } else if ( subNode.getNodeType() == Node.ELEMENT_NODE ) {
          isLeaf = false;
        } else {
          innerResults.put( nodeIf.getNodeName(), nodeIf.getTextContent() );
        }
      } else {
        isLeaf = false;
      }
    } else {
      final String content = nodeIf.getTextContent();
      if ( StringUtils.isEmpty( content, true ) == false ) {
        innerResults.put( nodeIf.getNodeName(), content );
      }
    }
  }

  if ( isLeaf == false ) {
    for ( int i = 0; i < childList.getLength(); i++ ) {
      final Node deepNode = childList.item( i );
      if ( deepNode.getNodeType() == Node.ELEMENT_NODE ) {
        final NodeList childNodes = deepNode.getChildNodes();
        if ( childNodes.getLength() > 1 ||
          ( childNodes.getLength() == 1 &&
            childNodes.item( 0 ).getNodeType() == Node.ELEMENT_NODE ) ) {
          if ( processNode( deepNode, innerResults, typedTableModel ) == false ) {
            return false;
          }
        }
      }
    }
    return true;
  } else {
    return addRow( innerResults, typedTableModel );
  }
}
 
Example 13
/**
 * If any EntityReference to be removed has descendants that are not
 * EntityReference, Text, or CDATASection nodes, the replaceWholeText method
 * must fail before performing any modification of the document, raising a
 * DOMException with the code NO_MODIFICATION_ALLOWED_ERR. Traverse previous
 * siblings of the node to be replaced. If a previous sibling is an
 * EntityReference node, get it's last child. If the first child was a Text
 * or CDATASection node and its next siblings are neither a replaceable
 * EntityReference or Text or CDATASection nodes, return false. IF the first
 * child was neither Text nor CDATASection nor a replaceable EntityReference
 * Node, then return true. If the first child was a Text or CDATASection
 * node any its next sibling was not or was an EntityReference that did not
 * contain only Text or CDATASection nodes, return false. Check this
 * recursively for EntityReference nodes.
 *
 * @param node
 * @return true - can replace text false - can't replace exception must be
 *         raised
 */
private boolean canModifyNext(Node node) {
    boolean textFirstChild = false;

    Node next = node.getNextSibling();
    while (next != null) {

        short type = next.getNodeType();

        if (type == Node.ENTITY_REFERENCE_NODE) {
            //If the previous sibling was entityreference
            //check if its content is replaceable
            Node firstChild = next.getFirstChild();

            //if the entity reference has no children
            //return false
            if (firstChild == null) {
                return false;
            }

            //The replacement text of the entity reference should
            //be either only text,cadatsections or replaceable entity
            //reference nodes or the last child should be neither of these
            while (firstChild != null) {
                short lType = firstChild.getNodeType();

                if (lType == Node.TEXT_NODE
                        || lType == Node.CDATA_SECTION_NODE) {
                    textFirstChild = true;
                } else if (lType == Node.ENTITY_REFERENCE_NODE) {
                    if (!canModifyNext(firstChild)) {
                        return false;
                    } else {
                        //If the EntityReference child contains
                        //only text, or non-text or ends with a
                        //non-text node.
                        textFirstChild = true;
                    }
                } else {
                    //If the first child was replaceable text and next
                    //children are not, then return false
                    if (textFirstChild) {
                        return false;
                    } else {
                        return true;
                    }
                }
                firstChild = firstChild.getNextSibling();
            }
        } else if (type == Node.TEXT_NODE
                || type == Node.CDATA_SECTION_NODE) {
            //If the previous sibling was text or cdatasection move to next
        } else {
            //If the next sibling was anything but text or
            //cdatasection or an entity reference, stop search and
            //return true
            return true;
        }

        next = next.getNextSibling();
    }

    return true;
}
 
Example 14
/**
 * End processing of given node
 *
 *
 * @param node Node we just finished processing
 *
 * @throws org.xml.sax.SAXException
 */
protected void endNode(Node node) throws org.xml.sax.SAXException
{

  switch (node.getNodeType())
  {
  case Node.DOCUMENT_NODE :
    break;

  case Node.ELEMENT_NODE :
    String ns = m_dh.getNamespaceOfNode(node);
    if(null == ns)
      ns = "";
    this.m_contentHandler.endElement(ns,
                                       m_dh.getLocalNameOfNode(node),
                                       node.getNodeName());

    NamedNodeMap atts = ((Element) node).getAttributes();
    int nAttrs = atts.getLength();

    for (int i = 0; i < nAttrs; i++)
    {
      Node attr = atts.item(i);
      String attrName = attr.getNodeName();

      if (attrName.equals("xmlns") || attrName.startsWith("xmlns:"))
      {
        int index;
        // Use "" instead of null, as Xerces likes "" for the
        // name of the default namespace.  Fix attributed
        // to "Steven Murray" <[email protected]>.
        String prefix = (index = attrName.indexOf(":")) < 0
                        ? "" : attrName.substring(index + 1);

        this.m_contentHandler.endPrefixMapping(prefix);
      }
    }
    break;
  case Node.CDATA_SECTION_NODE :
    break;
  case Node.ENTITY_REFERENCE_NODE :
  {
    EntityReference eref = (EntityReference) node;

    if (m_contentHandler instanceof LexicalHandler)
    {
      LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);

      lh.endEntity(eref.getNodeName());
    }
  }
  break;
  default :
  }
}
 
Example 15
/** Do processing for the start of a node. */
private void beginNode(Node node) {
    switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            fCurrentElement = node;
            // push namespace context
            fNamespaceContext.pushContext();
            // start element
            fillQName(fElementQName, node);
            processAttributes(node.getAttributes());
            fSchemaValidator.startElement(fElementQName, fAttributes, null);
            break;
        case Node.TEXT_NODE:
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.setIgnoringCharacters(true);
                sendCharactersToValidator(node.getNodeValue());
                fDOMValidatorHandler.setIgnoringCharacters(false);
                fDOMValidatorHandler.characters((Text) node);
            }
            else {
                sendCharactersToValidator(node.getNodeValue());
            }
            break;
        case Node.CDATA_SECTION_NODE:
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.setIgnoringCharacters(true);
                fSchemaValidator.startCDATA(null);
                sendCharactersToValidator(node.getNodeValue());
                fSchemaValidator.endCDATA(null);
                fDOMValidatorHandler.setIgnoringCharacters(false);
                fDOMValidatorHandler.cdata((CDATASection) node);
            }
            else {
                fSchemaValidator.startCDATA(null);
                sendCharactersToValidator(node.getNodeValue());
                fSchemaValidator.endCDATA(null);
            }
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            /**
             * The validator does nothing with processing instructions so bypass it.
             * Send the ProcessingInstruction node directly to the result builder.
             */
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.processingInstruction((ProcessingInstruction) node);
            }
            break;
        case Node.COMMENT_NODE:
            /**
             * The validator does nothing with comments so bypass it.
             * Send the Comment node directly to the result builder.
             */
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.comment((Comment) node);
            }
            break;
        case Node.DOCUMENT_TYPE_NODE:
            /**
             * Send the DocumentType node directly to the result builder.
             */
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.doctypeDecl((DocumentType) node);
            }
            break;
        default: // Ignore other node types.
            break;
    }
}
 
Example 16
/**
 * If any EntityReference to be removed has descendants that are not
 * EntityReference, Text, or CDATASection nodes, the replaceWholeText method
 * must fail before performing any modification of the document, raising a
 * DOMException with the code NO_MODIFICATION_ALLOWED_ERR. Traverse previous
 * siblings of the node to be replaced. If a previous sibling is an
 * EntityReference node, get it's last child. If the first child was a Text
 * or CDATASection node and its next siblings are neither a replaceable
 * EntityReference or Text or CDATASection nodes, return false. IF the first
 * child was neither Text nor CDATASection nor a replaceable EntityReference
 * Node, then return true. If the first child was a Text or CDATASection
 * node any its next sibling was not or was an EntityReference that did not
 * contain only Text or CDATASection nodes, return false. Check this
 * recursively for EntityReference nodes.
 *
 * @param node
 * @return true - can replace text false - can't replace exception must be
 *         raised
 */
private boolean canModifyNext(Node node) {
    boolean textFirstChild = false;

    Node next = node.getNextSibling();
    while (next != null) {

        short type = next.getNodeType();

        if (type == Node.ENTITY_REFERENCE_NODE) {
            //If the previous sibling was entityreference
            //check if its content is replaceable
            Node firstChild = next.getFirstChild();

            //if the entity reference has no children
            //return false
            if (firstChild == null) {
                return false;
            }

            //The replacement text of the entity reference should
            //be either only text,cadatsections or replaceable entity
            //reference nodes or the last child should be neither of these
            while (firstChild != null) {
                short lType = firstChild.getNodeType();

                if (lType == Node.TEXT_NODE
                        || lType == Node.CDATA_SECTION_NODE) {
                    textFirstChild = true;
                } else if (lType == Node.ENTITY_REFERENCE_NODE) {
                    if (!canModifyNext(firstChild)) {
                        return false;
                    } else {
                        //If the EntityReference child contains
                        //only text, or non-text or ends with a
                        //non-text node.
                        textFirstChild = true;
                    }
                } else {
                    //If the first child was replaceable text and next
                    //children are not, then return false
                    if (textFirstChild) {
                        return false;
                    } else {
                        return true;
                    }
                }
                firstChild = firstChild.getNextSibling();
            }
        } else if (type == Node.TEXT_NODE
                || type == Node.CDATA_SECTION_NODE) {
            //If the previous sibling was text or cdatasection move to next
        } else {
            //If the next sibling was anything but text or
            //cdatasection or an entity reference, stop search and
            //return true
            return true;
        }

        next = next.getNextSibling();
    }

    return true;
}
 
Example 17
Source Project: lucene-solr   File: DOMUtil.java    License: Apache License 2.0 4 votes vote down vote up
/** @see #getText(Node) */
private static void getText(Node nd, StringBuilder buf) {

  short type = nd.getNodeType();

  switch (type) {

  case Node.ELEMENT_NODE: /* fall through */
  case Node.ENTITY_NODE: /* fall through */
  case Node.ENTITY_REFERENCE_NODE: /* fall through */
  case Node.DOCUMENT_FRAGMENT_NODE:
    NodeList childs = nd.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
      Node child = childs.item(i);
      short childType = child.getNodeType();
      if (childType != Node.COMMENT_NODE &&
          childType != Node.PROCESSING_INSTRUCTION_NODE) {
        getText(child, buf);
      }
    }
    break;

  case Node.ATTRIBUTE_NODE: /* fall through */
    /* Putting Attribute nodes in this section does not exactly
       match the definition of how textContent should behave
       according to the DOM Level-3 Core documentation - which
       specifies that the Attr's children should have their
       textContent concated (Attr's can have a single child which
       is either Text node or an EntityReference).  In practice,
       DOM implementations do not seem to use child nodes of
       Attributes, storing the "text" directly as the nodeValue.
       Fortunately, the DOM Spec indicates that when Attr.nodeValue
       is read, it should return the nodeValue from the child Node,
       so this approach should work both for strict implementations,
       and implementations actually encountered.
    */
  case Node.TEXT_NODE: /* fall through */
  case Node.CDATA_SECTION_NODE: /* fall through */
  case Node.COMMENT_NODE: /* fall through */
  case Node.PROCESSING_INSTRUCTION_NODE: /* fall through */
    buf.append(nd.getNodeValue());
    break;

  case Node.DOCUMENT_NODE: /* fall through */
  case Node.DOCUMENT_TYPE_NODE: /* fall through */
  case Node.NOTATION_NODE: /* fall through */
  default:
    /* :NOOP: */

  }
}
 
Example 18
@NonNull
private static String getMarkupText(@NonNull NodeList children) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0, n = children.getLength(); i < n; i++) {
        Node child = children.item(i);

        short nodeType = child.getNodeType();

        switch (nodeType) {
            case Node.ELEMENT_NODE: {
                Element element = (Element) child;
                String tagName = element.getTagName();
                sb.append('<');
                sb.append(tagName);

                NamedNodeMap attributes = element.getAttributes();
                int attributeCount = attributes.getLength();
                if (attributeCount > 0) {
                    for (int j = 0; j < attributeCount; j++) {
                        Node attribute = attributes.item(j);
                        sb.append(' ');
                        sb.append(attribute.getNodeName());
                        sb.append('=').append('"');
                        XmlUtils.appendXmlAttributeValue(sb, attribute.getNodeValue());
                        sb.append('"');
                    }
                }
                sb.append('>');

                NodeList childNodes = child.getChildNodes();
                if (childNodes.getLength() > 0) {
                    sb.append(getMarkupText(childNodes));
                }

                sb.append('<');
                sb.append('/');
                sb.append(tagName);
                sb.append('>');

                break;
            }
            case Node.TEXT_NODE:
                sb.append(child.getNodeValue());
                break;
            case Node.CDATA_SECTION_NODE:
                sb.append(child.getNodeValue());
                break;
        }
    }

    return sb.toString();
}
 
Example 19
/** Do processing for the start of a node. */
private void beginNode(Node node) {
    switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            fCurrentElement = node;
            // push namespace context
            fNamespaceContext.pushContext();
            // start element
            fillQName(fElementQName, node);
            processAttributes(node.getAttributes());
            fSchemaValidator.startElement(fElementQName, fAttributes, null);
            break;
        case Node.TEXT_NODE:
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.setIgnoringCharacters(true);
                sendCharactersToValidator(node.getNodeValue());
                fDOMValidatorHandler.setIgnoringCharacters(false);
                fDOMValidatorHandler.characters((Text) node);
            }
            else {
                sendCharactersToValidator(node.getNodeValue());
            }
            break;
        case Node.CDATA_SECTION_NODE:
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.setIgnoringCharacters(true);
                fSchemaValidator.startCDATA(null);
                sendCharactersToValidator(node.getNodeValue());
                fSchemaValidator.endCDATA(null);
                fDOMValidatorHandler.setIgnoringCharacters(false);
                fDOMValidatorHandler.cdata((CDATASection) node);
            }
            else {
                fSchemaValidator.startCDATA(null);
                sendCharactersToValidator(node.getNodeValue());
                fSchemaValidator.endCDATA(null);
            }
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            /**
             * The validator does nothing with processing instructions so bypass it.
             * Send the ProcessingInstruction node directly to the result builder.
             */
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.processingInstruction((ProcessingInstruction) node);
            }
            break;
        case Node.COMMENT_NODE:
            /**
             * The validator does nothing with comments so bypass it.
             * Send the Comment node directly to the result builder.
             */
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.comment((Comment) node);
            }
            break;
        case Node.DOCUMENT_TYPE_NODE:
            /**
             * Send the DocumentType node directly to the result builder.
             */
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.doctypeDecl((DocumentType) node);
            }
            break;
        default: // Ignore other node types.
            break;
    }
}
 
Example 20
Source Project: xmlunit   File: XPathContextTest.java    License: Apache License 2.0 votes vote down vote up
public short getType() { return Node.CDATA_SECTION_NODE; }