/**
   * Implements javax.xml.transform.sax.TransformerHandler.setResult() Enables the user of the
   * TransformerHandler to set the to set the Result for the transformation.
   *
   * @param result A Result instance, should not be null
   * @throws IllegalArgumentException if result is invalid for some reason
   */
  public void setResult(Result result) throws IllegalArgumentException {
    _result = result;

    if (null == result) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.ER_RESULT_NULL);
      throw new IllegalArgumentException(err.toString()); // "result should not be null");
    }

    if (_isIdentity) {
      try {
        // Connect this object with output system directly
        SerializationHandler outputHandler = _transformer.getOutputHandler(result);
        _transformer.transferOutputProperties(outputHandler);

        _handler = outputHandler;
        _lexHandler = outputHandler;
      } catch (TransformerException e) {
        _result = null;
      }
    } else if (_done) {
      // Run the transformation now, if not already done
      try {
        _transformer.setDOM(_dom);
        _transformer.transform(null, _result);
      } catch (TransformerException e) {
        // What the hell are we supposed to do with this???
        throw new IllegalArgumentException(e.getMessage());
      }
    }
  }
  /**
   * javax.xml.transform.sax.TransformerFactory implementation. Look up the value of a feature (to
   * see if it is supported). This method must be updated as the various methods and features of
   * this class are implemented.
   *
   * @param name The feature name
   * @return 'true' if feature is supported, 'false' if not
   */
  public boolean getFeature(String name) {
    // All supported features should be listed here
    String[] features = {
      DOMSource.FEATURE,
      DOMResult.FEATURE,
      SAXSource.FEATURE,
      SAXResult.FEATURE,
      StreamSource.FEATURE,
      StreamResult.FEATURE
    };

    // feature name cannot be null
    if (name == null) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME);
      throw new NullPointerException(err.toString());
    }

    // Inefficient, but it really does not matter in a function like this
    for (int i = 0; i < features.length; i++) {
      if (name.equals(features[i])) return true;
    }

    // secure processing?
    if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
      return featureSecureProcessing;
    }

    // unknown feature
    return false;
  }
  /**
   * Implements org.xml.sax.ContentHandler.startDocument() Receive notification of the beginning of
   * a document.
   */
  public void startDocument() throws SAXException {
    // Make sure setResult() was called before the first SAX event
    if (_result == null) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_RESULT_ERR);
      throw new SAXException(err.toString());
    }

    if (!_isIdentity) {
      boolean hasIdCall = (_translet != null) ? _translet.hasIdCall() : false;
      XSLTCDTMManager dtmManager = null;

      // Create an internal DOM (not W3C) and get SAX2 input handler
      try {
        dtmManager =
            (XSLTCDTMManager)
                _transformer.getTransformerFactory().getDTMManagerClass().newInstance();
      } catch (Exception e) {
        throw new SAXException(e);
      }

      DTMWSFilter wsFilter;
      if (_translet != null && _translet instanceof StripFilter) {
        wsFilter = new DOMWSFilter(_translet);
      } else {
        wsFilter = null;
      }

      // Construct the DTM using the SAX events that come through
      _dom = (SAXImpl) dtmManager.getDTM(null, false, wsFilter, true, false, hasIdCall);

      _handler = _dom.getBuilder();
      _lexHandler = (LexicalHandler) _handler;
      _dtdHandler = (DTDHandler) _handler;
      _declHandler = (DeclHandler) _handler;

      // Set document URI
      _dom.setDocumentURI(_systemId);

      if (_locator != null) {
        _handler.setDocumentLocator(_locator);
      }
    }

    // Proxy call
    _handler.startDocument();
  }
  /**
   * Set a feature for this <code>SmartTransformerFactory</code> and <code>Transformer</code>s or
   * <code>Template</code>s created by this factory.
   *
   * <p>Feature names are fully qualified {@link java.net.URI}s. Implementations may define their
   * own features. An {@link TransformerConfigurationException} is thrown if this <code>
   * TransformerFactory</code> or the <code>Transformer</code>s or <code>Template</code>s it creates
   * cannot support the feature. It is possible for an <code>TransformerFactory</code> to expose a
   * feature value but be unable to change its state.
   *
   * <p>See {@link javax.xml.transform.TransformerFactory} for full documentation of specific
   * features.
   *
   * @param name Feature name.
   * @param value Is feature state <code>true</code> or <code>false</code>.
   * @throws TransformerConfigurationException if this <code>TransformerFactory</code> or the <code>
   *     Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
   * @throws NullPointerException If the <code>name</code> parameter is null.
   */
  public void setFeature(String name, boolean value) throws TransformerConfigurationException {

    // feature name cannot be null
    if (name == null) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME);
      throw new NullPointerException(err.toString());
    }
    // secure processing?
    else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
      featureSecureProcessing = value;
      // all done processing feature
      return;
    } else {
      // unknown feature
      ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name);
      throw new TransformerConfigurationException(err.toString());
    }
  }
 /** Compute the JVM signature for the class. */
 static final String getSignature(Class clazz) {
   if (clazz.isArray()) {
     final StringBuffer sb = new StringBuffer();
     Class cl = clazz;
     while (cl.isArray()) {
       sb.append("[");
       cl = cl.getComponentType();
     }
     sb.append(getSignature(cl));
     return sb.toString();
   } else if (clazz.isPrimitive()) {
     if (clazz == Integer.TYPE) {
       return "I";
     } else if (clazz == Byte.TYPE) {
       return "B";
     } else if (clazz == Long.TYPE) {
       return "J";
     } else if (clazz == Float.TYPE) {
       return "F";
     } else if (clazz == Double.TYPE) {
       return "D";
     } else if (clazz == Short.TYPE) {
       return "S";
     } else if (clazz == Character.TYPE) {
       return "C";
     } else if (clazz == Boolean.TYPE) {
       return "Z";
     } else if (clazz == Void.TYPE) {
       return "V";
     } else {
       final String name = clazz.toString();
       ErrorMsg err = new ErrorMsg(ErrorMsg.UNKNOWN_SIG_TYPE_ERR, name);
       throw new Error(err.toString());
     }
   } else {
     return "L" + clazz.getName().replace('.', '/') + ';';
   }
 }
Example #6
0
File: Util.java Project: srnsw/xena
  /** Creates a SAX2 InputSource object from a TrAX Source object */
  public static InputSource getInputSource(XSLTC xsltc, Source source)
      throws TransformerConfigurationException {
    InputSource input = null;

    String systemId = source.getSystemId();

    try {
      // Try to get InputSource from SAXSource input
      if (source instanceof SAXSource) {
        final SAXSource sax = (SAXSource) source;
        input = sax.getInputSource();
        // Pass the SAX parser to the compiler
        try {
          XMLReader reader = sax.getXMLReader();

          /*
           * Fix for bug 24695
           * According to JAXP 1.2 specification if a SAXSource
           * is created using a SAX InputSource the Transformer or
           * TransformerFactory creates a reader via the
           * XMLReaderFactory if setXMLReader is not used
           */

          if (reader == null) {
            try {
              reader = XMLReaderFactory.createXMLReader();
            } catch (Exception e) {
              try {

                // Incase there is an exception thrown
                // resort to JAXP
                SAXParserFactory parserFactory = SAXParserFactory.newInstance();
                parserFactory.setNamespaceAware(true);

                if (xsltc.isSecureProcessing()) {
                  try {
                    parserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                  } catch (org.xml.sax.SAXException se) {
                  }
                }

                reader = parserFactory.newSAXParser().getXMLReader();

              } catch (ParserConfigurationException pce) {
                throw new TransformerConfigurationException("ParserConfigurationException", pce);
              }
            }
          }
          reader.setFeature("http://xml.org/sax/features/namespaces", true);
          reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);

          xsltc.setXMLReader(reader);
        } catch (SAXNotRecognizedException snre) {
          throw new TransformerConfigurationException("SAXNotRecognizedException ", snre);
        } catch (SAXNotSupportedException snse) {
          throw new TransformerConfigurationException("SAXNotSupportedException ", snse);
        } catch (SAXException se) {
          throw new TransformerConfigurationException("SAXException ", se);
        }

      }
      // handle  DOMSource
      else if (source instanceof DOMSource) {
        final DOMSource domsrc = (DOMSource) source;
        final Document dom = (Document) domsrc.getNode();
        final DOM2SAX dom2sax = new DOM2SAX(dom);
        xsltc.setXMLReader(dom2sax);

        // Try to get SAX InputSource from DOM Source.
        input = SAXSource.sourceToInputSource(source);
        if (input == null) {
          input = new InputSource(domsrc.getSystemId());
        }
      }
      // Try to get InputStream or Reader from StreamSource
      else if (source instanceof StreamSource) {
        final StreamSource stream = (StreamSource) source;
        final InputStream istream = stream.getInputStream();
        final Reader reader = stream.getReader();
        xsltc.setXMLReader(null); // Clear old XML reader

        // Create InputSource from Reader or InputStream in Source
        if (istream != null) {
          input = new InputSource(istream);
        } else if (reader != null) {
          input = new InputSource(reader);
        } else {
          input = new InputSource(systemId);
        }
      } else {
        ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR);
        throw new TransformerConfigurationException(err.toString());
      }
      input.setSystemId(systemId);
    } catch (NullPointerException e) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR, "TransformerFactory.newTemplates()");
      throw new TransformerConfigurationException(err.toString());
    } catch (SecurityException e) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId);
      throw new TransformerConfigurationException(err.toString());
    }
    return input;
  }