Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
View
32 extensions/modules/src/org/exist/xquery/modules/file/SerializeToFile.java
@@ -44,6 +44,7 @@
import org.exist.xquery.Option;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
+import org.exist.xquery.util.SerializerUtils;
import org.exist.xquery.value.BinaryValue;
import org.exist.xquery.value.BooleanValue;
import org.exist.xquery.value.FunctionParameterSequenceType;
@@ -53,6 +54,7 @@
import org.exist.xquery.value.SequenceIterator;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;
+import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class SerializeToFile extends BasicFunction
@@ -75,8 +77,9 @@
Cardinality.ZERO_OR_MORE, "The contents to write to the file system." ),
new FunctionParameterSequenceType( "path", Type.ITEM,
Cardinality.EXACTLY_ONE, "The full path or URI to the file" ),
- new FunctionParameterSequenceType( "parameters", Type.STRING,
- Cardinality.ZERO_OR_MORE, "The serialization parameters specified as key-value pairs" )
+ new FunctionParameterSequenceType( "parameters", Type.ITEM,
+ Cardinality.ZERO_OR_MORE, "The serialization parameters: either a sequence of key=value pairs or an output:serialization-parameters " +
+ "element as defined by the standard fn:serialize function." )
},
new FunctionReturnSequenceType( Type.BOOLEAN,
Cardinality.ZERO_OR_ONE, "true on success - false if the specified file can not be "
@@ -96,8 +99,9 @@
Cardinality.ZERO_OR_MORE, "The contents to write to the file system." ),
new FunctionParameterSequenceType( "path", Type.ITEM,
Cardinality.EXACTLY_ONE, "The full path or URI to the file" ),
- new FunctionParameterSequenceType( "parameters", Type.STRING,
- Cardinality.ZERO_OR_MORE, "The serialization parameters specified as key-value pairs" ),
+ new FunctionParameterSequenceType( "parameters", Type.ITEM,
+ Cardinality.ZERO_OR_MORE, "The serialization parameters: either a sequence of key=value pairs or an output:serialization-parameters " +
+ "element as defined by the standard fn:serialize function." ),
new FunctionParameterSequenceType( "append", Type.BOOLEAN,
Cardinality.EXACTLY_ONE, "Should content be appended?")
},
@@ -170,7 +174,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
if(isCalledAs(FN_SERIALIZE_LN)) {
//parse serialization options from third argument to function
- final Properties outputProperties = parseXMLSerializationOptions( args[2].iterate() );
+ final Properties outputProperties = parseXMLSerializationOptions( args[2] );
final boolean doAppend = (args.length > 3) && "true".equals(args[3].itemAt(0).getStringValue());
//do the serialization
@@ -186,13 +190,17 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
}
- private Properties parseXMLSerializationOptions(final SequenceIterator siSerializeParams) throws XPathException {
- //parse serialization options
- final Properties outputProperties = new Properties();
+ private Properties parseXMLSerializationOptions(final Sequence sSerializeParams) throws XPathException {
+ //parse serialization options
+ final Properties outputProperties = new Properties();
- outputProperties.setProperty( OutputKeys.INDENT, "yes" );
- outputProperties.setProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" );
+ outputProperties.setProperty( OutputKeys.INDENT, "yes" );
+ outputProperties.setProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" );
+ if (sSerializeParams.hasOne() && Type.subTypeOf(sSerializeParams.getItemType(), Type.NODE)) {
+ SerializerUtils.getSerializationOptions(this, (NodeValue) sSerializeParams.itemAt(0), outputProperties);
+ } else {
+ SequenceIterator siSerializeParams = sSerializeParams.iterate();
while(siSerializeParams.hasNext()) {
final String serializeParam = siSerializeParams.nextItem().getStringValue();
final String opt[] = Option.parseKeyValuePair(serializeParam);
@@ -200,8 +208,8 @@ private Properties parseXMLSerializationOptions(final SequenceIterator siSeriali
outputProperties.setProperty( opt[0], opt[1] );
}
}
-
- return outputProperties;
+ }
+ return outputProperties;
}
View
30 src/org/exist/xquery/functions/fn/FunSerialize.java
@@ -9,6 +9,7 @@
import org.exist.util.serializer.SAXSerializer;
import org.exist.util.serializer.SerializerPool;
import org.exist.xquery.*;
+import org.exist.xquery.util.SerializerUtils;
import org.exist.xquery.value.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -53,8 +54,9 @@ public FunSerialize(XQueryContext context, FunctionSignature signature) {
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final Properties outputProperties = new Properties();
- if (getArgumentCount() == 2 && !args[1].isEmpty())
- {parseParameters((NodeValue) args[1].itemAt(0), outputProperties);}
+ if (getArgumentCount() == 2 && !args[1].isEmpty()) {
+ SerializerUtils.getSerializationOptions(this, (NodeValue) args[1].itemAt(0), outputProperties);
+ }
final StringBuilder out = new StringBuilder();
@@ -80,30 +82,6 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
}
}
- protected void parseParameters(NodeValue parameters, Properties properties) throws XPathException {
- try {
- final XMLStreamReader reader = context.getXMLStreamReader(parameters);
- while (reader.hasNext() && (reader.next() != XMLStreamReader.START_ELEMENT)) {
- }
- if (!reader.getNamespaceURI().equals(Namespaces.XSLT_XQUERY_SERIALIZATION_NS)) {
- throw new XPathException(this, FnModule.SENR0001, "serialization parameter elements should be in the output namespace");
- }
- while (reader.hasNext()) {
- final int status = reader.next();
- if (status == XMLStreamReader.START_ELEMENT) {
- final String key = reader.getLocalName();
- if (properties.contains(key))
- {throw new XPathException(this, FnModule.SEPM0019, "serialization parameter specified twice: " + key);}
- properties.put(key, reader.getElementText());
- }
- }
- } catch (final XMLStreamException e) {
- throw new XPathException(ErrorCodes.EXXQDY0001, e.getMessage());
- } catch (final IOException e) {
- throw new XPathException(ErrorCodes.EXXQDY0001, e.getMessage());
- }
- }
-
/**
* Sequence normalization as described in
* http://www.w3.org/TR/xslt-xquery-serialization-30/#serdm
View
36 src/org/exist/xquery/functions/util/Serialize.java
@@ -45,6 +45,7 @@
import org.exist.xquery.Option;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
+import org.exist.xquery.util.SerializerUtils;
import org.exist.xquery.value.BooleanValue;
import org.exist.xquery.value.FunctionParameterSequenceType;
import org.exist.xquery.value.NodeValue;
@@ -53,6 +54,7 @@
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.StringValue;
import org.exist.xquery.value.Type;
+import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class Serialize extends BasicFunction {
@@ -87,7 +89,9 @@
"called from.",
new SequenceType[] {
new FunctionParameterSequenceType("node-set", Type.NODE, Cardinality.ZERO_OR_MORE, "The node set to serialize"),
- new FunctionParameterSequenceType("parameters", Type.STRING, Cardinality.ZERO_OR_MORE, "The serialization parameters")
+ new FunctionParameterSequenceType("parameters", Type.ITEM, Cardinality.ZERO_OR_MORE,
+ "The serialization parameters: either a sequence of key=value pairs or an output:serialization-parameters " +
+ "element as defined by the standard fn:serialize function.")
},
new FunctionParameterSequenceType("result", Type.STRING, Cardinality.ZERO_OR_ONE, "the string containing the serialized node set.")
)
@@ -127,7 +131,7 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
}
//parse serialization options from third argument to function
- outputProperties = parseSerializationOptions(args[2].iterate());
+ outputProperties = parseSerializationOptions(args[2]);
//setup output stream for file
try
@@ -149,7 +153,7 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
/** serialize to string **/
//parse serialization options from second argument to function
- outputProperties = parseSerializationOptions(args[1].iterate());
+ outputProperties = parseSerializationOptions(args[1]);
//setup output stream for byte array
os = new ByteArrayOutputStream();
@@ -170,23 +174,27 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
}
- private Properties parseSerializationOptions(SequenceIterator siSerializeParams) throws XPathException
+ private Properties parseSerializationOptions(Sequence sSerializeParams) throws XPathException
{
//parse serialization options
final Properties outputProperties = new Properties();
- outputProperties.setProperty(OutputKeys.INDENT, "yes");
- outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- while(siSerializeParams.hasNext())
- {
- final String serializeParams = siSerializeParams.nextItem().getStringValue();
- final String params[] = serializeParams.split(" ");
- for(final String param : params)
+ if (sSerializeParams.hasOne() && Type.subTypeOf(sSerializeParams.getItemType(), Type.NODE)) {
+ SerializerUtils.getSerializationOptions(this, (NodeValue) sSerializeParams.itemAt(0), outputProperties);
+ } else {
+ SequenceIterator siSerializeParams = sSerializeParams.iterate();
+ outputProperties.setProperty(OutputKeys.INDENT, "yes");
+ outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ while(siSerializeParams.hasNext())
{
- final String opt[] = Option.parseKeyValuePair(param);
- outputProperties.setProperty(opt[0], opt[1]);
+ final String serializeParams = siSerializeParams.nextItem().getStringValue();
+ final String params[] = serializeParams.split(" ");
+ for(final String param : params)
+ {
+ final String opt[] = Option.parseKeyValuePair(param);
+ outputProperties.setProperty(opt[0], opt[1]);
+ }
}
}
-
return outputProperties;
}
View
2  src/org/exist/xquery/lib/test.xq
@@ -112,7 +112,7 @@ declare function t:run-test($test as element(test), $count as xs:integer) {
if ($output instance of element(error)) then
$output
else if ($test/@serialize) then
- let $options := $test/@serialize
+ let $options := $test/@serialize/string()
let $serialized :=
util:catch("*",
for $x in $output
View
56 src/org/exist/xquery/util/SerializerUtils.java
@@ -0,0 +1,56 @@
+package org.exist.xquery.util;
+
+import org.exist.Namespaces;
+import org.exist.xquery.ErrorCodes;
+import org.exist.xquery.Expression;
+import org.exist.xquery.XPathException;
+import org.exist.xquery.functions.fn.FnModule;
+import org.exist.xquery.value.NodeValue;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import java.io.IOException;
+import java.util.Properties;
+
+/**
+ * Serializer utilities used by several XQuery functions.
+ */
+public class SerializerUtils {
+
+ /**
+ * Parse output:serialization-parameters XML fragment into serialization
+ * properties as defined by the fn:serialize function.
+ *
+ * @param parent the parent expression calling this method
+ * @param parameters root node of the XML fragment
+ * @param properties parameters are added to the given properties
+ */
+ public static void getSerializationOptions(Expression parent, NodeValue parameters, Properties properties) throws XPathException {
+ try {
+ final XMLStreamReader reader = parent.getContext().getXMLStreamReader(parameters);
+ while (reader.hasNext() && (reader.next() != XMLStreamReader.START_ELEMENT)) {
+ }
+ if (!reader.getNamespaceURI().equals(Namespaces.XSLT_XQUERY_SERIALIZATION_NS)) {
+ throw new XPathException(parent, FnModule.SENR0001, "serialization parameter elements should be in the output namespace");
+ }
+ while (reader.hasNext()) {
+ final int status = reader.next();
+ if (status == XMLStreamReader.START_ELEMENT) {
+ final String key = reader.getLocalName();
+ if (properties.contains(key))
+ {throw new XPathException(parent, FnModule.SEPM0019, "serialization parameter specified twice: " + key);}
+ String value = reader.getAttributeValue("", "value");
+ if (value == null) {
+ // backwards compatibility: use element text as value
+ value = reader.getElementText();
+ }
+ properties.put(key, value);
+ }
+ }
+ } catch (final XMLStreamException e) {
+ throw new XPathException(parent, ErrorCodes.EXXQDY0001, e.getMessage());
+ } catch (final IOException e) {
+ throw new XPathException(parent, ErrorCodes.EXXQDY0001, e.getMessage());
+ }
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.