public class XmlPatcher extends Object implements javax.xml.stream.XMLEventReader
XmlPatcher uses StAX API and provides an API for micro-patching XML file
(such as changing text, adding/removing a tag, etc.) without altering the original XML file unnecessarily
as much as possible. For example, comments, whitespaces between attributes, indentation, and so on are kept intact.
The patching of XML file is primarily done via Mark, which represents a range of text in XML.
Mark can be created from mark(), or by combining existing marks (such as Mark.to(Mark).
The caller can retrieve the text inside Mark and replace them.
Active Marks are tracked by XmlPatcher so that when one mark changes its content, the positions
of the other marks are updated to stick to the text they are supposed to be pointing. Because of this, the caller
should stick to a bounded number of marks, or use Mark.clear() to throw away unneeded marks.
While this class intends to provide high-fidelity and preserves as much of the original XML as possible,
the StAX API (JSR-173) has inherent limitation. For example, the StAX API specifies that CR
characters will be stripped. Current implementations do not keep " and ' characters consistent.
| Constructor and Description |
|---|
XmlPatcher(File f) |
XmlPatcher(InputStream in) |
XmlPatcher(StringBuilder xml) |
| Modifier and Type | Method and Description |
|---|---|
StringBuilder |
asStringBuilder()
Returns a copy of the backing string buffer.
|
void |
close() |
String |
getBetween(Mark a,
Mark b) |
String |
getElementText() |
Mark |
getLast()
Returns a mark that points to the last returned
XMLEvent |
XmlPath |
getPath()
Gets the current location in the XML document.
|
String |
getPeekVerbatim()
Returns the verbatim text of the element returned by
peek(). |
Object |
getProperty(String name) |
boolean |
hasNext() |
boolean |
isModified()
Getter for property 'modified'.
|
Mark |
mark()
Creates a new mark.
|
Object |
next() |
javax.xml.stream.events.XMLEvent |
nextEvent() |
javax.xml.stream.events.XMLEvent |
nextTag() |
javax.xml.stream.events.XMLEvent |
peek() |
void |
remove() |
void |
replace(String replacement)
Replaces the current element with the replacement text.
|
void |
replaceBetween(Mark a,
Mark b,
String replacement)
Replaces all content between two marks with the replacement text.
|
void |
rewind()
Rewind to the start so we can run through again.
|
boolean |
scan(XmlVisitor v) |
void |
setFactory(javax.xml.stream.XMLInputFactory factory) |
void |
writeTo(File f) |
public XmlPatcher(StringBuilder xml)
public XmlPatcher(File f) throws IOException
IOExceptionpublic XmlPatcher(InputStream in) throws IOException
IOExceptionpublic void setFactory(javax.xml.stream.XMLInputFactory factory)
public void rewind()
throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException - when things go wrong.public XmlPath getPath()
public boolean scan(XmlVisitor v) throws javax.xml.stream.XMLStreamException
public boolean isModified()
public javax.xml.stream.events.XMLEvent nextEvent() throws javax.xml.stream.XMLStreamException
nextEvent in interface javax.xml.stream.XMLEventReaderjavax.xml.stream.XMLStreamExceptionpublic javax.xml.stream.events.XMLEvent peek() throws javax.xml.stream.XMLStreamException
peek in interface javax.xml.stream.XMLEventReaderjavax.xml.stream.XMLStreamExceptionpublic String getElementText() throws javax.xml.stream.XMLStreamException
getElementText in interface javax.xml.stream.XMLEventReaderjavax.xml.stream.XMLStreamExceptionpublic javax.xml.stream.events.XMLEvent nextTag() throws javax.xml.stream.XMLStreamException
nextTag in interface javax.xml.stream.XMLEventReaderjavax.xml.stream.XMLStreamExceptionpublic Object getProperty(String name)
getProperty in interface javax.xml.stream.XMLEventReaderpublic void close()
throws javax.xml.stream.XMLStreamException
close in interface javax.xml.stream.XMLEventReaderjavax.xml.stream.XMLStreamExceptionpublic StringBuilder asStringBuilder()
public String getPeekVerbatim()
peek().peek().public boolean hasNext()
hasNext in interface IteratorhasNext in interface javax.xml.stream.XMLEventReaderpublic Mark mark()
public void replace(String replacement)
replacement - The replacement.public void replaceBetween(Mark a, Mark b, String replacement)
replacement - The replacement.public void writeTo(File f) throws IOException
IOExceptionCopyright © 2013. All Rights Reserved.