Пример #1
0
  protected XSLProcessorImpl init(Result result) throws TransformerException {

    XSLProcessorImpl processor = (XSLProcessorImpl) _processor.clone();

    if (result instanceof StreamResult) {
      StreamResult sr = (StreamResult) result;

      OutputMethodHandlerImpl outputMethodHandler = new OutputMethodHandlerImpl();

      processor.setOutputMethodHandler(outputMethodHandler);
      Destination dest;
      OutputStream ostream = sr.getOutputStream();
      if (ostream != null) {

        dest = new OutputStreamDestination(ostream);
      } else {
        // FIXME: we need to handle a characterWriter
        throw new TransformerException("cannot use Writer result");
      }

      outputMethodHandler.setDestination(dest);

    } else if (result instanceof SAXResult) {

      SAXResult sr = (SAXResult) result;
      processor.setContentHandler(sr.getHandler());
      // FIXME: set lexical handler?
    } else {
      throw new TransformerException("unrecognized Result class: " + result.getClass().getName());
    }

    return processor;
  }
 private void setTarget(SAXResult result) {
   ContentHandler ch = result.getHandler();
   Check.notNull(ch);
   contentHandler = ch;
   LexicalHandler lh = result.getLexicalHandler();
   if (lh != null) {
     lexicalHandler = lh;
     if (lexicalHandler instanceof DTDHandler) {
       dtdHandler = (DTDHandler) lexicalHandler;
     }
   }
   if (contentHandler instanceof DTDHandler) {
     dtdHandler = (DTDHandler) contentHandler;
   }
 }
Пример #3
0
  public void validate(Source source, Result result) throws SAXException, IOException {
    if (result instanceof SAXResult || result == null) {
      final SAXSource saxSource = (SAXSource) source;
      final SAXResult saxResult = (SAXResult) result;

      if (result != null) {
        setContentHandler(saxResult.getHandler());
      }

      try {
        XMLReader reader = saxSource.getXMLReader();
        if (reader == null) {
          // create one now
          SAXParserFactory spf = SAXParserFactory.newInstance();
          spf.setNamespaceAware(true);
          try {
            reader = spf.newSAXParser().getXMLReader();
            // If this is a Xerces SAX parser, set the security manager if there is one
            if (reader instanceof com.sun.org.apache.xerces.internal.parsers.SAXParser) {
              SecurityManager securityManager =
                  (SecurityManager) fComponentManager.getProperty(SECURITY_MANAGER);
              if (securityManager != null) {
                try {
                  reader.setProperty(SECURITY_MANAGER, securityManager);
                }
                // Ignore the exception if the security manager cannot be set.
                catch (SAXException exc) {
                }
              }
            }
          } catch (Exception e) {
            // this is impossible, but better safe than sorry
            throw new FactoryConfigurationError(e);
          }
        }

        // If XML names and Namespace URIs are already internalized we
        // can avoid running them through the SymbolTable.
        try {
          fStringsInternalized = reader.getFeature(STRING_INTERNING);
        } catch (SAXException exc) {
          // The feature isn't recognized or getting it is not supported.
          // In either case, assume that strings are not internalized.
          fStringsInternalized = false;
        }

        ErrorHandler errorHandler = fComponentManager.getErrorHandler();
        reader.setErrorHandler(
            errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance());
        reader.setEntityResolver(fResolutionForwarder);
        fResolutionForwarder.setEntityResolver(fComponentManager.getResourceResolver());
        reader.setContentHandler(this);
        reader.setDTDHandler(this);

        InputSource is = saxSource.getInputSource();
        reader.parse(is);
      } finally {
        // release the reference to user's handler ASAP
        setContentHandler(null);
      }
      return;
    }
    throw new IllegalArgumentException(
        JAXPValidationMessageFormatter.formatMessage(
            Locale.getDefault(),
            "SourceResultMismatch",
            new Object[] {source.getClass().getName(), result.getClass().getName()}));
  }
Пример #4
0
 public SaxSerializer(SAXResult result) {
   this(result.getHandler(), result.getLexicalHandler());
 }