📄 saxhandler.java
字号:
if (!inDTD) {
if (inEntity) {
getCurrentElement().setContent(factory.text(data));
} else {
getCurrentElement().addContent(factory.text(data));
}
*/
if (previousCDATA) {
factory.addContent(getCurrentElement(), factory.cdata(data));
}
else {
factory.addContent(getCurrentElement(), factory.text(data));
}
previousCDATA = inCDATA;
}
/**
* Indicates the end of an element
* (<code></[element name]></code>) is reached. Note that
* the parser does not distinguish between empty
* elements and non-empty elements, so this will occur uniformly.
*
* @param namespaceURI <code>String</code> URI of namespace this
* element is associated with
* @param localName <code>String</code> name of element without prefix
* @param qName <code>String</code> name of element in XML 1.0 form
* @throws SAXException when things go wrong
*/
public void endElement(String namespaceURI, String localName,
String qName) throws SAXException {
if (suppress) return;
flushCharacters();
if (!atRoot) {
Parent p = currentElement.getParent();
if (p instanceof Document) {
atRoot = true;
}
else {
currentElement = (Element) p;
}
}
else {
throw new SAXException(
"Ill-formed XML document (missing opening tag for " +
localName + ")");
}
}
/**
* This will signify that a DTD is being parsed, and can be
* used to ensure that comments and other lexical structures
* in the DTD are not added to the JDOM <code>Document</code>
* object.
*
* @param name <code>String</code> name of element listed in DTD
* @param publicID <code>String</code> public ID of DTD
* @param systemID <code>String</code> system ID of DTD
*/
public void startDTD(String name, String publicID, String systemID)
throws SAXException {
flushCharacters(); // Is this needed here?
factory.addContent(document, factory.docType(name, publicID, systemID));
inDTD = true;
inInternalSubset = true;
}
/**
* This signifies that the reading of the DTD is complete.
*
* @throws SAXException
*/
public void endDTD() throws SAXException {
document.getDocType().setInternalSubset(internalSubset.toString());
inDTD = false;
inInternalSubset = false;
}
public void startEntity(String name) throws SAXException {
entityDepth++;
if (expand || entityDepth > 1) {
// Short cut out if we're expanding or if we're nested
return;
}
// A "[dtd]" entity indicates the beginning of the external subset
if (name.equals("[dtd]")) {
inInternalSubset = false;
return;
}
// Ignore DTD references, and translate the standard 5
if ((!inDTD) &&
(!name.equals("amp")) &&
(!name.equals("lt")) &&
(!name.equals("gt")) &&
(!name.equals("apos")) &&
(!name.equals("quot"))) {
if (!expand) {
String pub = null;
String sys = null;
String[] ids = (String[]) externalEntities.get(name);
if (ids != null) {
pub = ids[0]; // may be null, that's OK
sys = ids[1]; // may be null, that's OK
}
/**
* if no current element, this entity belongs to an attribute
* in these cases, it is an error on the part of the parser
* to call startEntity but this will help in some cases.
* See org/xml/sax/ext/LexicalHandler.html#startEntity(java.lang.String)
* for more information
*/
if (!atRoot) {
flushCharacters();
EntityRef entity = factory.entityRef(name, pub, sys);
// no way to tell if the entity was from an attribute or element so just assume element
factory.addContent(getCurrentElement(), entity);
}
suppress = true;
}
}
}
public void endEntity(String name) throws SAXException {
entityDepth--;
if (entityDepth == 0) {
// No way are we suppressing if not in an entity,
// regardless of the "expand" value
suppress = false;
}
if (name.equals("[dtd]")) {
inInternalSubset = true;
}
}
/**
* Report a CDATA section
*
* @throws SAXException
*/
public void startCDATA() throws SAXException {
if (suppress) return;
inCDATA = true;
}
/**
* Report a CDATA section
*/
public void endCDATA() throws SAXException {
if (suppress) return;
previousCDATA = true;
inCDATA = false;
}
/**
* This reports that a comments is parsed. If not in the
* DTD, this comment is added to the current JDOM
* <code>Element</code>, or the <code>Document</code> itself
* if at that level.
*
* @param ch <code>ch[]</code> array of comment characters.
* @param start <code>int</code> index to start reading from.
* @param length <code>int</code> length of data.
* @throws SAXException
*/
public void comment(char[] ch, int start, int length)
throws SAXException {
if (suppress) return;
flushCharacters();
String commentText = new String(ch, start, length);
if (inDTD && inInternalSubset && (expand == false)) {
internalSubset.append(" <!--")
.append(commentText)
.append("-->\n");
return;
}
if ((!inDTD) && (!commentText.equals(""))) {
if (atRoot) {
factory.addContent(document, factory.comment(commentText));
} else {
factory.addContent(getCurrentElement(), factory.comment(commentText));
}
}
}
/**
* Handle the declaration of a Notation in a DTD
*
* @param name name of the notation
* @param publicID the public ID of the notation
* @param systemID the system ID of the notation
*/
public void notationDecl(String name, String publicID, String systemID)
throws SAXException {
if (!inInternalSubset) return;
internalSubset.append(" <!NOTATION ")
.append(name);
appendExternalId(publicID, systemID);
internalSubset.append(">\n");
}
/**
* Handler for unparsed entity declarations in the DTD
*
* @param name <code>String</code> of the unparsed entity decl
* @param publicID <code>String</code> of the unparsed entity decl
* @param systemID <code>String</code> of the unparsed entity decl
* @param notationName <code>String</code> of the unparsed entity decl
*/
public void unparsedEntityDecl(String name, String publicID,
String systemID, String notationName)
throws SAXException {
if (!inInternalSubset) return;
internalSubset.append(" <!ENTITY ")
.append(name);
appendExternalId(publicID, systemID);
internalSubset.append(" NDATA ")
.append(notationName);
internalSubset.append(">\n");
}
/**
* Appends an external ID to the internal subset buffer. Either publicID
* or systemID may be null, but not both.
*
* @param publicID the public ID
* @param systemID the system ID
*/
private void appendExternalId(String publicID, String systemID) {
if (publicID != null) {
internalSubset.append(" PUBLIC \"")
.append(publicID)
.append('\"');
}
if (systemID != null) {
if (publicID == null) {
internalSubset.append(" SYSTEM ");
}
else {
internalSubset.append(' ');
}
internalSubset.append('\"')
.append(systemID)
.append('\"');
}
}
/**
* Returns the being-parsed element.
*
* @return <code>Element</code> - element being built.
* @throws SAXException
*/
public Element getCurrentElement() throws SAXException {
if (currentElement == null) {
throw new SAXException(
"Ill-formed XML document (multiple root elements detected)");
}
return currentElement;
}
/**
* Returns the the JDOM Attribute type value from the SAX 2.0
* attribute type string provided by the parser.
*
* @param typeName <code>String</code> the SAX 2.0 attribute
* type string.
*
* @return <code>int</code> the JDOM attribute type.
*
* @see Attribute#setAttributeType
* @see Attributes#getType
*/
private static int getAttributeType(String typeName) {
Integer type = (Integer)(attrNameToTypeMap.get(typeName));
if (type == null) {
if (typeName != null && typeName.length() > 0 &&
typeName.charAt(0) == '(') {
// Xerces 1.4.X reports attributes of enumerated type with
// a type string equals to the enumeration definition, i.e.
// starting with a parenthesis.
return Attribute.ENUMERATED_TYPE;
}
else {
return Attribute.UNDECLARED_TYPE;
}
} else {
return type.intValue();
}
}
/**
* Receives an object for locating the origin of SAX document
* events. This method is invoked by the SAX parser.
* <p>
* {@link org.jdom.JDOMFactory} implementations can use the
* {@link #getDocumentLocator} method to get access to the
* {@link Locator} during parse.
* </p>
*
* @param locator <code>Locator</code> an object that can return
* the location of any SAX document event.
*/
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
/**
* Provides access to the {@link Locator} object provided by the
* SAX parser.
*
* @return <code>Locator</code> an object that can return
* the location of any SAX document event.
*/
public Locator getDocumentLocator() {
return locator;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -