view software/mpdl-services/mpiwg-mpdl-xml/src/de/mpg/mpiwg/berlin/mpdl/xml/transform/BasicTransformer.java @ 23:e845310098ba

diverse Korrekturen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Nov 2012 12:35:19 +0100
parents dc5e9fcb3fdc
children
line wrap: on
line source

package de.mpg.mpiwg.berlin.mpdl.xml.transform;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;

import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XdmAtomicValue;
import net.sf.saxon.s9api.XdmValue;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;

public class BasicTransformer {
  private Processor processor;
  private XsltCompiler xsltCompiler;
  private XsltTransformer xsltTransformer;
  
  public static void main(String[] args) {
    try {
      BasicTransformer t = new BasicTransformer();
      String xmlDocFileName = "file:/Users/jwillenborg/texts/mpdl/documents/echo/la/Aristoteles_1585_XSY685ZD.xml";
      String xslDocFileName = "/Users/jwillenborg/java/mpiwg-mpdl-xml/examples/generateId.xsl";
      String destFileName = "/Users/jwillenborg/java/mpiwg-mpdl-xml/examples/Aristoteles_1585_XSY685ZD-Ids.xml";
      // t.transform(xmlDocFileName, xslDocFileName, null, "encoding=utf-8 indent=yes", destFileName);
      t.transform(xmlDocFileName, xslDocFileName, null, null);
      // String result = t.transform(xmlDocFileName, xslDocFileName, null, null);
      // ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes("utf-8"));
      // FileUtil.getInstance().saveInputStreamToLocalFile(is, destFileName);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public BasicTransformer() {
    processor = new Processor(false); 
    xsltCompiler = processor.newXsltCompiler();
  }
  
  public void transform(String srcUrl, String xslUrl, String parametersStr, String outputPropertiesStr, String destFileName) throws ApplicationException {
    try {
      StreamSource xslStreamSource = new StreamSource(xslUrl);
      XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource);
      xsltTransformer = xsltExecutable.load();
      StreamSource xmlDoc = new StreamSource(srcUrl);
      Serializer serializer = new Serializer();
      String encoding = "utf-8"; // default
      if (outputPropertiesStr != null) {
        String enc = getEncoding(outputPropertiesStr);
        if (enc != null && ! enc.isEmpty())
          encoding = enc;
      }
      Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFileName), encoding));
      serializer.setOutputWriter(writer);
      setOutputProperties(serializer, outputPropertiesStr);
      xsltTransformer.setSource(xmlDoc);  // needs some time for bigger documents
      xsltTransformer.setDestination(serializer);
      setParameters(xsltTransformer, parametersStr);
      xsltTransformer.transform();  // needs some time for bigger documents
    } catch (SaxonApiException e) {
      throw new ApplicationException(e);
    } catch (IOException e) {
      throw new ApplicationException(e);
    }
  }
  
  public String transform(String srcUrl, String xslUrl, String parametersStr, String outputPropertiesStr) throws ApplicationException {
    String retStr = null;
    try {
      StreamSource xslStreamSource = new StreamSource(xslUrl);
      XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource);
      xsltTransformer = xsltExecutable.load();
      StreamSource xmlDoc = new StreamSource(srcUrl);
      Serializer serializer = new Serializer();
      StringWriter stringWriter = new StringWriter();
      serializer.setOutputWriter(stringWriter);
      setOutputProperties(serializer, outputPropertiesStr);
      xsltTransformer.setSource(xmlDoc);  // needs some time for bigger documents
      xsltTransformer.setDestination(serializer);
      setParameters(xsltTransformer, parametersStr);
      xsltTransformer.transform();  // needs some time for bigger documents
      StringWriter out = (StringWriter) serializer.getOutputDestination();
      retStr = out.toString(); 
    } catch (SaxonApiException e) {
      throw new ApplicationException(e);
    }
    return retStr;
  }
  
  public String transform(StringReader inputStrReader, String xslUrl, String parametersStr, String outputPropertiesStr) throws ApplicationException {
    String retStr = null;
    try {
      StreamSource xslStreamSource = new StreamSource(xslUrl);
      XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource);
      xsltTransformer = xsltExecutable.load();
      StreamSource xmlDoc = new StreamSource(inputStrReader);
      Serializer serializer = new Serializer();
      StringWriter stringWriter = new StringWriter();
      serializer.setOutputWriter(stringWriter);
      setOutputProperties(serializer, outputPropertiesStr);
      xsltTransformer.setSource(xmlDoc);  // needs some time for bigger documents
      xsltTransformer.setDestination(serializer);
      setParameters(xsltTransformer, parametersStr);
      xsltTransformer.transform();  // needs some time for bigger documents
      StringWriter out = (StringWriter) serializer.getOutputDestination();
      retStr = out.toString(); 
    } catch (SaxonApiException e) {
      throw new ApplicationException(e);
    }
    return retStr;
  }
  
  private String getEncoding(String outputPropertiesStr) {
    String str = outputPropertiesStr.toLowerCase();
    String encoding = null;
    if (str.matches("encoding=([^ ]+)$")) {
      encoding = outputPropertiesStr.toLowerCase().replaceAll("encoding=([^ ]+)$", "$1");
    } else if (str.matches("encoding=(.+) .*")) {
      encoding = outputPropertiesStr.toLowerCase().replaceAll("encoding=(.+) .*", "$1");
    }
    return encoding;
  }
  
  private void setParameters(XsltTransformer xsltTransformer, String parametersStr) {
    if (parametersStr != null && ! parametersStr.equals("")) {
      String[] parameters = parametersStr.split(" ");
      for (int i=0; i<parameters.length; i++) {
        String param = parameters[i];
        int index = param.indexOf("=");
        String key = param.substring(0, index);
        String value = param.substring(index + 1);
        if (key != null && value != null && ! key.isEmpty() && !value.isEmpty()) {
          QName keyQName = new QName(key);
          XdmValue valueXdmValue = new XdmAtomicValue(value);
          xsltTransformer.setParameter(keyQName, valueXdmValue);
        }
      }
    }
  }
  
  private void setOutputProperties(Serializer serializer, String outputPropertiesStr) {
    if (outputPropertiesStr != null && ! outputPropertiesStr.equals("")) {
      String[] outputProps = outputPropertiesStr.split(" ");
      for (int i=0; i<outputProps.length; i++) {
        String prop = outputProps[i];
        int index = prop.indexOf("=");
        String key = prop.substring(0, index).toLowerCase();
        String value = prop.substring(index + 1);
        if (key != null && value != null && ! key.isEmpty() && !value.isEmpty()) {
          if (key.equals("method"))
            serializer.setOutputProperty(Serializer.Property.METHOD, value);
          else if (key.equals("indent"))
            serializer.setOutputProperty(Serializer.Property.INDENT, value);
          else if (key.equals("media-type"))
            serializer.setOutputProperty(Serializer.Property.MEDIA_TYPE, value);
          else if (key.equals("omit-xml-declaration"))
            serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, value);
          else if (key.equals("encoding"))
            serializer.setOutputProperty(Serializer.Property.ENCODING, value);
          else if (key.equals("cdata-section-elements"))
            serializer.setOutputProperty(Serializer.Property.CDATA_SECTION_ELEMENTS, value);
          else if (key.equals("version"))
            serializer.setOutputProperty(Serializer.Property.VERSION, value);
          else if (key.equals("doctype-system"))
            serializer.setOutputProperty(Serializer.Property.DOCTYPE_SYSTEM, value);
          else if (key.equals("doctype-public"))
            serializer.setOutputProperty(Serializer.Property.DOCTYPE_PUBLIC, value);
          else if (key.equals("standalone"))
            serializer.setOutputProperty(Serializer.Property.STANDALONE, value);
        }
      }
    }
  }

  private void setOutputProperties(Transformer transformer, String outputPropertiesStr) {
    if (outputPropertiesStr != null && ! outputPropertiesStr.equals("")) {
      String[] outputProps = outputPropertiesStr.split(" ");
      for (int i=0; i<outputProps.length; i++) {
        String prop = outputProps[i];
        int index = prop.indexOf("=");
        String key = prop.substring(0, index).toLowerCase();
        String value = prop.substring(index + 1);
        if (key != null && value != null && ! key.isEmpty() && !value.isEmpty()) {
          if (key.equals("method"))
            transformer.setOutputProperty(OutputKeys.METHOD, value);
          if (key.equals("indent"))
            transformer.setOutputProperty(OutputKeys.INDENT, value);
          if (key.equals("media-type"))
            transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, value);
          if (key.equals("omit-xml-declaration"))
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, value);
          if (key.equals("encoding"))
            transformer.setOutputProperty(OutputKeys.ENCODING, value);
          else if (key.equals("cdata-section-elements"))
            transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, value);
          else if (key.equals("version"))
            transformer.setOutputProperty(OutputKeys.VERSION, value);
          else if (key.equals("doctype-system"))
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, value);
          else if (key.equals("doctype-public"))
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, value);
          else if (key.equals("standalone"))
            transformer.setOutputProperty(OutputKeys.STANDALONE, value);
        }
      }
    }
  }
  
  private void getFragment(String xmlFileName, String ms1Name, int ms1Pos, String ms2Name, int ms2Pos) throws ApplicationException {
  	try {
      StreamSource xmlDoc = new StreamSource(new StringReader("<?xml version=\"1.0\"?><bla/>"));
      xmlDoc = new StreamSource(xmlFileName);
      Serializer serializer = new Serializer();
      serializer.setOutputWriter(new StringWriter());
      serializer.setOutputProperty(Serializer.Property.INDENT, "yes");
      xsltTransformer.setSource(xmlDoc);  // needs some time for bigger documents
      xsltTransformer.setDestination(serializer);
      QName xmlFileNameQName = new QName("xmlFileName");
      XdmValue xmlFileNameXdmValue = new XdmAtomicValue(xmlFileName);
      QName ms1NameQName = new QName("ms1Name");
      XdmValue ms1NameXdmValue = new XdmAtomicValue(ms1Name);
      QName ms1PositionQName = new QName("ms1Position");
      XdmValue ms1PositionXdmValue = new XdmAtomicValue(ms1Pos);
      QName ms2NameQName = new QName("ms2Name");
      XdmValue ms2NameXdmValue = new XdmAtomicValue(ms2Name);
      QName ms2PositionQName = new QName("ms2Position");
      XdmValue ms2PositionXdmValue = new XdmAtomicValue(ms2Pos);
      xsltTransformer.setParameter(xmlFileNameQName, xmlFileNameXdmValue);
      xsltTransformer.setParameter(ms1NameQName, ms1NameXdmValue);
      xsltTransformer.setParameter(ms1PositionQName, ms1PositionXdmValue);
      xsltTransformer.setParameter(ms2NameQName, ms2NameXdmValue);
      xsltTransformer.setParameter(ms2PositionQName, ms2PositionXdmValue);
      xsltTransformer.transform();  // needs some time for bigger documents
      // String pageFragment = serializer.getOutputDestination().toString();
      // System.out.println(pageFragment);
  	} catch (SaxonApiException e) {
      throw new ApplicationException(e); 
  	}
  }

  private String transformByTransformerFactory(String xmlString, String xslFileName, String parametersStr, String outputPropertiesStr) throws ApplicationException {
    String resultString = null;
    try {
      StreamSource xslSource = new StreamSource(xslFileName);
      Transformer transformer = TransformerFactory.newInstance(net.sf.saxon.TransformerFactoryImpl.class.getName(), null).newTransformer(xslSource);
      setOutputProperties(transformer, outputPropertiesStr);
      StreamResult result = new StreamResult(new StringWriter());
      StreamSource source = new StreamSource(xmlString);
      transformer.transform(source, result);
      resultString = result.getWriter().toString();
    } catch (TransformerConfigurationException e) {
      throw new ApplicationException(e);
    } catch (TransformerException e) {
      throw new ApplicationException(e);
    }
    return resultString;
  }
  
  
  
}