public class ConfigurableXMLRewriter extends AbstractRewriter implements ILinkRewriter
Modifier and Type | Class and Description |
---|---|
static class |
ConfigurableXMLRewriter.DefaultMergeLinkAdjuster
A default implementation of
ConfigurableXMLRewriter.IMergeLinkAdjuster , intended for use
with TextGrid URIs. |
static interface |
ConfigurableXMLRewriter.IMergeLinkAdjuster
When using merge mode, users must
supply an implementation of this interface to adjust links and IDs such
that all IDs in the documents to merge are unique.
|
protected class |
ConfigurableXMLRewriter.Replacer
implementers must implement
ConfigurableXMLRewriter.Replacer.apply(String) and can use the other
methods |
Constructor and Description |
---|
ConfigurableXMLRewriter(ImportMapping mapping,
boolean export) |
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> |
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.
|
static URI |
relativize(URI base,
URI child)
From StackOverflow user Alex, http://stackoverflow.com/a/10802188/1719996
#12790
|
protected String |
replaceAttributeValue(StartElement element,
Attribute attribute)
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(StartElement startElement,
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)
|
void |
rewrite(XMLEventReader reader,
XMLEventWriter writer) |
void |
setBase(URI base)
Use the given base for the local links.
|
void |
setMergeLinkAdjuster(ConfigurableXMLRewriter.IMergeLinkAdjuster adjuster)
Configures this rewriter for merge
mode using the given adjuster to rewrite links.
|
void |
setMergeMode(boolean mergeMode)
Turns merge mode on or off.
|
getImportMode, getMapping, isExport
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getImportMode, getMapping, isExport
public ConfigurableXMLRewriter(ImportMapping mapping, boolean export)
public URI getBase()
setBase(URI)
, or
null
if no base handling will take place.setBase(URI)
public void setBase(URI base)
foo.xml sub +--bar.xml +--baz.xml quux.xmlNow, 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.base
- the base URI used from now on in
rewrite(InputStream, OutputStream)
operations or
null
(the default) to switch this feature off.public static URI relativize(URI base, URI child)
public void configure(XmlConfiguration configuration)
configuration
- public void configure(URI uri) throws IllegalArgumentException
uri
- an URI or URI fragment identifying the configuration to load.
#foo
internal:textgrid#aggregation
IllegalArgumentException
- if the given URI cannot be resolved to a valid configuration.configure(XmlConfiguration)
,
XmlConfiguration
public static Map<URI,XmlConfiguration> getInternalConfigurations()
protected ConfigurableXMLRewriter.Replacer getReplacer(ReplaceMethod method)
protected String replaceElementContent(StartElement startElement, String content)
startElement
- The name of the element in which the content occurs.content
- (coalesced) character content if the specified element.protected String replaceAttributeValue(StartElement element, Attribute attribute)
element
- Name of the current elementattribute
- Name of the attributevalue
- value of the attribute to rewritepublic void rewrite(InputStream input, OutputStream output) throws IOException, XMLStreamException
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.
This method will create a parser on the input stream and a serializer on
the output and then call rewrite(XMLEventReader, XMLEventWriter)
. It will not close the streams you pass in.
rewrite
in interface ILinkRewriter
rewrite
in class AbstractRewriter
input
- the stream from which the rewriter readsoutput
- the stream to which the rewriter writes the rewritten codeIOException
- if reading or writing failsXMLStreamException
- on fatal errors during XML processing.public void rewrite(XMLEventReader reader, XMLEventWriter writer) throws FactoryConfigurationError, XMLStreamException
protected static <T extends NodeRewriteSpec> T find(Iterable<T> iterable, QName name) throws NoSuchElementException
NoSuchElementException
public ConfigurableXMLRewriter recordMissingReferences()
URI
s but that are not contained in
the ImportMapping
.public Set<String> getMissingReferences()
null
if nothing has been
recorded.recordMissingReferences()
public void setMergeMode(boolean mergeMode)
public void setMergeLinkAdjuster(ConfigurableXMLRewriter.IMergeLinkAdjuster adjuster)
adjuster
- An adjuster that rewrites IDs and links.Copyright © 2018 TextGrid. All rights reserved.