Skip to content
Snippets Groups Projects
Commit 8d2a544e authored by Emmanuel Bourg's avatar Emmanuel Bourg
Browse files

Imported Upstream version 1.1.19

parent a954a349
No related branches found
No related tags found
No related merge requests found
Showing
with 149 additions and 22 deletions
......@@ -10,7 +10,7 @@
<groupId>net.sf.practicalxml</groupId>
<artifactId>practicalxml</artifactId>
<version>1.1.18</version>
<version>1.1.19</version>
<name>practicalxml</name>
<packaging>jar</packaging>
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......@@ -19,6 +19,17 @@ import net.sf.practicalxml.builder.ElementNode;
import static net.sf.practicalxml.builder.XmlBuilder.*;
/**
* Example of constructing an XML document using the <code>XmlBuilder</code> API.
* All builder methods are statically imported; each produces a <code>Node</code>
* object that can be used as an argument for other functions in the API.
* <p>
* Note that the produced <code>ElementNode</code> does <em>not</em> implement any
* part of the JDK XML API. If you want to manipulate it as a DOM, for example, you
* must explicitly call <code>toDOM()</code>. This happens behind the scenes in the
* call to <code>toString()</code>: the node tree is converted to a DOM, which then
* passes through <code>OutputUtil</code> to produce the indented string format.
*/
public class BuilderExample
{
public static void main(String[] argv)
......
// Copyright 2008-2014 severally by the contributors
//
// 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.sf.practicalxml.example;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPathFunction;
import javax.xml.xpath.XPathFunctionException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import net.sf.practicalxml.ParseUtil;
import net.sf.practicalxml.xpath.XPathWrapper;
/**
* Example of using XPathFactory, including namespace and function bindings.
*/
public class XPathExample
{
/**
* Some sample XML. Note that all elements are namespaced, but do not use qualified names.
*/
private final static String xml = "<root xmlns='urn:X-EXAMPLE'>"
+ "<entry id='1' code='A'>foo</entry>"
+ "<entry id='2' code='A'>bar</entry>"
+ "<entry id='3' code='C'>baz</entry>"
+ "<entry id='4' code='E'>argle</entry>"
+ "<entry id='4' code='E'>bargle</entry>"
+ "</root>";
/**
* An XPath function that tests a single node's value against an inclusive string-valued
* range (useful because XPath 1.0 does not support relational operations against strings).
* <p>
* This implementation uses the standard XPathFunction API, and makes assumptions about the
* types of its argument. For real-world functions, I recommend starting from
* <code>net.sf.practicalxml.xpath.AbstractFunction</code>, which uses a Template Method
* approach to the boilerplate of argument processing.
*/
private static class XPathRange implements XPathFunction
{
@SuppressWarnings("rawtypes")
public Object evaluate(List args) throws XPathFunctionException
{
try
{
java.util.Iterator argItx = args.iterator();
NodeList selection = (NodeList)argItx.next();
String lowerBound = (String)argItx.next();
String upperBound = (String)argItx.next();
String value = (selection.getLength() > 0) ? selection.item(0).getNodeValue() : "";
return (value.compareTo(lowerBound) >= 0) && (value.compareTo(upperBound) <= 0);
}
catch (Exception ex)
{
throw new XPathFunctionException(ex);
}
}
}
public static void main(String[] argv) throws Exception
{
Document dom = ParseUtil.parse(xml);
// selects a single element using an attribute test
List<Element> selection1 = new XPathWrapper("//ns:entry[@code='C']")
.bindNamespace("ns", "urn:X-EXAMPLE")
.evaluate(dom, Element.class);
System.out.println("nodes selected by simple test = " + selection1.size());
// selects the same element using a function -- note the second namespace binding
List<Element> selection2 = new XPathWrapper("//ns:entry[fn:inRange(@code, 'B', 'D')]")
.bindNamespace("ns", "urn:X-EXAMPLE")
.bindNamespace("fn", "urn:X-EXAMPLE-FN")
.bindFunction(new QName("urn:X-EXAMPLE-FN", "inRange"), new XPathRange(), 3)
.evaluate(dom, Element.class);
System.out.println("nodes selected by function = " + selection2.size());
}
}
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......@@ -225,7 +225,7 @@ public class ParseUtil
*/
public static Document parseFromClasspath(String resourcePath)
{
return parseFromClasspath(Thread.currentThread().getContextClassLoader(), resourcePath, new ExceptionErrorHandler());
return parseFromClasspath(resourcePath, new ExceptionErrorHandler());
}
......@@ -245,7 +245,7 @@ public class ParseUtil
*/
public static Document parseFromClasspath(String resourcePath, Class<?> klass)
{
return parseFromClasspath(klass.getClassLoader(), resourcePath, new ExceptionErrorHandler());
return parseFromClasspath(resourcePath, new ExceptionErrorHandler());
}
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......@@ -14,12 +14,15 @@
package net.sf.practicalxml;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import net.sf.kdgcommons.lang.StringUtil;
......@@ -161,6 +164,21 @@ public class XmlUtil
return getXsdDecimalFormatter().format(value);
}
/**
* Parses an XML Schema <code>decimal</code> value. Note that the return
* type is <code>BigDecimal</code>: this provides us exact representation
* where necessary, and meets the spec's minimum requirement of 18 digits
* of precision. Use <code>.toLong()</code>, <code>toDouble()</code>, or
* equivalent to create the type that you want.
*
* @since 1.1.19
*/
public static BigDecimal parseXsdDecimal(String value)
{
// note: this constructor parses in a locale-independent format
return new BigDecimal(value.trim());
}
/**
* Converts a <code>boolean</code> value to the literal strings "true" or
......@@ -366,7 +384,8 @@ public class XmlUtil
DecimalFormat format = _xsdDecimalFormatter.get();
if (format == null)
{
format = new DecimalFormat("#0.0################;-#");
format = new DecimalFormat("#0.0################;-#",
new DecimalFormatSymbols(Locale.US));
_xsdDecimalFormatter.set(format);
}
return format;
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
// Copyright 2008-2012 severally by the contributors
// Copyright 2008-2014 severally by the contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment