| Method from org.dom4j.io.XPP3Reader Detail: |
public void addHandler(String path,
ElementHandler handler) {
getDispatchHandler().addHandler(path, handler);
}
Adds the ElementHandler to be called when the specified
path is encounted. |
protected Reader createReader(InputStream in) throws IOException {
return new BufferedReader(new InputStreamReader(in));
}
Factory method to create a Reader from the given InputStream. |
protected DispatchHandler getDispatchHandler() {
if (dispatchHandler == null) {
dispatchHandler = new DispatchHandler();
}
return dispatchHandler;
}
|
public DocumentFactory getDocumentFactory() {
if (factory == null) {
factory = DocumentFactory.getInstance();
}
return factory;
}
|
public XmlPullParserFactory getXPPFactory() throws XmlPullParserException {
if (xppFactory == null) {
xppFactory = XmlPullParserFactory.newInstance();
}
xppFactory.setNamespaceAware(true);
return xppFactory;
}
|
public XmlPullParser getXPPParser() throws XmlPullParserException {
if (xppParser == null) {
xppParser = getXPPFactory().newPullParser();
}
return xppParser;
}
|
protected Document parseDocument() throws IOException, DocumentException, XmlPullParserException {
DocumentFactory df = getDocumentFactory();
Document document = df.createDocument();
Element parent = null;
XmlPullParser pp = getXPPParser();
pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
while (true) {
int type = pp.nextToken();
switch (type) {
case XmlPullParser.PROCESSING_INSTRUCTION: {
String text = pp.getText();
int loc = text.indexOf(" ");
if (loc >= 0) {
String target = text.substring(0, loc);
String txt = text.substring(loc + 1);
document.addProcessingInstruction(target, txt);
} else {
document.addProcessingInstruction(text, "");
}
break;
}
case XmlPullParser.COMMENT: {
if (parent != null) {
parent.addComment(pp.getText());
} else {
document.addComment(pp.getText());
}
break;
}
case XmlPullParser.CDSECT: {
if (parent != null) {
parent.addCDATA(pp.getText());
} else {
String msg = "Cannot have text content outside of the "
+ "root document";
throw new DocumentException(msg);
}
break;
}
case XmlPullParser.ENTITY_REF:
break;
case XmlPullParser.END_DOCUMENT:
return document;
case XmlPullParser.START_TAG: {
QName qname = (pp.getPrefix() == null) ? df.createQName(pp
.getName(), pp.getNamespace()) : df.createQName(pp
.getName(), pp.getPrefix(), pp.getNamespace());
Element newElement = df.createElement(qname);
int nsStart = pp.getNamespaceCount(pp.getDepth() - 1);
int nsEnd = pp.getNamespaceCount(pp.getDepth());
for (int i = nsStart; i < nsEnd; i++) {
if (pp.getNamespacePrefix(i) != null) {
newElement.addNamespace(pp.getNamespacePrefix(i),
pp.getNamespaceUri(i));
}
}
for (int i = 0; i < pp.getAttributeCount(); i++) {
QName qa = (pp.getAttributePrefix(i) == null) ? df
.createQName(pp.getAttributeName(i)) : df
.createQName(pp.getAttributeName(i), pp
.getAttributePrefix(i), pp
.getAttributeNamespace(i));
newElement.addAttribute(qa, pp.getAttributeValue(i));
}
if (parent != null) {
parent.add(newElement);
} else {
document.add(newElement);
}
parent = newElement;
break;
}
case XmlPullParser.END_TAG: {
if (parent != null) {
parent = parent.getParent();
}
break;
}
case XmlPullParser.TEXT: {
String text = pp.getText();
if (parent != null) {
parent.addText(text);
} else {
String msg = "Cannot have text content outside of the "
+ "root document";
throw new DocumentException(msg);
}
break;
}
default:
break;
}
}
}
|
public Document read(File file) throws IOException, DocumentException, XmlPullParserException {
String systemID = file.getAbsolutePath();
return read(new BufferedReader(new FileReader(file)), systemID);
}
|
public Document read(URL url) throws IOException, DocumentException, XmlPullParserException {
String systemID = url.toExternalForm();
return read(createReader(url.openStream()), systemID);
}
|
public Document read(String systemID) throws IOException, DocumentException, XmlPullParserException {
if (systemID.indexOf(':") >= 0) {
// lets assume its a URL
return read(new URL(systemID));
} else {
// lets assume that we are given a file name
return read(new File(systemID));
}
}
Reads a Document from the given URL or filename.
If the systemID contains a ':' character then it is
assumed to be a URL otherwise its assumed to be a file name. If you want
finer grained control over this mechansim then please explicitly pass in
either a URL or a File instance instead of a String to denote the source of the document.
|
public Document read(InputStream in) throws IOException, DocumentException, XmlPullParserException {
return read(createReader(in));
}
|
public Document read(Reader reader) throws IOException, DocumentException, XmlPullParserException {
getXPPParser().setInput(reader);
return parseDocument();
}
|
public Document read(char[] text) throws IOException, DocumentException, XmlPullParserException {
getXPPParser().setInput(new CharArrayReader(text));
return parseDocument();
}
|
public Document read(InputStream in,
String systemID) throws IOException, DocumentException, XmlPullParserException {
return read(createReader(in), systemID);
}
|
public Document read(Reader reader,
String systemID) throws IOException, DocumentException, XmlPullParserException {
Document document = read(reader);
document.setName(systemID);
return document;
}
|
public void removeHandler(String path) {
getDispatchHandler().removeHandler(path);
}
Removes the ElementHandler from the event based processor,
for the specified path. |
public void setDefaultHandler(ElementHandler handler) {
getDispatchHandler().setDefaultHandler(handler);
}
When multiple ElementHandler instances have been
registered, this will set a default ElementHandler to be
called for any path which does NOT have a handler registered. |
protected void setDispatchHandler(DispatchHandler dispatchHandler) {
this.dispatchHandler = dispatchHandler;
}
|
public void setDocumentFactory(DocumentFactory documentFactory) {
this.factory = documentFactory;
}
This sets the DocumentFactory used to create new
documents. This method allows the building of custom DOM4J tree objects
to be implemented easily using a custom derivation of
DocumentFactory
|
public void setXPPFactory(XmlPullParserFactory xPPfactory) {
this.xppFactory = xPPfactory;
}
|