Package net.sourceforge.gpstools.castor

Source Code of net.sourceforge.gpstools.castor.TransformerXMLSerializerFactory

/*
* Copyright 2007 Moritz Ringler
*
* 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 javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXTransformerFactory;
import org.exolab.castor.xml.OutputFormat;
import org.exolab.castor.xml.Serializer;
import org.exolab.castor.xml.XMLSerializerFactory;


/**
* An XMLSerializerFactory that relies only on the public javax.xml.transform
* API.
*
* @author Moritz Ringler
*/
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
   */
  @Override
public Serializer getSerializer() {
      try{
        return new TransformerSerializer(stf.newTransformerHandler());
      } catch (TransformerConfigurationException tcx){
          throw new RuntimeException("Cannot create TransformerSerializer.", tcx);
      }
  }

  /**
   * @inheritDoc
   */
  @Override
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);
  }
}
TOP

Related Classes of net.sourceforge.gpstools.castor.TransformerXMLSerializerFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.