Package org.apache.uima.util.XMLSerializer

Examples of org.apache.uima.util.XMLSerializer.CharacterValidatingContentHandler


      }
    }

    // start element
    outputStartElement(aContentHandler, infoset, inf.namespace, inf.elementTagName, inf.elementTagName, attrs);   
    CharacterValidatingContentHandler cc = (CharacterValidatingContentHandler) aContentHandler;
    cc.setLastOutputNode(infoset);
    cc.lastOutputNodeAddLevel();
    // write child elements
    try {
      for (int i = 0; i < inf.propertyInfo.length; i++) {
        PropertyXmlInfo propInf = inf.propertyInfo[i];
        writePropertyAsElement(propInf, inf.namespace, aContentHandler);
      }
    } finally {
      cc.lastOutputNodeClearLevel();
    }
    
    // end element
    outputEndElement(aContentHandler, infoset, inf.namespace, inf.elementTagName, inf.elementTagName);
  }
View Full Code Here


            && (val == null || (val.getClass().isArray() && ((Object[]) val).length == 0)))
      return;

    // if XML element name was supplied, write a tag
    String elementName = aPropInfo.xmlElementName;     
    CharacterValidatingContentHandler cc = (CharacterValidatingContentHandler) aContentHandler;
    Node elementNode = null;

    if (null != elementName) { // can be null in this case:
        //               <fixedFlow>       <== after outputting this,
        //                                     there is no <array> conntaining:
        //                  <node>A</node> 
        //                  <node>B</node>
 
      elementNode = findMatchingSubElement(aContentHandler, aPropInfo.xmlElementName);
      outputStartElement(aContentHandler, elementNode, aNamespace, aPropInfo.xmlElementName, aPropInfo.xmlElementName,
                EMPTY_ATTRIBUTES);
      cc.lastOutputNodeAddLevel();
    }
    // get class of property
    Class propClass = getAttributeClass(aPropInfo.propertyName);

    try {
    // if value is null then write nothing
    if (val != null) {
      // if value is an array then we have to treat that specially
      if (val.getClass().isArray()) {
        writeArrayPropertyAsElement(aPropInfo.propertyName, propClass, val,
                aPropInfo.arrayElementTagName, aNamespace, aContentHandler);
      } else {
        // if value is an XMLizable object, call its toXML method
        if (val instanceof XMLizable) {
          ((XMLizable) val).toXML(aContentHandler);
        }
        // else, if property's class is java.lang.Object, attempt to write
        // it as a primitive
        else if (propClass == Object.class) {
          writePrimitiveValue(val, aContentHandler);
        } else {
          // assume attribute's class is known (e.g. String, Integer), so it
          // is not necessary to write the class name to the XML. Just write
          // the string representation of the object
          // XMLUtils.writeNormalizedString(val.toString(), aWriter, true);
          String valStr = val.toString();
          aContentHandler.characters(valStr.toCharArray(), 0, valStr.length());
        }
      }
    }
    } finally {
      if (null != elementName) {
        cc.lastOutputNodeClearLevel();
      }
    }

    // if XML element name was supplied, end the element that we started
    if (null != elementName) {
View Full Code Here

   *          representation
   */
  protected void writeArrayPropertyAsElement(String aPropName, Class aPropClass, Object aValue,
          String aArrayElementTagName, String aNamespace, ContentHandler aContentHandler)
          throws SAXException {
    CharacterValidatingContentHandler cc = (CharacterValidatingContentHandler) aContentHandler;

    // if aPropClass is generic Object, reader won't know whether to expect
    // an array, so we tell it be writing an "array" element here.
    Node arraySubElement = findMatchingSubElement(aContentHandler, "array");
   
    if (aPropClass == Object.class) {  // skip writting <array> unless the property class (of objects in the array) is "Object"
                                       // skipped e.g. in <fixedFlow> values, where aPropClass is String
      outputStartElement(aContentHandler, arraySubElement, aNamespace, "array", "array", EMPTY_ATTRIBUTES);
      cc.lastOutputNodeAddLevel();
    }

    // iterate through elements of the array (at this point we don't allow
    // nested arrays here
    int len = ((Object[]) aValue).length;
    try {
      for (int i = 0; i < len; i++) {
        Object curElem = Array.get(aValue, i);
        Node matchingArrayElement = findMatchingSubElement(aContentHandler, aArrayElementTagName);
       
        // if a particular array element tag has been specified, write it
        outputStartElement(aContentHandler, matchingArrayElement, aNamespace, aArrayElementTagName, aArrayElementTagName,
                  EMPTY_ATTRIBUTES);
 
        // if attribute's value is an XMLizable object, call its toXML method
        if (curElem instanceof XMLizable) {
          ((XMLizable) curElem).toXML(aContentHandler);
        }
        // else, attempt to write it as a primitive
        else {
          if (aArrayElementTagName == null) {
            // need to include the type, e.g. <string>
            writePrimitiveValue(curElem, aContentHandler);
          } else {
            // don't include the type - just write the value
            String valStr = curElem.toString();
            aContentHandler.characters(valStr.toCharArray(), 0, valStr.length());
          }
        }
 
        // if we started an element, end it
        outputEndElement(aContentHandler, matchingArrayElement, aNamespace, aArrayElementTagName, aArrayElementTagName);
      }
    } finally {
      if (aPropClass == Object.class) {
        cc.lastOutputNodeClearLevel();
      }
    }

    // if we started an "Array" element, end it
    if (aPropClass == Object.class) {
View Full Code Here

      }
    } else {
      // write start tag for attribute if desired
      outputStartElement(aContentHandler, matchingNode, aNamespace, aXmlElementName, aXmlElementName, EMPTY_ATTRIBUTES);       

      CharacterValidatingContentHandler cc = (CharacterValidatingContentHandler) aContentHandler;
      cc.lastOutputNodeAddLevel();
      try {
        // iterate over entries in the Map
        for (Map.Entry<String, Object> curEntry : theMap.entrySet()) {
          String key = curEntry.getKey();
 
          // write a tag for the value, with a "key" attribute
          AttributesImpl attrs = new AttributesImpl();
          attrs.addAttribute("", aKeyXmlAttribute, aKeyXmlAttribute, null, key); // are these nulls OK?
          Node innerMatchingNode = findMatchingSubElement(aContentHandler, aValueTagName);
          outputStartElement(aContentHandler, innerMatchingNode, aNamespace, aValueTagName, aValueTagName, attrs);     
          // write the value (must be XMLizable or an array of XMLizable)
          Object val = curEntry.getValue();
          if (val.getClass().isArray()) {
            Object[] arr = (Object[]) val;
            for (int j = 0; j < arr.length; j++) {
              XMLizable elem = (XMLizable) arr[j];
              elem.toXML(aContentHandler);
            }
          } else {
            cc.lastOutputNodeAddLevel();
            try {
            ((XMLizable) val).toXML(aContentHandler);
            } finally {
              cc.lastOutputNodeClearLevel();             
            }
          }
 
          // write end tag for the value
          outputEndElement(aContentHandler, innerMatchingNode, aNamespace, aValueTagName, aValueTagName);
        }
      } finally {
        cc.lastOutputNodeClearLevel();
      }

      // if we wrote start tag for attribute, now write end tag
      outputEndElement(aContentHandler, matchingNode, aNamespace, aXmlElementName, aXmlElementName);
    }
View Full Code Here

      indent = ((CharacterValidatingContentHandler) contentHandler).getIndent();;
    }
  
    if (null == node) {
      if (contentHandler instanceof CharacterValidatingContentHandler) {
        CharacterValidatingContentHandler cvch = (CharacterValidatingContentHandler)contentHandler;
        if (!cvch.prevNL) {
          outputNL(contentHandler);
          outputIndent(indent, contentHandler);
        }
      }
View Full Code Here

      indent = ((CharacterValidatingContentHandler) contentHandler).prevIndent();
    }
   
    if (null == node || (!hasElementChildNode(node))) {
      if (null == node && contentHandler instanceof CharacterValidatingContentHandler) {
        CharacterValidatingContentHandler cvch = (CharacterValidatingContentHandler)contentHandler;
        if (cvch.prevWasEndElement) {
          outputNL(contentHandler);
          outputIndent(indent, contentHandler);
        }
      }
View Full Code Here

   */
  protected Node findMatchingSubElement(ContentHandler contentHandler, String elementName) {
    if (null == infoset || null == elementName) {
      return null;
    }
    CharacterValidatingContentHandler c = (CharacterValidatingContentHandler) contentHandler;
    Node lastOutput = c.getLastOutputNode();
    Node n = null;
   
    if (lastOutput == null) {
      lastOutput = c.getLastOutputNodePrevLevel();
      if (lastOutput == null) {
        return null;
      }
      n = lastOutput.getFirstChild();
    } else {
      n = lastOutput.getNextSibling();
    }
    for (; n != null; n = n.getNextSibling()) {
      if ((n instanceof Element) &&
          elementName.equals(((Element)n).getTagName())) {
        c.setLastOutputNode(n);
        return n;
      }
    }
    return null;
  }
View Full Code Here

      }
    }

    // start element
    outputStartElement(aContentHandler, infoset, inf.namespace, inf.elementTagName, inf.elementTagName, attrs);
    CharacterValidatingContentHandler cc = maybeGetCharacterValidatingContentHandler(aContentHandler);
    // https://issues.apache.org/jira/browse/UIMA-3477
    if (cc != null) {
      cc.setLastOutputNode(infoset);
      cc.lastOutputNodeAddLevel();
    }
    // write child elements
    try {
      for (int i = 0; i < inf.propertyInfo.length; i++) {
        PropertyXmlInfo propInf = inf.propertyInfo[i];
        writePropertyAsElement(propInf, inf.namespace, aContentHandler);
      }
    } finally {
      if (cc != null) {
        cc.lastOutputNodeClearLevel();
      }
    }
    
    // end element
    outputEndElement(aContentHandler, infoset, inf.namespace, inf.elementTagName, inf.elementTagName);
View Full Code Here

            && (val == null || (val.getClass().isArray() && ((Object[]) val).length == 0)))
      return;

    // if XML element name was supplied, write a tag
    String elementName = aPropInfo.xmlElementName;
    CharacterValidatingContentHandler cc = maybeGetCharacterValidatingContentHandler(aContentHandler);
    Node elementNode = null;

    if (null != elementName) { // can be null in this case:
        //               <fixedFlow>       <== after outputting this,
        //                                     there is no <array> conntaining:
        //                  <node>A</node> 
        //                  <node>B</node>
 
      elementNode = findMatchingSubElement(aContentHandler, aPropInfo.xmlElementName);
      outputStartElement(aContentHandler, elementNode, aNamespace, aPropInfo.xmlElementName, aPropInfo.xmlElementName,
                EMPTY_ATTRIBUTES);
      if (cc != null) {
        cc.lastOutputNodeAddLevel();
      }
    }
    // get class of property
    Class propClass = getAttributeClass(aPropInfo.propertyName);

    try {
    // if value is null then write nothing
    if (val != null) {
      // if value is an array then we have to treat that specially
      if (val.getClass().isArray()) {
        writeArrayPropertyAsElement(aPropInfo.propertyName, propClass, val,
                aPropInfo.arrayElementTagName, aNamespace, aContentHandler);
      } else {
        // if value is an XMLizable object, call its toXML method
        if (val instanceof XMLizable) {
          ((XMLizable) val).toXML(aContentHandler);
        }
        // else, if property's class is java.lang.Object, attempt to write
        // it as a primitive
        else if (propClass == Object.class) {
          writePrimitiveValue(val, aContentHandler);
        } else {
          // assume attribute's class is known (e.g. String, Integer), so it
          // is not necessary to write the class name to the XML. Just write
          // the string representation of the object
          // XMLUtils.writeNormalizedString(val.toString(), aWriter, true);
          String valStr = val.toString();
          aContentHandler.characters(valStr.toCharArray(), 0, valStr.length());
        }
      }
    }
    } finally {
      if (null != elementName) {
        if (cc != null) {
          cc.lastOutputNodeClearLevel();
        }
      }
    }

    // if XML element name was supplied, end the element that we started
View Full Code Here

   *          representation
   */
  protected void writeArrayPropertyAsElement(String aPropName, Class aPropClass, Object aValue,
          String aArrayElementTagName, String aNamespace, ContentHandler aContentHandler)
          throws SAXException {
    CharacterValidatingContentHandler cc = maybeGetCharacterValidatingContentHandler(aContentHandler);

    // if aPropClass is generic Object, reader won't know whether to expect
    // an array, so we tell it be writing an "array" element here.
    Node arraySubElement = findMatchingSubElement(aContentHandler, "array");
   
    if (aPropClass == Object.class) {  // skip writting <array> unless the property class (of objects in the array) is "Object"
                                       // skipped e.g. in <fixedFlow> values, where aPropClass is String
      outputStartElement(aContentHandler, arraySubElement, aNamespace, "array", "array", EMPTY_ATTRIBUTES);
      if (null != cc) {
        cc.lastOutputNodeAddLevel();
      }
    }

    // iterate through elements of the array (at this point we don't allow
    // nested arrays here
    int len = ((Object[]) aValue).length;
    try {
      for (int i = 0; i < len; i++) {
        Object curElem = Array.get(aValue, i);
        Node matchingArrayElement = findMatchingSubElement(aContentHandler, aArrayElementTagName);
       
        // if a particular array element tag has been specified, write it
        outputStartElement(aContentHandler, matchingArrayElement, aNamespace, aArrayElementTagName, aArrayElementTagName,
                  EMPTY_ATTRIBUTES);
 
        // if attribute's value is an XMLizable object, call its toXML method
        if (curElem instanceof XMLizable) {
          ((XMLizable) curElem).toXML(aContentHandler);
        }
        // else, attempt to write it as a primitive
        else {
          if (aArrayElementTagName == null) {
            // need to include the type, e.g. <string>
            writePrimitiveValue(curElem, aContentHandler);
          } else {
            // don't include the type - just write the value
            String valStr = curElem.toString();
            aContentHandler.characters(valStr.toCharArray(), 0, valStr.length());
          }
        }
 
        // if we started an element, end it
        outputEndElement(aContentHandler, matchingArrayElement, aNamespace, aArrayElementTagName, aArrayElementTagName);
      }
    } finally {
      if (aPropClass == Object.class) {
        if (null != cc) {
          cc.lastOutputNodeClearLevel();
        }
      }
    }

    // if we started an "Array" element, end it
View Full Code Here

TOP

Related Classes of org.apache.uima.util.XMLSerializer.CharacterValidatingContentHandler

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.