info.textgrid.utils.linkrewriter
Class ConfigurableXMLRewriter

java.lang.Object
  extended by info.textgrid.utils.linkrewriter.AbstractRewriter
      extended by info.textgrid.utils.linkrewriter.ConfigurableXMLRewriter
All Implemented Interfaces:
ILinkRewriter

public class ConfigurableXMLRewriter
extends AbstractRewriter
implements ILinkRewriter

The configurable XML rewriter allows you to configure where in an XML file link rewriting may occur. You can specify where rewriting will occur and what kind of rewriting will occur there.

Where to rewrite

You can specify a list of element names (QNames, that is). For each element name you can specify a rewrite method for the direct text contents and for a list of attribute names. For element and attribute names, we do recognize the special value '*' for any which will match any name in any namespace.

How to rewrite

none (default)
No rewriting will occur
full
rewriting only if the full contents (stripped of surrounding whitespace and a possible fragment) matches
token
each token (delimited by whitespace) in here will be looked at


Nested Class Summary
Modifier and Type Class and Description
protected  class ConfigurableXMLRewriter.Replacer
          implementers must implement ConfigurableXMLRewriter.Replacer.apply(String) and can use the other methods
 
Constructor Summary
Constructor and Description
ConfigurableXMLRewriter(ImportMapping mapping, boolean export)
           
 
Method Summary
Modifier and Type Method and Description
 void configure(URI uri)
          Configures this XML rewriter for the configuration identified by the given URI.
 void configure(XmlConfiguration configuration)
          Configures this rewriter for the given configuration.
protected static
<T extends NodeRewriteSpec>
T
find(Iterable<T> iterable, QName name)
           
 URI getBase()
          Returns the currently active base URI.
static Map<URI,XmlConfiguration> getInternalConfigurations()
           
static List<String> getInternalSpecs()
           
 Set<String> getMissingReferences()
           
protected  ConfigurableXMLRewriter.Replacer getReplacer(ReplaceMethod method)
           
 ConfigurableXMLRewriter recordMissingReferences()
          After calls to this function the rewriter will record links to missing references, i.e.
protected  String replaceAttributeValue(QName element, QName attribute, String value)
          Helper method that correctly applies the configured replacement to the given attribute in the given element according to the rewriter's current configuration.
protected  String replaceElementContent(QName element, String content)
          Helper method that correctly applies the configured replacement to the given content according to te given element.
 void rewrite(InputStream input, OutputStream output)
          Copies contents from input to output, rewriting links according to the mapping and the export setting, in an implementation dependent manner.
 void setBase(URI base)
          Use the given base for the local links.
 
Methods inherited from class info.textgrid.utils.linkrewriter.AbstractRewriter
getMapping, isExport
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface info.textgrid.utils.linkrewriter.ILinkRewriter
getMapping, isExport
 

Constructor Detail

ConfigurableXMLRewriter

public ConfigurableXMLRewriter(ImportMapping mapping,
                               boolean export)
Method Detail

getBase

public URI getBase()
Returns the currently active base URI.

Returns:
the current base URI as set by setBase(URI), or null if no base handling will take place.
See Also:
setBase(URI)

setBase

public void setBase(URI base)
Use the given base for the local links. Assume you create the following structure on the local filesystem:
 foo.xml
 sub
 +--bar.xml
 +--baz.xml
 quux.xml
 
Now, in your import mapping you will have sub/bar.xml and sub/baz.xml. However, a link from bar.xml to baz.xml will be something like <ref target="baz.xml">Baz</ref>. The solution is to call setBase(URI.create("sub/")) before rewriting bar.xml and baz.xml. This will let the rewriter add the given prefix before looking for matches in the import case, and it will relativize the URIs against the base in the export case.

Parameters:
base - the base URI used from now on in rewrite(InputStream, OutputStream) operations or null (the default) to switch this feature off.

configure

public void configure(XmlConfiguration configuration)
Configures this rewriter for the given configuration. Note that it does not reconfigure, i.e., if you need an XML rewriter with a new configuration, create a new XML rewriter.

Parameters:
configuration -

configure

public void configure(URI uri)
               throws IllegalArgumentException
Configures this XML rewriter for the configuration identified by the given URI.

Parameters:
uri - an URI or URI fragment identifying the configuration to load.
fragment URI only, e.g., #foo
Configuration with the ID foo from the current import spec.
internal URI, e.g., internal:textgrid#aggregation
One of the predefined configurations.
URL
Only theoretically supported yet
TextGrid URI
not supported yet
Throws:
IllegalArgumentException - if the given URI cannot be resolved to a valid configuration.
See Also:
configure(XmlConfiguration), XmlConfiguration

getInternalSpecs

public static List<String> getInternalSpecs()

getInternalConfigurations

public static Map<URI,XmlConfiguration> getInternalConfigurations()

getReplacer

protected ConfigurableXMLRewriter.Replacer getReplacer(ReplaceMethod method)

replaceElementContent

protected String replaceElementContent(QName element,
                                       String content)
Helper method that correctly applies the configured replacement to the given content according to te given element.

Parameters:
element - The name of the element in which the content occurs.
content - (coalesced) character content if the specified element.
Returns:
either content with replacements applied, or content as is.

replaceAttributeValue

protected String replaceAttributeValue(QName element,
                                       QName attribute,
                                       String value)
Helper method that correctly applies the configured replacement to the given attribute in the given element according to the rewriter's current configuration.

Parameters:
element - Name of the current element
attribute - Name of the attribute
value - value of the attribute to rewrite
Returns:
value, either as is or with replacements applied according to the configuration.

rewrite

public void rewrite(InputStream input,
                    OutputStream output)
             throws IOException,
                    XMLStreamException
Copies contents from input to output, rewriting links according to the mapping and the export setting, in an implementation dependent manner.

This rewriter will rewrite according to the configuration specified using this class's other methods. See also the class documentation.

The rewriter will try to detect the input document's encoding. It will always serialize to an UTF-8 encoded XML document. It will fail on non-wellformed documents.

Specified by:
rewrite in interface ILinkRewriter
Specified by:
rewrite in class AbstractRewriter
Parameters:
input - the stream from which the rewriter reads
output - the stream to which the rewriter writes the rewritten code
Throws:
IOException - if reading or writing fails
XMLStreamException - on fatal errors during XML processing.

find

protected static <T extends NodeRewriteSpec> T find(Iterable<T> iterable,
                                                    QName name)
                                         throws NoSuchElementException
Throws:
NoSuchElementException

recordMissingReferences

public ConfigurableXMLRewriter recordMissingReferences()
After calls to this function the rewriter will record links to missing references, i.e. to URIs (sans fragment) that have been identified as links according to the configuration, that are valid URIs but that are not contained in the ImportMapping.


getMissingReferences

public Set<String> getMissingReferences()
Returns:
an immutable set containing the URIs of the missing resources (sans fragment), or null if nothing has been recorded.
See Also:
recordMissingReferences()


Copyright © 2012 TextGrid. All Rights Reserved.