} else {
else
  public void transform(Source xmlSource, Result outputTarget)    throws TransformerException  {    // Get the source tree    DOMSource source;    synchronized (factory.resolver)      {        factory.resolver.setUserResolver(uriResolver);        factory.resolver.setUserListener(errorListener);        source = factory.resolver.resolveDOM(xmlSource, null, null);      }    Node context = source.getNode();    Document doc = (context instanceof Document) ? (Document) context :      context.getOwnerDocument();    if (doc instanceof DomDocument)      {        // Suppress mutation events        ((DomDocument) doc).setBuilding(true);        // TODO find a better/more generic way of doing this than        // casting      }    // Get the result tree    Node parent = null, nextSibling = null;    if (outputTarget instanceof DOMResult)      {        DOMResult dr = (DOMResult) outputTarget;        parent = dr.getNode();        nextSibling = dr.getNextSibling();        Document rdoc = (parent instanceof Document) ? (Document) parent :          parent.getOwnerDocument();        if (rdoc instanceof DomDocument)          {            // Suppress mutation events and allow multiple root elements            DomDocument drdoc = (DomDocument) rdoc;            drdoc.setBuilding(true);            drdoc.setCheckWellformedness(false);            // TODO find a better/more generic way of doing this than            // casting          }      }    boolean created = false;    // Transformation    if (stylesheet != null)      {        if (parent == null)          {            // Create a new document to hold the result            DomDocument resultDoc = new DomDocument();            resultDoc.setBuilding(true);            resultDoc.setCheckWellformedness(false);            parent = resultDoc;            created = true;          }        // Make a copy of the source node, and strip it        context = context.cloneNode(true);        strip(context);        // XSLT transformation        try          {            // Set output properties in the underlying stylesheet            ((TransformerOutputProperties) outputProperties).apply();            stylesheet.initTopLevelVariables(context);            TemplateNode t = stylesheet.getTemplate(null, context, false);            if (t != null)              {                stylesheet.current = context;                t.apply(stylesheet, null, context, 1, 1, parent, nextSibling);              }          }        catch (TransformerException e)          {            // Done transforming, reset document            if (doc instanceof DomDocument)              {                ((DomDocument) doc).setBuilding(false);              }            throw e;          }      }    else      {        // Identity transform        Node clone = context.cloneNode(true);        if (context.getNodeType() != Node.DOCUMENT_NODE)          {            Document resultDoc;            if (parent == null)              {                // Create a new document to hold the result                DomDocument rd = new DomDocument();                rd.setBuilding(true);                rd.setCheckWellformedness(false);                parent = resultDoc = rd;                created = true;              }            else              {                resultDoc = (parent instanceof Document) ?                  (Document) parent :                  parent.getOwnerDocument();              }            Document sourceDoc = context.getOwnerDocument();            if (sourceDoc != resultDoc)              {                clone = resultDoc.adoptNode(clone);              }            if (nextSibling != null)              {                parent.insertBefore(clone, nextSibling);              }            else              {                parent.appendChild(clone);              }          }        else          {            // Cannot append document to another tree            parent = clone;            created = true;          }      }    String method = outputProperties.getProperty(OutputKeys.METHOD);    int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML :      "text".equals(method) ? Stylesheet.OUTPUT_TEXT :      Stylesheet.OUTPUT_XML;    String encoding = outputProperties.getProperty(OutputKeys.ENCODING);    String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC);    String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM);    String version = outputProperties.getProperty(OutputKeys.VERSION);    boolean omitXmlDeclaration =       "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION));    boolean standalone =       "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE));    String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);    String cdataSectionElements =      outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS);    boolean indent =      "yes".equals(outputProperties.getProperty(OutputKeys.INDENT));    if (created)      {        // Discover document element        DomDocument resultDoc = (DomDocument) parent;        Node root = resultDoc.getDocumentElement();        // Add doctype if specified        if ((publicId != null || systemId != null) &&            root != null)          {            // We must know the name of the root element to            // create the document type            resultDoc.appendChild(new DomDoctype(resultDoc,                                                 root.getNodeName(),                                                 publicId,                                                 systemId));          }        resultDoc.setBuilding(false);        resultDoc.setCheckWellformedness(true);      }    else if (publicId != null || systemId != null)      {        switch (parent.getNodeType())          {          case Node.DOCUMENT_NODE:          case Node.DOCUMENT_FRAGMENT_NODE:            Document resultDoc = (parent instanceof Document) ?              (Document) parent :              parent.getOwnerDocument();            DOMImplementation impl = resultDoc.getImplementation();            DocumentType doctype =              impl.createDocumentType(resultDoc.getNodeName(),                                      publicId,                                      systemId);            // Try to insert doctype before first element            Node ctx = parent.getFirstChild();            for (; ctx != null &&                 ctx.getNodeType() != Node.ELEMENT_NODE;                 ctx = ctx.getNextSibling())              {              }            if (ctx != null)              {                parent.insertBefore(doctype, ctx);              }            else              {                parent.appendChild(doctype);              }          }      }    if (version != null)      {        parent.setUserData("version", version, stylesheet);      }    if (omitXmlDeclaration)      {        parent.setUserData("omit-xml-declaration", "yes", stylesheet);      }    if (standalone)      {        parent.setUserData("standalone", "yes", stylesheet);      }    if (mediaType != null)      {        parent.setUserData("media-type", mediaType, stylesheet);      }    if (cdataSectionElements != null)      {        List list = new LinkedList();        StringTokenizer st = new StringTokenizer(cdataSectionElements);        while (st.hasMoreTokens())          {            String name = st.nextToken();            String localName = name;            String uri = null;            String prefix = null;            int ci = name.indexOf(':');            if (ci != -1)              {                // Use namespaces defined on xsl:output node to resolve                // namespaces for QName                prefix = name.substring(0, ci);                localName = name.substring(ci + 1);                uri = stylesheet.output.lookupNamespaceURI(prefix);              }            list.add(new QName(uri, localName, prefix));          }        if (!list.isEmpty())          {            Document resultDoc = (parent instanceof Document) ?              (Document) parent :              parent.getOwnerDocument();            convertCdataSectionElements(resultDoc, parent, list);          }      }    if (indent)      {        parent.normalize();        strip(parent);        Document resultDoc = (parent instanceof Document) ?          (Document) parent :          parent.getOwnerDocument();        reindent(resultDoc, parent, 0);      }    // Render result to the target device    if (outputTarget instanceof DOMResult)      {        if (created)          {            DOMResult dr = (DOMResult) outputTarget;            dr.setNode(parent);            dr.setNextSibling(null);          }      }    else if (outputTarget instanceof StreamResult)      {        StreamResult sr = (StreamResult) outputTarget;        IOException ex = null;        try          {            writeStreamResult(parent, sr, outputMethod, encoding);          }        catch (UnsupportedEncodingException e)          {            try              {                writeStreamResult(parent, sr, outputMethod, "UTF-8");              }            catch (IOException e2)              {                ex = e2;              }          }        catch (IOException e)          {            ex = e;          }        if (ex != null)          {            if (errorListener != null)              {                errorListener.error(new TransformerException(ex));              }            else              {                ex.printStackTrace(System.err);              }          }      }    else if (outputTarget instanceof SAXResult)      {        SAXResult sr = (SAXResult) outputTarget;        try          {            ContentHandler ch = sr.getHandler();            LexicalHandler lh = sr.getLexicalHandler();            if (lh == null && ch instanceof LexicalHandler)              {                lh = (LexicalHandler) ch;              }            SAXSerializer serializer = new SAXSerializer();            serializer.serialize(parent, ch, lh);          }        catch (SAXException e)          {            if (errorListener != null)              {                errorListener.error(new TransformerException(e));              }            else              {                e.printStackTrace(System.err);              }          }      }  }