Exemple #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;
  }
Exemple #2
0
  /** Public default constructor. */
  public JDOMResult() {
    // Allocate custom builder object...
    DocumentBuilder builder = new DocumentBuilder();

    // And use it as ContentHandler and LexicalHandler.
    super.setHandler(builder);
    super.setLexicalHandler(builder);
  }
 /** Transform a SAX source to SAX events. */
 public static void sourceToSAX(Source source, XMLReceiver xmlReceiver) {
   try {
     final Transformer identity = getIdentityTransformer();
     final SAXResult saxResult = new SAXResult(xmlReceiver);
     saxResult.setLexicalHandler(xmlReceiver);
     identity.transform(source, saxResult);
   } catch (TransformerException e) {
     throw new OXFException(e);
   }
 }
 @Override
 protected void marshalSaxHandlers(
     Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
     throws XmlMappingException {
   try {
     // JiBX does not support SAX natively, so we write to a buffer first, and transform that to
     // the handlers
     SAXResult saxResult = new SAXResult(contentHandler);
     saxResult.setLexicalHandler(lexicalHandler);
     transformAndMarshal(graph, saxResult);
   } catch (IOException ex) {
     throw new MarshallingFailureException("JiBX marshalling exception", ex);
   }
 }
 /* (non-Javadoc)
  * @see org.exist.collections.Trigger#prepare(java.lang.String, org.w3c.dom.Document)
  */
 public void prepare(
     int event,
     DBBroker broker,
     Txn transaction,
     XmldbURI documentName,
     DocumentImpl existingDocument)
     throws TriggerException {
   SAXResult result = new SAXResult();
   result.setHandler(getOutputHandler());
   result.setLexicalHandler(getLexicalOutputHandler());
   handler.setResult(result);
   setOutputHandler(handler);
   setLexicalOutputHandler(handler);
 }
 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;
   }
 }
  @Override
  protected void setSAXConsumer(SAXConsumer consumer) {
    TransformerHandler handler = createTransformerHandler();

    if (consumer instanceof SaxonSerializer) {
      // serializer will finish setup of handler result
      SaxonSerializer serializer = (SaxonSerializer) consumer;
      serializer.setTransformerHandler(handler);
    } else {
      SAXResult result = new SAXResult();
      result.setHandler(consumer);
      // According to TrAX specification, all TransformerHandlers are LexicalHandlers
      result.setLexicalHandler(consumer);
      handler.setResult(result);
    }

    SAXConsumerAdapter saxConsumerAdapter = new SAXConsumerAdapter();
    saxConsumerAdapter.setContentHandler(handler);

    super.setSAXConsumer(saxConsumerAdapter);
  }
  /** Set the <code>XMLConsumer</code> that will receive XML data. */
  public void setConsumer(XMLConsumer consumer) {

    if (this.transformerHandler == null) {
      try {
        this.transformerHandler = this.xsltProcessor.getTransformerHandler(this.inputSource);
      } catch (XSLTProcessorException se) {
        // the exception will be thrown during startDocument()
        this.exceptionDuringSetConsumer =
            new SAXException(
                "Unable to get transformer handler for " + this.inputSource.getURI(), se);
        return;
      }
    }
    final Map map = getLogicSheetParameters();
    if (map != null) {
      final javax.xml.transform.Transformer transformer = this.transformerHandler.getTransformer();
      final Iterator iterator = map.entrySet().iterator();
      while (iterator.hasNext()) {
        final Map.Entry entry = (Entry) iterator.next();
        transformer.setParameter((String) entry.getKey(), entry.getValue());
      }
    }

    super.setContentHandler(this.transformerHandler);
    super.setLexicalHandler(this.transformerHandler);

    if (this.transformerHandler instanceof LogEnabled) {
      ((LogEnabled) this.transformerHandler).enableLogging(getLogger());
    }
    // According to TrAX specs, all TransformerHandlers are LexicalHandlers
    final SAXResult result = new SAXResult(consumer);
    result.setLexicalHandler(consumer);
    this.transformerHandler.setResult(result);

    this.errorListener = new TraxErrorListener(getLogger(), this.inputSource.getURI());
    this.transformerHandler.getTransformer().setErrorListener(this.errorListener);
  }
  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()}));
  }
 public SaxSerializer(SAXResult result) {
   this(result.getHandler(), result.getLexicalHandler());
 }