Werner Guttmann schrieb:
Hello Werner,
I've written some draft code. Most of it is actually concerned with
wrapping a SAX2 ContentHandler as a SAX1 DocumentHandler. It seems to
work fine in my application but I haven't tested it thoroughly. I've
attached the files (hoping that the list accepts attachments) feel free
to use them in Castor as you please.
> PS One more question: are you deliberately using the 'pretty printing'
> feature of Castor ? 'cause if not, there's no dependency to Xerces
> anymore.
Oh, I didn't know that. So I could probably have spared myself the
effort and simply sent the non-indented marshalling result through an
indenting identity transformer to get pretty-printing. Tant pis. It was
a nice exercise.
Regards,
Moritz
/*
 * Copyright 2006 Werner Guttmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sourceforge.gpstools.castor;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.castor.util.Messages;
import org.xml.sax.DocumentHandler;

/**
 * Implementation of the Serializer interface built upon
 * a SAX TransformerHandler.
 *
 * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner 
Guttmann</a>
 * @version $Revision: 6216 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 
2006) $
 * @since 1.1
 */
public class TransformerSerializer implements org.exolab.castor.xml.Serializer, 
org.xml.sax.DocumentHandler{
    private final TransformerHandler th;

    public TransformerSerializer(TransformerHandler handler) {
        th = handler;
    }

    public void setOutputCharStream(Writer out) {
        th.getTransformer().setResult(new StreamResult(out));
    }

    public DocumentHandler asDocumentHandler() throws IOException {
        return this;
    }

    public void setOutputFormat(OutputFormat format) {
        th.setOutputProperties((Properties) format.getFormat());
    }

    public void setOutputByteStream(OutputStream output) {
        th.getTransformer().setResult(new StreamResult(output));
    }

    public void setDocumentLocator (Locator locator){
        th.setDocumentLocator(locator);
    }

    public void startDocument ()
    throws SAXException{
        th.startdocument();
    }

    public void endDocument ()
    throws SAXException{
        endDocument();
    }

    public abstract void startElement (String name, AttributeList atts)
    throws SAXException{
        //judging from org.exolab.castor.xml.util.DocumentHandlerAdapter
        //uses the qname (3rd argument)
        th.startElement("", "", name, atts);
    }

    public abstract void endElement (String name)
    throws SAXException{
        th.endElement("", "", name);
    }

    public abstract void characters (char ch[], int start, int length)
    throws SAXException{
        th.characters(ch, start, length);
    }

    public abstract void ignorableWhitespace (char ch[], int start, int length)
    throws SAXException{
        th.ignorableWhitespace(ch, start, length);
    }

    public abstract void processingInstruction (String target, String data)
    throws SAXException{
        th.processingInstruction(target, data);
    }
}
/*
 * Copyright 2006 Werner Guttmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sourceforge.gpstools.castor;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.castor.util.Messages;
import org.xml.sax.DocumentHandler;

/**
 * Xerces-specific implementation of the Serializer interface, used for
 * JDK 5 only where Xerecs has been integrated with the core code base.
 *
 * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner 
Guttmann</a>
 * @version $Revision: 6216 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 
2006) $
 * @since 1.1
 */
/**
 * Weblogic Xerces-specific implementation of the [EMAIL PROTECTED] 
XMLSerializerFactory} interface.
 * Returns Weblogic Xerces-specific instances of the [EMAIL PROTECTED] 
Serializer} and
 * [EMAIL PROTECTED] OutputFormat} interfaces.
 */
public class TransformerXMLSerializerFactory implements XMLSerializerFactory {
  private final SAXTransformerFactory stf;

  /** @throws TransformerConfigurationError if the TransformerFactory
     returned by TransformerFactory.newInstance() is not a SAXTransformerFactory
   **/
  public TransformerXMLSerializerFactory(){
      TransformerFactory tf = TransformerFactory.newInstance();
      if(!tf.getFeature(SAXTransformerFactory.FEATURE) || !(tf instanceof 
SAXTransformerFactory)){
          throw new TransformerFactoryConfigurationError(
              tf.getClass().getName() +
              " is not a SAXTransformerFactory." +
              "Please set the system property " +
              "javax.xml.transform.TransformerFactory to a "+
              "SAXTransformerFactory implementation.") ;
      }
      stf = (SAXTransformerFactory) tf;
  }

  /**
   * @inheritDoc
   */
  public Serializer getSerializer() {
    return new TransformerSerializer(stf.newTransformerHandler());
  }

  /**
   * @inheritDoc
   */
  public OutputFormat getOutputFormat() {
      String preserveSpaceProperty = null;
      String omitDocumentTypeProperty = null;
      //String tfClass = stf.getClass().getName();
      //if("my.special.transformer.Factory".equals(tfClass)){
          // preserveSpaceProperty = 
my.special.transformer.OutputKeys.PRESERVE_SPACE;
          // omitDocumentTypeProperty = 
my.special.transformer.OutputKeys.OMIT_DOCUMENT_TYPE_DECLARATION;
      //} else if(...) {
      // }
      return new TransformerOutputFormat(preserveSpaceProperty, 
omitDocumentTypeProperty);
  }
}
/*
 * Copyright 2006 Werner Guttmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sourceforge.gpstools.castor;

import org.apache.xml.serialize.OutputFormat;
import org.exolab.castor.util.Properties;
import javax.xml.transform.OutputKeys;

public class TransformerOutputFormat implements 
org.exolab.castor.xml.OutputFormat {

    final Properties p = new Properties();
    final private String preserveSpaceProperty;
    final private String omitDocumentTypeProperty;

    /**
        Constructs a new PropertiesOutputFormat that uses
        only standard output properties from
        [EMAIL PROTECTED] javax.xml.transform.OutputKeys} and ignores
        setOmitDocumentType and setPreserveSpace.
    **/
    public PropertiesOutputFormat() {
        this(null, null);//default constructor
    }

    /** Constructs a new PropertiesOutputFormat that uses
        non-standard output properties. By default
        TransformerOutputFormat ignores setOmitDocumentType
        and setPreserveSpace, because no corresponding fields
        exist in [EMAIL PROTECTED] javax.xml.transform.OutputKeys}. If you know
        that your Transformer implementation supports additional
        output properties corresponding to these methods, you can
        specify them here. These properties will then be set to "yes" or
        "no" in the Properties object returned by getFormat, depending
        on the boolean arguments passed to the corresponding setXXX
        method.
     **/
    public PropertiesOutputFormat(String preserveSpaceProperty, String 
omitDocumentTypeProperty){
        this.preserveSpaceProperty = preserveSpaceProperty;
        this.omitDocumentTypeProperty = omitDocumentTypeProperty;
    }

    public void setMethod(String method) {
        if(XML.equals(method)){
            p.setProperty(OutputKeys.METHOD, "xml");
        } else {
            p.setProperty(OutputKeys.METHOD, method);
        }
    }

    public void setIndenting(boolean indent) {
        p.setProperty(OutputKeys.INDENT, (indent)? "yes" : "no");
    }

    public void setPreserveSpace(boolean preserveSpace) {
        // no corresponding OutputKeys field
        if(preserveSpaceProperty != null){
            p.setProperty(preserveSpaceProperty, (preserveSpace)? "yes" : "no");
        }
    }

    public Object getFormat() {
        return p;
    }

    public void setDoctype (String type1, String type2) {
        p.setProperty(OutputKeys.DOCTYPE_PUBLIC, type1);
        p.setProperty(OutputKeys.DOCTYPE_SYSTEM, type2);
    }

    public void setOmitXMLDeclaration(boolean b) {
        p.setProperty(OutputKeys.OMIT_XML_DECLARATION, (b)? "yes" : "no");
    }

    public void setOmitDocumentType(boolean omitDocumentType) {
        // no corresponding OutputKeys field
        if(omitDocumentTypeProperty != null){
            p.setProperty(omitDocumentTypeProperty, (preserveSpace)? "yes" : 
"no");
        }
    }

    public void setEncoding(String encoding) {
        p.setProperty(OutputKeys.ENCODING, encoding);
    }

}


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to