Java Code Examples for org.apache.xml.serializer.SerializationHandler#flushPending()

The following examples show how to use org.apache.xml.serializer.SerializationHandler#flushPending() . 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 check out the related API usage on the sidebar.
Example 1
Source File: SerializerUtils.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Given a result tree fragment, walk the tree and
 * output it to the SerializationHandler.
 *
 * @param obj Result tree fragment object
 * @param support XPath context for the result tree fragment
 *
 * @throws org.xml.sax.SAXException
 */
public static void outputResultTreeFragment(
    SerializationHandler handler,
    XObject obj,
    XPathContext support)
    throws org.xml.sax.SAXException
{

    int doc = obj.rtf();
    DTM dtm = support.getDTM(doc);

    if (null != dtm)
    {
        for (int n = dtm.getFirstChild(doc);
            DTM.NULL != n;
            n = dtm.getNextSibling(n))
        {
            handler.flushPending();

            // I think. . . . This used to have a (true) arg
            // to flush prefixes, will that cause problems ???
            if (dtm.getNodeType(n) == DTM.ELEMENT_NODE
                    && dtm.getNamespaceURI(n) == null)
                handler.startPrefixMapping("", "");
            dtm.dispatchToEvents(n, handler);
        }
    }
}
 
Example 2
Source File: TransformerImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
    * Execute each of the children of a template element.
    *
    * @param elem The ElemTemplateElement that contains the children
    * that should execute.
    * @param handler The ContentHandler to where the result events
    * should be fed.
    *
    * @throws TransformerException
    * @xsl.usage advanced
    */
   public void executeChildTemplates(
           ElemTemplateElement elem, ContentHandler handler)
             throws TransformerException
   {

     SerializationHandler xoh = this.getSerializationHandler();

     // These may well not be the same!  In this case when calling
     // the Redirect extension, it has already set the ContentHandler
     // in the Transformer.
     SerializationHandler savedHandler = xoh;

     try
     {
       xoh.flushPending();

       // %REVIEW% Make sure current node is being pushed.
       LexicalHandler lex = null;
       if (handler instanceof LexicalHandler) {
          lex = (LexicalHandler) handler;
       }
       m_serializationHandler = new ToXMLSAXHandler(handler, lex, savedHandler.getEncoding());
       m_serializationHandler.setTransformer(this);
       executeChildTemplates(elem, true);
     }
     catch (TransformerException e)
     {
       throw e;
     }
     catch (SAXException se) {
     	 throw new TransformerException(se);
     }
     finally
     {
       m_serializationHandler = savedHandler;
  }
}
 
Example 3
Source File: TransformerImpl.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
  * Given a stylesheet element, create a result tree fragment from it's
  * contents.
  * @param templateParent The template element that holds the fragment.
  * @param dtmFrag The DTM to write the RTF into
  * @return the NodeHandle for the root node of the resulting RTF.
  *
  * @throws TransformerException
  * @xsl.usage advanced
  */
 private int transformToRTF(ElemTemplateElement templateParent,DTM dtmFrag)
         throws TransformerException
 {

   XPathContext xctxt = m_xcontext;
   
   ContentHandler rtfHandler = dtmFrag.getContentHandler();

   // Obtain the ResultTreeFrag's root node.
   // NOTE: In SAX2RTFDTM, this value isn't available until after
   // the startDocument has been issued, so assignment has been moved
   // down a bit in the code.
   int resultFragment; // not yet reliably = dtmFrag.getDocument();

   // Save the current result tree handler.
   SerializationHandler savedRTreeHandler = this.m_serializationHandler;


   // And make a new handler for the RTF.
   ToSAXHandler h = new ToXMLSAXHandler();
   h.setContentHandler(rtfHandler);
   h.setTransformer(this);
   
   // Replace the old handler (which was already saved)
   m_serializationHandler = h;

   // use local variable for the current handler
   SerializationHandler rth = m_serializationHandler;

   try
   {
     rth.startDocument();
     
     // startDocument is "bottlenecked" in RTH. We need it acted upon immediately,
     // to set the DTM's state as in-progress, so that if the xsl:variable's body causes
     // further RTF activity we can keep that from bashing this DTM.
     rth.flushPending(); 

     try
     {

       // Do the transformation of the child elements.
       executeChildTemplates(templateParent, true);

       // Make sure everything is flushed!
       rth.flushPending();
       
       // Get the document ID. May not exist until the RTH has not only
       // received, but flushed, the startDocument, and may be invalid
       // again after the document has been closed (still debating that)
       // ... so waiting until just before the end seems simplest/safest. 
resultFragment = dtmFrag.getDocument();      
     }
     finally
     {
       rth.endDocument();
     }
   }
   catch (org.xml.sax.SAXException se)
   {
     throw new TransformerException(se);
   }
   finally
   {

     // Restore the previous result tree handler.
     this.m_serializationHandler = savedRTreeHandler;
   }

   return resultFragment;
 }