📄 saxreader.java
字号:
* </p>
*
* @param reader
* is the reader for the input
*
* @return the newly created Document instance
*
* @throws DocumentException
* if an error occurs during parsing.
*/
public Document read(Reader reader) throws DocumentException {
InputSource source = new InputSource(reader);
if (this.encoding != null) {
source.setEncoding(this.encoding);
}
return read(source);
}
/**
* <p>
* Reads a Document from the given stream using SAX
* </p>
*
* @param in
* <code>InputStream</code> to read from.
* @param systemId
* is the URI for the input
*
* @return the newly created Document instance
*
* @throws DocumentException
* if an error occurs during parsing.
*/
public Document read(InputStream in, String systemId)
throws DocumentException {
InputSource source = new InputSource(in);
source.setSystemId(systemId);
if (this.encoding != null) {
source.setEncoding(this.encoding);
}
return read(source);
}
/**
* <p>
* Reads a Document from the given <code>Reader</code> using SAX
* </p>
*
* @param reader
* is the reader for the input
* @param systemId
* is the URI for the input
*
* @return the newly created Document instance
*
* @throws DocumentException
* if an error occurs during parsing.
*/
public Document read(Reader reader, String systemId)
throws DocumentException {
InputSource source = new InputSource(reader);
source.setSystemId(systemId);
if (this.encoding != null) {
source.setEncoding(this.encoding);
}
return read(source);
}
/**
* <p>
* Reads a Document from the given <code>InputSource</code> using SAX
* </p>
*
* @param in
* <code>InputSource</code> to read from.
*
* @return the newly created Document instance
*
* @throws DocumentException
* if an error occurs during parsing.
*/
public Document read(InputSource in) throws DocumentException {
try {
XMLReader reader = getXMLReader();
reader = installXMLFilter(reader);
EntityResolver thatEntityResolver = this.entityResolver;
if (thatEntityResolver == null) {
thatEntityResolver = createDefaultEntityResolver(in
.getSystemId());
this.entityResolver = thatEntityResolver;
}
reader.setEntityResolver(thatEntityResolver);
SAXContentHandler contentHandler = createContentHandler(reader);
contentHandler.setEntityResolver(thatEntityResolver);
contentHandler.setInputSource(in);
boolean internal = isIncludeInternalDTDDeclarations();
boolean external = isIncludeExternalDTDDeclarations();
contentHandler.setIncludeInternalDTDDeclarations(internal);
contentHandler.setIncludeExternalDTDDeclarations(external);
contentHandler.setMergeAdjacentText(isMergeAdjacentText());
contentHandler.setStripWhitespaceText(isStripWhitespaceText());
contentHandler.setIgnoreComments(isIgnoreComments());
reader.setContentHandler(contentHandler);
configureReader(reader, contentHandler);
reader.parse(in);
return contentHandler.getDocument();
} catch (Exception e) {
if (e instanceof SAXParseException) {
// e.printStackTrace();
SAXParseException parseException = (SAXParseException) e;
String systemId = parseException.getSystemId();
if (systemId == null) {
systemId = "";
}
String message = "Error on line "
+ parseException.getLineNumber() + " of document "
+ systemId + " : " + parseException.getMessage();
throw new DocumentException(message, e);
} else {
throw new DocumentException(e.getMessage(), e);
}
}
}
// Properties
// -------------------------------------------------------------------------
/**
* DOCUMENT ME!
*
* @return the validation mode, true if validating will be done otherwise
* false.
*/
public boolean isValidating() {
return validating;
}
/**
* Sets the validation mode.
*
* @param validation
* indicates whether or not validation should occur.
*/
public void setValidation(boolean validation) {
this.validating = validation;
}
/**
* DOCUMENT ME!
*
* @return whether internal DTD declarations should be expanded into the
* DocumentType object or not.
*/
public boolean isIncludeInternalDTDDeclarations() {
return includeInternalDTDDeclarations;
}
/**
* Sets whether internal DTD declarations should be expanded into the
* DocumentType object or not.
*
* @param include
* whether or not DTD declarations should be expanded and
* included into the DocumentType object.
*/
public void setIncludeInternalDTDDeclarations(boolean include) {
this.includeInternalDTDDeclarations = include;
}
/**
* DOCUMENT ME!
*
* @return whether external DTD declarations should be expanded into the
* DocumentType object or not.
*/
public boolean isIncludeExternalDTDDeclarations() {
return includeExternalDTDDeclarations;
}
/**
* Sets whether DTD external declarations should be expanded into the
* DocumentType object or not.
*
* @param include
* whether or not DTD declarations should be expanded and
* included into the DocumentType object.
*/
public void setIncludeExternalDTDDeclarations(boolean include) {
this.includeExternalDTDDeclarations = include;
}
/**
* Sets whether String interning is enabled or disabled for element &
* attribute names and namespace URIs. This proprety is enabled by default.
*
* @return DOCUMENT ME!
*/
public boolean isStringInternEnabled() {
return stringInternEnabled;
}
/**
* Sets whether String interning is enabled or disabled for element &
* attribute names and namespace URIs
*
* @param stringInternEnabled
* DOCUMENT ME!
*/
public void setStringInternEnabled(boolean stringInternEnabled) {
this.stringInternEnabled = stringInternEnabled;
}
/**
* Returns whether adjacent text nodes should be merged together.
*
* @return Value of property mergeAdjacentText.
*/
public boolean isMergeAdjacentText() {
return mergeAdjacentText;
}
/**
* Sets whether or not adjacent text nodes should be merged together when
* parsing.
*
* @param mergeAdjacentText
* New value of property mergeAdjacentText.
*/
public void setMergeAdjacentText(boolean mergeAdjacentText) {
this.mergeAdjacentText = mergeAdjacentText;
}
/**
* Sets whether whitespace between element start and end tags should be
* ignored
*
* @return Value of property stripWhitespaceText.
*/
public boolean isStripWhitespaceText() {
return stripWhitespaceText;
}
/**
* Sets whether whitespace between element start and end tags should be
* ignored.
*
* @param stripWhitespaceText
* New value of property stripWhitespaceText.
*/
public void setStripWhitespaceText(boolean stripWhitespaceText) {
this.stripWhitespaceText = stripWhitespaceText;
}
/**
* Returns whether we should ignore comments or not.
*
* @return boolean
*/
public boolean isIgnoreComments() {
return ignoreComments;
}
/**
* Sets whether we should ignore comments or not.
*
* @param ignoreComments
* whether we should ignore comments or not.
*/
public void setIgnoreComments(boolean ignoreComments) {
this.ignoreComments = ignoreComments;
}
/**
* DOCUMENT ME!
*
* @return the <code>DocumentFactory</code> used to create document
* objects
*/
public DocumentFactory getDocumentFactory() {
if (factory == null) {
factory = DocumentFactory.getInstance();
}
return factory;
}
/**
* <p>
* This sets the <code>DocumentFactory</code> used to create new
* documents. This method allows the building of custom DOM4J tree objects
* to be implemented easily using a custom derivation of
* {@link DocumentFactory}
* </p>
*
* @param documentFactory
* <code>DocumentFactory</code> used to create DOM4J objects
*/
public void setDocumentFactory(DocumentFactory documentFactory) {
this.factory = documentFactory;
}
/**
* DOCUMENT ME!
*
* @return the <code>ErrorHandler</code> used by SAX
*/
public ErrorHandler getErrorHandler() {
return errorHandler;
}
/**
* Sets the <code>ErrorHandler</code> used by the SAX
* <code>XMLReader</code>.
*
* @param errorHandler
* is the <code>ErrorHandler</code> used by SAX
*/
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
/**
* Returns the current entity resolver used to resolve entities
*
* @return DOCUMENT ME!
*/
public EntityResolver getEntityResolver() {
return entityResolver;
}
/**
* Sets the entity resolver used to resolve entities.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -