comparison 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
comparison
equal deleted inserted replaced
15:e99964f390e4 16:257f67be5c00
1 /*
2 * eXist Open Source Native XML Database: Extension module
3 * Copyright (C) 2008 Josef Willenborg
4 * jwillenborg@mpiwg-berlin.mpg.de
5 * http://www.mpiwg-berlin.mpg.de
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * $Id: $
22 */
23 package org.exist.xquery.modules.mpdldoc;
24
25 import java.util.Properties;
26
27 import org.exist.dom.QName;
28 import org.exist.xquery.BasicFunction;
29 import org.exist.xquery.Cardinality;
30 import org.exist.xquery.FunctionSignature;
31 import org.exist.xquery.XPathException;
32 import org.exist.xquery.XQueryContext;
33 import org.exist.xquery.value.Sequence;
34 import org.exist.xquery.value.SequenceType;
35 import org.exist.xquery.value.StringValue;
36 import org.exist.xquery.value.Type;
37 import org.exist.xquery.value.ValueSequence;
38
39 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
40 import de.mpg.mpiwg.berlin.mpdl.util.XmlUtil;
41
42 /**
43 * @author Josef Willenborg (jwillenborg@mpiwg-berlin.mpg.de)
44 */
45 public class Transform extends BasicFunction {
46
47 public final static FunctionSignature signature =
48 new FunctionSignature(
49 new QName("transform", MPDLDocModule.NAMESPACE_URI, MPDLDocModule.PREFIX),
50 "A function which transforms the input xml string by the xsl stylesheet given as xslFileName and outputs it as a string.",
51 new SequenceType[] {
52 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE),
53 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE),
54 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE)
55 },
56 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE));
57
58 public Transform(XQueryContext context) {
59 super(context, signature);
60 }
61
62 public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
63 try {
64 Sequence firstSeq = args[0];
65 Sequence secondSeq = args[1];
66 Sequence thirdSeq = args[2];
67 if (firstSeq.isEmpty() || secondSeq.isEmpty())
68 return Sequence.EMPTY_SEQUENCE;
69 String xmlStr = firstSeq.getStringValue();
70 String xslFileName = secondSeq.getStringValue();
71 String outputPropertiesStr = thirdSeq.getStringValue();
72 Properties outputProperties = new Properties();
73 if (outputPropertiesStr != null && ! outputPropertiesStr.equals("")) {
74 String[] outputProps = outputPropertiesStr.split(" ");
75 for (int i=0; i<outputProps.length; i++) {
76 String prop = outputProps[i];
77 int index = prop.indexOf("=");
78 String key = prop.substring(0, index);
79 String value = prop.substring(index + 1);
80 outputProperties.setProperty(key, value);
81 }
82 }
83 String resultStr = XmlUtil.getInstance().transform(xmlStr, xslFileName, outputProperties);
84 ValueSequence resultSequence = new ValueSequence();
85 resultSequence.add(new StringValue(resultStr));
86 return resultSequence;
87 } catch (ApplicationException e) {
88 throw new XPathException(e.getMessage());
89 }
90 }
91 }