view software/eXist/mpdl-modules/src/org/exist/xquery/modules/mpdldoc/Transform.java @ 16:257f67be5c00

diverse Fehlerbehebungen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Sep 2011 16:40:57 +0200
parents
children
line wrap: on
line source

/*
 *  eXist Open Source Native XML Database: Extension module
 *  Copyright (C) 2008 Josef Willenborg
 *  jwillenborg@mpiwg-berlin.mpg.de
 *  http://www.mpiwg-berlin.mpg.de
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id:  $
 */
package org.exist.xquery.modules.mpdldoc;

import java.util.Properties;

import org.exist.dom.QName;
import org.exist.xquery.BasicFunction;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.StringValue;
import org.exist.xquery.value.Type;
import org.exist.xquery.value.ValueSequence;

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

/**
 * @author Josef Willenborg (jwillenborg@mpiwg-berlin.mpg.de)
 */
public class Transform extends BasicFunction {

	public final static FunctionSignature signature =
		new FunctionSignature(
			new QName("transform", MPDLDocModule.NAMESPACE_URI, MPDLDocModule.PREFIX),
			"A function which transforms the input xml string by the xsl stylesheet given as xslFileName and outputs it as a string.",
			new SequenceType[] { 
        new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE),
        new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE),
        new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE)
			  },
			new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE));

	public Transform(XQueryContext context) {
		super(context, signature);
	}

  public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    try {
      Sequence firstSeq = args[0];
      Sequence secondSeq = args[1];
      Sequence thirdSeq = args[2];
      if (firstSeq.isEmpty() || secondSeq.isEmpty())
        return Sequence.EMPTY_SEQUENCE;
      String xmlStr = firstSeq.getStringValue();
      String xslFileName = secondSeq.getStringValue();
      String outputPropertiesStr = thirdSeq.getStringValue();
      Properties outputProperties = new Properties();
      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);
          String value = prop.substring(index + 1);
          outputProperties.setProperty(key, value);
        }
      }
      String resultStr = XmlUtil.getInstance().transform(xmlStr, xslFileName, outputProperties);
      ValueSequence resultSequence = new ValueSequence();
      resultSequence.add(new StringValue(resultStr));
      return resultSequence;
    } catch (ApplicationException e) {
      throw new XPathException(e.getMessage());
    }
  }
}