org.semanticdesktop.aperture.rdf.util
Class XmlSafetyUtils

java.lang.Object
  extended by org.semanticdesktop.aperture.rdf.util.XmlSafetyUtils

public class XmlSafetyUtils
extends Object

A set of utility-methods for dealing with XML-safety.

The XML specification states that all characters in a valid XML document must be taken from a certain set. Defined at:

http://www.w3.org/TR/2006/REC-xml-20060816/#charsets The methods of this class help remove invalid characters from the output generated by Aperture components.


Constructor Summary
XmlSafetyUtils()
           
 
Method Summary
static char[] makeXmlSafe(char[] s)
          Removes all non-valid XML character data chars from the specified char array.
static char[] makeXmlSafe(char[] s, int off, int len)
          Removes all non-valid XML character data chars from the specified char array and substitutes them with spaces.
static Node makeXmlSafe(Model model, Node node)
          Returns an XML-safe version of the given node.
static Statement makeXmlSafe(Model model, Statement stmt)
          Returns a statement with the same subject and predicate, but with an XML-safe object.
static String makeXmlSafe(String s)
          Strips all invalid XML Character data chars from the given string.
static String makeXmlSafe(String in, int off, int len)
          Strips all invalid XML Character data chars from the given string.
static Model wrapXmlSafeModel(Model model)
          Wraps the given model in a wrapper that ensures that all data passed into this model is free of any characters that are invalid in XML.
static RDFContainer wrapXmlSafeRDFContainer(RDFContainer container)
          Returns an RDFContainer backed by the same model as the given one, describing the same URI as the given one but ensuring that all the data passed into the model is XML-safe i.e. it doesn't conatain any characters that are invalid in XML.
static RDFContainerFactory wrapXmlSafeRDFContainerFactory(RDFContainerFactory factory)
          Returns a wrapper around the given factory that guarantees that all the rdf containers created by that factory will be XML-safe
static Reader wrapXmlSafeReader(Reader reader)
          Wraps the given reader in a filter that ensures that no XML-invalid characters are actually read.
static Writer wrapXmlSafeWriter(Writer writer)
          Wraps the given writer in a filter that ensures that no XML-invalid characters are actually written.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlSafetyUtils

public XmlSafetyUtils()
Method Detail

makeXmlSafe

public static String makeXmlSafe(String s)
Strips all invalid XML Character data chars from the given string. If all characters are valid character data chars, this method will return the same String.

This method has been copy-pasted from: here and modified slightly. The original version removed the xml-invalid chars altogether this version substitutes them with spaces

Parameters:
s - the input string
Returns:
the input string with all characters that are invalid in xml removed

makeXmlSafe

public static String makeXmlSafe(String in,
                                 int off,
                                 int len)
Strips all invalid XML Character data chars from the given string. It starts the checking at the given offset and checks the substring of the given length. If all characters are valid character data chars, this method will return the same String. If any invalid characters are encountered, this method returns a substring. The content of the returned substring is taken from the original string, starting at the offset off, continuing for len characters, with all XML-invalid characters substituted with spaces.

Parameters:
in - the input string
off - the offset where the validity checking should begin
len - the length of the substring to be checked
Returns:
the required substring with all XML-invalid characters substituted with spaces

makeXmlSafe

public static char[] makeXmlSafe(char[] s)
Removes all non-valid XML character data chars from the specified char array. If all characters are valid XML character data chars, this method will return the same array. If the method encounters any invalid characters it will return a new array whose contents will be copied from s, with the invalid characters substituted with spaces

Parameters:
s - the array to be checked
Returns:
an array whose content is taken from s, with all XML-invalid characters substituted with spaces

makeXmlSafe

public static char[] makeXmlSafe(char[] s,
                                 int off,
                                 int len)
Removes all non-valid XML character data chars from the specified char array and substitutes them with spaces. If all characters are valid character data chars, this method will return the same array. If the method encounters any invalid characters it will return a portion cut from the input array, beginning with the offset off, whose length will be equal to len.

Parameters:
s - the array to be checked
off - the offset where checking should begin
len - of the sub-array to be checked
Returns:
an array whose content is taken from s, with all XML-invalid characters substituted with spaces

makeXmlSafe

public static Node makeXmlSafe(Model model,
                               Node node)
Returns an XML-safe version of the given node. It checks whether the node is an literal, converts the value of that literal to an XML-safe version, constructs a new Literal object and returns it. If no XML-invalid characters are found, the original object is returned. Other kinds of nodes (uris and blank nodes) are always returned without any changes

Parameters:
model - the model used as the literal factory, in case the method needs to construct a new XML-safe object
node - the node to be checked
Returns:
for URIs, blank nodes and literals without any XML-invalid characters, this method returns the node passed as the node argument. If the given node is a literal and it has some XML-invalid characters, a new, XML-safe version is returned

makeXmlSafe

public static Statement makeXmlSafe(Model model,
                                    Statement stmt)
Returns a statement with the same subject and predicate, but with an XML-safe object. If no problems are detected, this method returns the same statement.

Parameters:
model - the model used as the literal factory, in case the method needs to construct a new XML-safe object
stmt - the statement to be checked
Returns:
a statement with the same subject and predicate, but with an XML-safe object. If no problems are detected, this method returns the same statement.

wrapXmlSafeWriter

public static Writer wrapXmlSafeWriter(Writer writer)
Wraps the given writer in a filter that ensures that no XML-invalid characters are actually written.

Parameters:
writer -
Returns:
an XML-safe version of the writer

wrapXmlSafeReader

public static Reader wrapXmlSafeReader(Reader reader)
Wraps the given reader in a filter that ensures that no XML-invalid characters are actually read.

Parameters:
reader -
Returns:
an XML-safe version of the reader

wrapXmlSafeModel

public static Model wrapXmlSafeModel(Model model)
Wraps the given model in a wrapper that ensures that all data passed into this model is free of any characters that are invalid in XML.

Note that the current implementation handles the add... methods and the methods that read from readers. Id doesn't handle the update methods AbstractModel#update(Diff) and AbstractModel.update(org.ontoware.rdf2go.model.DiffReader) and the methods that read from input streams i.e. ModelIO.readFrom(java.io.InputStream), ModelIO.readFrom(java.io.InputStream, Syntax), ModelIO.readFrom(java.io.InputStream, Syntax, String). So it's still possible for XML-invalid characters to sneak in via these channels. The user is advised to take care about this.

Parameters:
model -
Returns:
an xml-safe version of the given model

wrapXmlSafeRDFContainer

public static RDFContainer wrapXmlSafeRDFContainer(RDFContainer container)
Returns an RDFContainer backed by the same model as the given one, describing the same URI as the given one but ensuring that all the data passed into the model is XML-safe i.e. it doesn't conatain any characters that are invalid in XML.

Parameters:
container - the container to wrap
Returns:
an XML-safe version of the given RDFContainer.

wrapXmlSafeRDFContainerFactory

public static RDFContainerFactory wrapXmlSafeRDFContainerFactory(RDFContainerFactory factory)
Returns a wrapper around the given factory that guarantees that all the rdf containers created by that factory will be XML-safe

Parameters:
factory -
Returns:
and xml-safe versionof the RDFContainerFactory


Copyright © 2010 Aperture Development Team. All Rights Reserved.