📄 saxbuilder.java
字号:
boolean coreFeatures)
throws JDOMException {
// Set any user-specified features on the parser.
Iterator iter = features.keySet().iterator();
while (iter.hasNext()) {
String name = (String)iter.next();
Boolean value = (Boolean)features.get(name);
internalSetFeature(parser, name, value.booleanValue(), name);
}
// Set any user-specified properties on the parser.
iter = properties.keySet().iterator();
while (iter.hasNext()) {
String name = (String)iter.next();
internalSetProperty(parser, name, properties.get(name), name);
}
if (coreFeatures) {
// Set validation.
try {
internalSetFeature(parser,
"http://xml.org/sax/features/validation",
validate, "Validation");
} catch (JDOMException e) {
// If validation is not supported, and the user is requesting
// that we don't validate, that's fine - don't throw an
// exception.
if (validate)
throw e;
}
// Setup some namespace features.
internalSetFeature(parser,
"http://xml.org/sax/features/namespaces",
true, "Namespaces");
internalSetFeature(parser,
"http://xml.org/sax/features/namespace-prefixes",
true, "Namespace prefixes");
}
// Set entity expansion
// Note SAXHandler can work regardless of how this is set, but when
// entity expansion it's worth it to try to tell the parser not to
// even bother with external general entities.
// Apparently no parsers yet support this feature.
// XXX It might make sense to setEntityResolver() with a resolver
// that simply ignores external general entities
try {
if (parser.getFeature("http://xml.org/sax/features/external-general-entities") != expand) {
parser.setFeature("http://xml.org/sax/features/external-general-entities", expand);
}
}
catch (SAXNotRecognizedException e) { /* Ignore... */ }
catch (SAXNotSupportedException e) { /* Ignore... */ }
}
/**
* Tries to set a feature on the parser. If the feature cannot be set,
* throws a JDOMException describing the problem.
*/
private void internalSetFeature(XMLReader parser, String feature,
boolean value, String displayName) throws JDOMException {
try {
parser.setFeature(feature, value);
} catch (SAXNotSupportedException e) {
throw new JDOMException(
displayName + " feature not supported for SAX driver " + parser.getClass().getName());
} catch (SAXNotRecognizedException e) {
throw new JDOMException(
displayName + " feature not recognized for SAX driver " + parser.getClass().getName());
}
}
/**
* <p>
* Tries to set a property on the parser. If the property cannot be set,
* throws a JDOMException describing the problem.
* </p>
*/
private void internalSetProperty(XMLReader parser, String property,
Object value, String displayName) throws JDOMException {
try {
parser.setProperty(property, value);
} catch (SAXNotSupportedException e) {
throw new JDOMException(
displayName + " property not supported for SAX driver " + parser.getClass().getName());
} catch (SAXNotRecognizedException e) {
throw new JDOMException(
displayName + " property not recognized for SAX driver " + parser.getClass().getName());
}
}
/**
* <p>
* This builds a document from the supplied
* input stream.
* </p>
*
* @param in <code>InputStream</code> to read from
* @return <code>Document</code> resultant Document object
* @throws JDOMException when errors occur in parsing
* @throws IOException when an I/O error prevents a document
* from being fully parsed.
*/
public Document build(InputStream in)
throws JDOMException, IOException {
return build(new InputSource(in));
}
/**
* <p>
* This builds a document from the supplied
* filename.
* </p>
*
* @param file <code>File</code> to read from
* @return <code>Document</code> resultant Document object
* @throws JDOMException when errors occur in parsing
* @throws IOException when an I/O error prevents a document
* from being fully parsed
*/
public Document build(File file)
throws JDOMException, IOException {
try {
URL url = fileToURL(file);
return build(url);
} catch (MalformedURLException e) {
throw new JDOMException("Error in building", e);
}
}
/**
* <p>
* This builds a document from the supplied
* URL.
* </p>
*
* @param url <code>URL</code> to read from.
* @return <code>Document</code> - resultant Document object.
* @throws JDOMException when errors occur in parsing
* @throws IOException when an I/O error prevents a document
* from being fully parsed.
*/
public Document build(URL url)
throws JDOMException, IOException {
String systemID = url.toExternalForm();
return build(new InputSource(systemID));
}
/**
* <p>
* This builds a document from the supplied
* input stream.
* </p>
*
* @param in <code>InputStream</code> to read from.
* @param systemId base for resolving relative URIs
* @return <code>Document</code> resultant Document object
* @throws JDOMException when errors occur in parsing
* @throws IOException when an I/O error prevents a document
* from being fully parsed
*/
public Document build(InputStream in, String systemId)
throws JDOMException, IOException {
InputSource src = new InputSource(in);
src.setSystemId(systemId);
return build(src);
}
/**
* <p>
* This builds a document from the supplied
* Reader. It's the programmer's responsibility to make sure
* the reader matches the encoding of the file. It's often easier
* and safer to use an InputStream rather than a Reader, and to let the
* parser auto-detect the encoding from the XML declaration.
* </p>
*
* @param characterStream <code>Reader</code> to read from
* @return <code>Document</code> resultant Document object
* @throws JDOMException when errors occur in parsing
* @throws IOException when an I/O error prevents a document
* from being fully parsed
*/
public Document build(Reader characterStream)
throws JDOMException, IOException {
return build(new InputSource(characterStream));
}
/**
* <p>
* This builds a document from the supplied
* Reader. It's the programmer's responsibility to make sure
* the reader matches the encoding of the file. It's often easier
* and safer to use an InputStream rather than a Reader, and to let the
* parser auto-detect the encoding from the XML declaration.
* </p>
*
* @param characterStream <code>Reader</code> to read from.
* @param systemId base for resolving relative URIs
* @return <code>Document</code> resultant Document object
* @throws JDOMException when errors occur in parsing
* @throws IOException when an I/O error prevents a document
* from being fully parsed
*/
public Document build(Reader characterStream, String systemId)
throws JDOMException, IOException {
InputSource src = new InputSource(characterStream);
src.setSystemId(systemId);
return build(src);
}
/**
* <p>
* This builds a document from the supplied
* URI.
* </p>
* @param systemId URI for the input
* @return <code>Document</code> resultant Document object
* @throws JDOMException when errors occur in parsing
* @throws IOException when an I/O error prevents a document
* from being fully parsed
*/
public Document build(String systemId)
throws JDOMException, IOException {
return build(new InputSource(systemId));
}
// /**
// * Imitation of File.toURL(), a JDK 1.2 method, reimplemented
// * here to work with JDK 1.1.
// *
// * @see java.io.File
// *
// * @param f the file to convert
// * @return the file path converted to a file: URL
// */
// protected URL fileToURL(File f) throws MalformedURLException {
// String path = f.getAbsolutePath();
// if (File.separatorChar != '/') {
// path = path.replace(File.separatorChar, '/');
// }
// if (!path.startsWith("/")) {
// path = "/" + path;
// }
// if (!path.endsWith("/") && f.isDirectory()) {
// path = path + "/";
// }
// return new URL("file", "", path);
// }
/** Custom File.toUrl() implementation to handle special chars in file names
*
* @param file file object whose path will be converted
* @return URL form of the file, with special characters handled
* @throws MalformedURLException if there's a problem constructing a URL
*/
private static URL fileToURL(File file) throws MalformedURLException {
StringBuffer buffer = new StringBuffer();
String path = file.getAbsolutePath();
// Convert non-URL style file separators
if (File.separatorChar != '/') {
path = path.replace(File.separatorChar, '/');
}
// Make sure it starts at root
if (!path.startsWith("/")) {
buffer.append('/');
}
// Copy, converting URL special characters as we go
int len = path.length();
for (int i = 0; i < len; i++) {
char c = path.charAt(i);
if (c == ' ')
buffer.append("%20");
else if (c == '#')
buffer.append("%23");
else if (c == '%')
buffer.append("%25");
else if (c == '&')
buffer.append("%26");
else if (c == ';')
buffer.append("%3B");
else if (c == '<')
buffer.append("%3C");
else if (c == '=')
buffer.append("%3D");
else if (c == '>')
buffer.append("%3E");
else if (c == '?')
buffer.append("%3F");
else if (c == '~')
buffer.append("%7E");
else
buffer.append(c);
}
// Make sure directories end with slash
if (!path.endsWith("/") && file.isDirectory()) {
buffer.append('/');
}
// Return URL
return new URL("file", "", buffer.toString());
}
/**
* Returns whether or not entities are being expanded into normal text
* content.
*
* @return whether entities are being expanded
*/
public boolean getExpandEntities() {
return expand;
}
/**
* <p>
* This sets whether or not to expand entities for the builder.
* A true means to expand entities as normal content. A false means to
* leave entities unexpanded as <code>EntityRef</code> objects. The
* default is true.
* </p>
* <p>
* When this setting is false, the internal DTD subset is retained; when
* this setting is true, the internal DTD subset is not retained.
* </p>
* <p>
* Note that Xerces (at least up to 1.4.4) has a bug where entities
* in attribute values will be misreported if this flag is turned off,
* resulting in entities to appear within element content. When turning
* entity expansion off either avoid entities in attribute values, or
* use another parser like Crimson.
* http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6111
* </p>
*
* @param expand <code>boolean</code> indicating whether entity expansion
* should occur.
*/
public void setExpandEntities(boolean expand) {
this.expand = expand;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -