|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package org.cyberneko.html; |
|
|
|
import java.io.EOFException; |
|
import java.io.FilterInputStream; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.InputStreamReader; |
|
import java.io.Reader; |
|
import java.io.UnsupportedEncodingException; |
|
import java.net.URL; |
|
import java.util.BitSet; |
|
import java.util.Locale; |
|
import java.util.Stack; |
|
|
|
import org.apache.xerces.util.EncodingMap; |
|
import org.apache.xerces.util.NamespaceSupport; |
|
import org.apache.xerces.util.URI; |
|
import org.apache.xerces.util.XMLAttributesImpl; |
|
import org.apache.xerces.util.XMLResourceIdentifierImpl; |
|
import org.apache.xerces.util.XMLStringBuffer; |
|
import org.apache.xerces.xni.Augmentations; |
|
import org.apache.xerces.xni.NamespaceContext; |
|
import org.apache.xerces.xni.QName; |
|
import org.apache.xerces.xni.XMLAttributes; |
|
import org.apache.xerces.xni.XMLDocumentHandler; |
|
import org.apache.xerces.xni.XMLLocator; |
|
import org.apache.xerces.xni.XMLResourceIdentifier; |
|
import org.apache.xerces.xni.XMLString; |
|
import org.apache.xerces.xni.XNIException; |
|
import org.apache.xerces.xni.parser.XMLComponentManager; |
|
import org.apache.xerces.xni.parser.XMLConfigurationException; |
|
import org.apache.xerces.xni.parser.XMLDocumentScanner; |
|
import org.apache.xerces.xni.parser.XMLInputSource; |
|
import org.cyberneko.html.xercesbridge.XercesBridge; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class HTMLScanner |
|
implements XMLDocumentScanner, XMLLocator, HTMLComponent { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String HTML_4_01_STRICT_PUBID = "-//W3C//DTD HTML 4.01//EN"; |
|
|
|
|
|
public static final String HTML_4_01_STRICT_SYSID = "http://www.w3.org/TR/html4/strict.dtd"; |
|
|
|
|
|
|
|
|
|
public static final String HTML_4_01_TRANSITIONAL_PUBID = "-//W3C//DTD HTML 4.01 Transitional//EN"; |
|
|
|
|
|
public static final String HTML_4_01_TRANSITIONAL_SYSID = "http://www.w3.org/TR/html4/loose.dtd"; |
|
|
|
|
|
|
|
|
|
public static final String HTML_4_01_FRAMESET_PUBID = "-//W3C//DTD HTML 4.01 Frameset//EN"; |
|
|
|
|
|
public static final String HTML_4_01_FRAMESET_SYSID = "http://www.w3.org/TR/html4/frameset.dtd"; |
|
|
|
|
|
|
|
|
|
protected static final String AUGMENTATIONS = "http://cyberneko.org/html/features/augmentations"; |
|
|
|
|
|
protected static final String REPORT_ERRORS = "http://cyberneko.org/html/features/report-errors"; |
|
|
|
|
|
public static final String NOTIFY_CHAR_REFS = "http://apache.org/xml/features/scanner/notify-char-refs"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String NOTIFY_XML_BUILTIN_REFS = "http://apache.org/xml/features/scanner/notify-builtin-refs"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String NOTIFY_HTML_BUILTIN_REFS = "http://cyberneko.org/html/features/scanner/notify-builtin-refs"; |
|
|
|
|
|
public static final String FIX_MSWINDOWS_REFS = "http://cyberneko.org/html/features/scanner/fix-mswindows-refs"; |
|
|
|
|
|
|
|
|
|
|
|
public static final String SCRIPT_STRIP_COMMENT_DELIMS = "http://cyberneko.org/html/features/scanner/script/strip-comment-delims"; |
|
|
|
|
|
|
|
|
|
|
|
public static final String SCRIPT_STRIP_CDATA_DELIMS = "http://cyberneko.org/html/features/scanner/script/strip-cdata-delims"; |
|
|
|
|
|
|
|
|
|
|
|
public static final String STYLE_STRIP_COMMENT_DELIMS = "http://cyberneko.org/html/features/scanner/style/strip-comment-delims"; |
|
|
|
|
|
|
|
|
|
|
|
public static final String STYLE_STRIP_CDATA_DELIMS = "http://cyberneko.org/html/features/scanner/style/strip-cdata-delims"; |
|
|
|
|
|
|
|
|
|
|
|
public static final String IGNORE_SPECIFIED_CHARSET = "http://cyberneko.org/html/features/scanner/ignore-specified-charset"; |
|
|
|
|
|
public static final String CDATA_SECTIONS = "http://cyberneko.org/html/features/scanner/cdata-sections"; |
|
|
|
|
|
public static final String OVERRIDE_DOCTYPE = "http://cyberneko.org/html/features/override-doctype"; |
|
|
|
|
|
public static final String INSERT_DOCTYPE = "http://cyberneko.org/html/features/insert-doctype"; |
|
|
|
|
|
public static final String PARSE_NOSCRIPT_CONTENT = "http://cyberneko.org/html/features/parse-noscript-content"; |
|
|
|
|
|
public static final String ALLOW_SELFCLOSING_IFRAME = "http://cyberneko.org/html/features/scanner/allow-selfclosing-iframe"; |
|
|
|
|
|
public static final String ALLOW_SELFCLOSING_TAGS = "http://cyberneko.org/html/features/scanner/allow-selfclosing-tags"; |
|
|
|
|
|
protected static final String NORMALIZE_ATTRIBUTES = "http://cyberneko.org/html/features/scanner/normalize-attrs"; |
|
|
|
|
|
private static final String[] RECOGNIZED_FEATURES = { |
|
AUGMENTATIONS, |
|
REPORT_ERRORS, |
|
NOTIFY_CHAR_REFS, |
|
NOTIFY_XML_BUILTIN_REFS, |
|
NOTIFY_HTML_BUILTIN_REFS, |
|
FIX_MSWINDOWS_REFS, |
|
SCRIPT_STRIP_CDATA_DELIMS, |
|
SCRIPT_STRIP_COMMENT_DELIMS, |
|
STYLE_STRIP_CDATA_DELIMS, |
|
STYLE_STRIP_COMMENT_DELIMS, |
|
IGNORE_SPECIFIED_CHARSET, |
|
CDATA_SECTIONS, |
|
OVERRIDE_DOCTYPE, |
|
INSERT_DOCTYPE, |
|
NORMALIZE_ATTRIBUTES, |
|
PARSE_NOSCRIPT_CONTENT, |
|
ALLOW_SELFCLOSING_IFRAME, |
|
ALLOW_SELFCLOSING_TAGS, |
|
}; |
|
|
|
|
|
private static final Boolean[] RECOGNIZED_FEATURES_DEFAULTS = { |
|
null, |
|
null, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
Boolean.TRUE, |
|
Boolean.FALSE, |
|
Boolean.FALSE, |
|
}; |
|
|
|
|
|
|
|
|
|
protected static final String NAMES_ELEMS = "http://cyberneko.org/html/properties/names/elems"; |
|
|
|
|
|
protected static final String NAMES_ATTRS = "http://cyberneko.org/html/properties/names/attrs"; |
|
|
|
|
|
protected static final String DEFAULT_ENCODING = "http://cyberneko.org/html/properties/default-encoding"; |
|
|
|
|
|
protected static final String ERROR_REPORTER = "http://cyberneko.org/html/properties/error-reporter"; |
|
|
|
|
|
protected static final String DOCTYPE_PUBID = "http://cyberneko.org/html/properties/doctype/pubid"; |
|
|
|
|
|
protected static final String DOCTYPE_SYSID = "http://cyberneko.org/html/properties/doctype/sysid"; |
|
|
|
|
|
private static final String[] RECOGNIZED_PROPERTIES = { |
|
NAMES_ELEMS, |
|
NAMES_ATTRS, |
|
DEFAULT_ENCODING, |
|
ERROR_REPORTER, |
|
DOCTYPE_PUBID, |
|
DOCTYPE_SYSID, |
|
}; |
|
|
|
|
|
private static final Object[] RECOGNIZED_PROPERTIES_DEFAULTS = { |
|
null, |
|
null, |
|
"Windows-1252", |
|
null, |
|
HTML_4_01_TRANSITIONAL_PUBID, |
|
HTML_4_01_TRANSITIONAL_SYSID, |
|
}; |
|
|
|
|
|
|
|
|
|
protected static final short STATE_CONTENT = 0; |
|
|
|
|
|
protected static final short STATE_MARKUP_BRACKET = 1; |
|
|
|
|
|
protected static final short STATE_START_DOCUMENT = 10; |
|
|
|
|
|
protected static final short STATE_END_DOCUMENT = 11; |
|
|
|
|
|
|
|
|
|
protected static final short NAMES_NO_CHANGE = 0; |
|
|
|
|
|
protected static final short NAMES_UPPERCASE = 1; |
|
|
|
|
|
protected static final short NAMES_LOWERCASE = 2; |
|
|
|
|
|
|
|
|
|
protected static final int DEFAULT_BUFFER_SIZE = 2048; |
|
|
|
|
|
|
|
|
|
private static final boolean DEBUG_SCANNER = false; |
|
|
|
|
|
private static final boolean DEBUG_SCANNER_STATE = false; |
|
|
|
|
|
private static final boolean DEBUG_BUFFER = false; |
|
|
|
|
|
private static final boolean DEBUG_CHARSET = false; |
|
|
|
|
|
protected static final boolean DEBUG_CALLBACKS = false; |
|
|
|
|
|
|
|
|
|
protected static final HTMLEventInfo SYNTHESIZED_ITEM = |
|
new HTMLEventInfo.SynthesizedItem(); |
|
|
|
private final static BitSet ENTITY_CHARS = new BitSet(); |
|
static { |
|
final String str = "-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"; |
|
for (int i = 0; i < str.length(); ++i) { |
|
char c = str.charAt(i); |
|
ENTITY_CHARS.set(c); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected boolean fAugmentations; |
|
|
|
|
|
protected boolean fReportErrors; |
|
|
|
|
|
protected boolean fNotifyCharRefs; |
|
|
|
|
|
protected boolean fNotifyXmlBuiltinRefs; |
|
|
|
|
|
protected boolean fNotifyHtmlBuiltinRefs; |
|
|
|
|
|
protected boolean fFixWindowsCharRefs; |
|
|
|
|
|
protected boolean fScriptStripCDATADelims; |
|
|
|
|
|
protected boolean fScriptStripCommentDelims; |
|
|
|
|
|
protected boolean fStyleStripCDATADelims; |
|
|
|
|
|
protected boolean fStyleStripCommentDelims; |
|
|
|
|
|
protected boolean fIgnoreSpecifiedCharset; |
|
|
|
|
|
protected boolean fCDATASections; |
|
|
|
|
|
protected boolean fOverrideDoctype; |
|
|
|
|
|
protected boolean fInsertDoctype; |
|
|
|
|
|
protected boolean fNormalizeAttributes; |
|
|
|
|
|
protected boolean fParseNoScriptContent; |
|
|
|
|
|
protected boolean fParseNoFramesContent; |
|
|
|
|
|
protected boolean fAllowSelfclosingIframe; |
|
|
|
|
|
protected boolean fAllowSelfclosingTags; |
|
|
|
|
|
|
|
|
|
protected short fNamesElems; |
|
|
|
|
|
protected short fNamesAttrs; |
|
|
|
|
|
protected String fDefaultIANAEncoding; |
|
|
|
|
|
protected HTMLErrorReporter fErrorReporter; |
|
|
|
|
|
protected String fDoctypePubid; |
|
|
|
|
|
protected String fDoctypeSysid; |
|
|
|
|
|
|
|
|
|
protected int fBeginLineNumber; |
|
|
|
|
|
protected int fBeginColumnNumber; |
|
|
|
|
|
protected int fBeginCharacterOffset; |
|
|
|
|
|
protected int fEndLineNumber; |
|
|
|
|
|
protected int fEndColumnNumber; |
|
|
|
|
|
protected int fEndCharacterOffset; |
|
|
|
|
|
|
|
|
|
protected PlaybackInputStream fByteStream; |
|
|
|
|
|
protected CurrentEntity fCurrentEntity; |
|
|
|
|
|
protected final Stack fCurrentEntityStack = new Stack(); |
|
|
|
|
|
protected Scanner fScanner; |
|
|
|
|
|
protected short fScannerState; |
|
|
|
|
|
protected XMLDocumentHandler fDocumentHandler; |
|
|
|
|
|
protected String fIANAEncoding; |
|
|
|
|
|
protected String fJavaEncoding; |
|
|
|
|
|
protected boolean fIso8859Encoding; |
|
|
|
|
|
protected int fElementCount; |
|
|
|
|
|
protected int fElementDepth; |
|
|
|
|
|
|
|
|
|
protected Scanner fContentScanner = new ContentScanner(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected SpecialScanner fSpecialScanner = new SpecialScanner(); |
|
|
|
|
|
|
|
|
|
protected final XMLStringBuffer fStringBuffer = new XMLStringBuffer(1024); |
|
|
|
|
|
private final XMLStringBuffer fStringBuffer2 = new XMLStringBuffer(1024); |
|
|
|
|
|
private final XMLStringBuffer fNonNormAttr = new XMLStringBuffer(128); |
|
|
|
|
|
private final HTMLAugmentations fInfosetAugs = new HTMLAugmentations(); |
|
|
|
|
|
private final LocationItem fLocationItem = new LocationItem(); |
|
|
|
|
|
private final boolean[] fSingleBoolean = { false }; |
|
|
|
|
|
private final XMLResourceIdentifierImpl fResourceId = new XMLResourceIdentifierImpl(); |
|
|
|
private final char REPLACEMENT_CHARACTER = '\uFFFD'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void pushInputSource(XMLInputSource inputSource) { |
|
final Reader reader = getReader(inputSource); |
|
|
|
fCurrentEntityStack.push(fCurrentEntity); |
|
String encoding = inputSource.getEncoding(); |
|
String publicId = inputSource.getPublicId(); |
|
String baseSystemId = inputSource.getBaseSystemId(); |
|
String literalSystemId = inputSource.getSystemId(); |
|
String expandedSystemId = expandSystemId(literalSystemId, baseSystemId); |
|
fCurrentEntity = new CurrentEntity(reader, encoding, |
|
publicId, baseSystemId, |
|
literalSystemId, expandedSystemId); |
|
} |
|
|
|
private Reader getReader(final XMLInputSource inputSource) { |
|
Reader reader = inputSource.getCharacterStream(); |
|
if (reader == null) { |
|
try { |
|
return new InputStreamReader(inputSource.getByteStream(), fJavaEncoding); |
|
} |
|
catch (final UnsupportedEncodingException e) { |
|
|
|
} |
|
} |
|
return reader; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void evaluateInputSource(XMLInputSource inputSource) { |
|
final Scanner previousScanner = fScanner; |
|
final short previousScannerState = fScannerState; |
|
final CurrentEntity previousEntity = fCurrentEntity; |
|
final Reader reader = getReader(inputSource); |
|
|
|
String encoding = inputSource.getEncoding(); |
|
String publicId = inputSource.getPublicId(); |
|
String baseSystemId = inputSource.getBaseSystemId(); |
|
String literalSystemId = inputSource.getSystemId(); |
|
String expandedSystemId = expandSystemId(literalSystemId, baseSystemId); |
|
fCurrentEntity = new CurrentEntity(reader, encoding, |
|
publicId, baseSystemId, |
|
literalSystemId, expandedSystemId); |
|
setScanner(fContentScanner); |
|
setScannerState(STATE_CONTENT); |
|
try { |
|
do { |
|
fScanner.scan(false); |
|
} while (fScannerState != STATE_END_DOCUMENT); |
|
} |
|
catch (final IOException e) { |
|
|
|
} |
|
setScanner(previousScanner); |
|
setScannerState(previousScannerState); |
|
fCurrentEntity = previousEntity; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void cleanup(boolean closeall) { |
|
int size = fCurrentEntityStack.size(); |
|
if (size > 0) { |
|
|
|
if (fCurrentEntity != null) { |
|
fCurrentEntity.closeQuietly(); |
|
} |
|
|
|
for (int i = closeall ? 0 : 1; i < size; i++) { |
|
fCurrentEntity = (CurrentEntity) fCurrentEntityStack.pop(); |
|
fCurrentEntity.closeQuietly(); |
|
} |
|
} |
|
else if (closeall && fCurrentEntity != null) { |
|
fCurrentEntity.closeQuietly(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getEncoding() { |
|
return fCurrentEntity != null ? fCurrentEntity.encoding : null; |
|
} |
|
|
|
|
|
public String getPublicId() { |
|
return fCurrentEntity != null ? fCurrentEntity.publicId : null; |
|
} |
|
|
|
|
|
public String getBaseSystemId() { |
|
return fCurrentEntity != null ? fCurrentEntity.baseSystemId : null; |
|
} |
|
|
|
|
|
public String getLiteralSystemId() { |
|
return fCurrentEntity != null ? fCurrentEntity.literalSystemId : null; |
|
} |
|
|
|
|
|
public String getExpandedSystemId() { |
|
return fCurrentEntity != null ? fCurrentEntity.expandedSystemId : null; |
|
} |
|
|
|
|
|
public int getLineNumber() { |
|
return fCurrentEntity != null ? fCurrentEntity.getLineNumber() : -1; |
|
} |
|
|
|
|
|
public int getColumnNumber() { |
|
return fCurrentEntity != null ? fCurrentEntity.getColumnNumber() : -1; |
|
} |
|
|
|
|
|
public String getXMLVersion() { |
|
return fCurrentEntity != null ? fCurrentEntity.version : null; |
|
} |
|
|
|
|
|
public int getCharacterOffset() { |
|
return fCurrentEntity != null ? fCurrentEntity.getCharacterOffset() : -1; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public Boolean getFeatureDefault(String featureId) { |
|
int length = RECOGNIZED_FEATURES != null ? RECOGNIZED_FEATURES.length : 0; |
|
for (int i = 0; i < length; i++) { |
|
if (RECOGNIZED_FEATURES[i].equals(featureId)) { |
|
return RECOGNIZED_FEATURES_DEFAULTS[i]; |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
|
|
public Object getPropertyDefault(String propertyId) { |
|
int length = RECOGNIZED_PROPERTIES != null ? RECOGNIZED_PROPERTIES.length : 0; |
|
for (int i = 0; i < length; i++) { |
|
if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { |
|
return RECOGNIZED_PROPERTIES_DEFAULTS[i]; |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public String[] getRecognizedFeatures() { |
|
return RECOGNIZED_FEATURES; |
|
} |
|
|
|
|
|
public String[] getRecognizedProperties() { |
|
return RECOGNIZED_PROPERTIES; |
|
} |
|
|
|
|
|
public void reset(XMLComponentManager manager) |
|
throws XMLConfigurationException { |
|
|
|
|
|
fAugmentations = manager.getFeature(AUGMENTATIONS); |
|
fReportErrors = manager.getFeature(REPORT_ERRORS); |
|
fNotifyCharRefs = manager.getFeature(NOTIFY_CHAR_REFS); |
|
fNotifyXmlBuiltinRefs = manager.getFeature(NOTIFY_XML_BUILTIN_REFS); |
|
fNotifyHtmlBuiltinRefs = manager.getFeature(NOTIFY_HTML_BUILTIN_REFS); |
|
fFixWindowsCharRefs = manager.getFeature(FIX_MSWINDOWS_REFS); |
|
fScriptStripCDATADelims = manager.getFeature(SCRIPT_STRIP_CDATA_DELIMS); |
|
fScriptStripCommentDelims = manager.getFeature(SCRIPT_STRIP_COMMENT_DELIMS); |
|
fStyleStripCDATADelims = manager.getFeature(STYLE_STRIP_CDATA_DELIMS); |
|
fStyleStripCommentDelims = manager.getFeature(STYLE_STRIP_COMMENT_DELIMS); |
|
fIgnoreSpecifiedCharset = manager.getFeature(IGNORE_SPECIFIED_CHARSET); |
|
fCDATASections = manager.getFeature(CDATA_SECTIONS); |
|
fOverrideDoctype = manager.getFeature(OVERRIDE_DOCTYPE); |
|
fInsertDoctype = manager.getFeature(INSERT_DOCTYPE); |
|
fNormalizeAttributes = manager.getFeature(NORMALIZE_ATTRIBUTES); |
|
fParseNoScriptContent = manager.getFeature(PARSE_NOSCRIPT_CONTENT); |
|
fAllowSelfclosingIframe = manager.getFeature(ALLOW_SELFCLOSING_IFRAME); |
|
fAllowSelfclosingTags = manager.getFeature(ALLOW_SELFCLOSING_TAGS); |
|
|
|
|
|
fNamesElems = getNamesValue(String.valueOf(manager.getProperty(NAMES_ELEMS))); |
|
fNamesAttrs = getNamesValue(String.valueOf(manager.getProperty(NAMES_ATTRS))); |
|
fDefaultIANAEncoding = String.valueOf(manager.getProperty(DEFAULT_ENCODING)); |
|
fErrorReporter = (HTMLErrorReporter)manager.getProperty(ERROR_REPORTER); |
|
fDoctypePubid = String.valueOf(manager.getProperty(DOCTYPE_PUBID)); |
|
fDoctypeSysid = String.valueOf(manager.getProperty(DOCTYPE_SYSID)); |
|
|
|
} |
|
|
|
|
|
public void setFeature(final String featureId, final boolean state) { |
|
|
|
if (featureId.equals(AUGMENTATIONS)) { |
|
fAugmentations = state; |
|
} |
|
else if (featureId.equals(IGNORE_SPECIFIED_CHARSET)) { |
|
fIgnoreSpecifiedCharset = state; |
|
} |
|
else if (featureId.equals(NOTIFY_CHAR_REFS)) { |
|
fNotifyCharRefs = state; |
|
} |
|
else if (featureId.equals(NOTIFY_XML_BUILTIN_REFS)) { |
|
fNotifyXmlBuiltinRefs = state; |
|
} |
|
else if (featureId.equals(NOTIFY_HTML_BUILTIN_REFS)) { |
|
fNotifyHtmlBuiltinRefs = state; |
|
} |
|
else if (featureId.equals(FIX_MSWINDOWS_REFS)) { |
|
fFixWindowsCharRefs = state; |
|
} |
|
else if (featureId.equals(SCRIPT_STRIP_CDATA_DELIMS)) { |
|
fScriptStripCDATADelims = state; |
|
} |
|
else if (featureId.equals(SCRIPT_STRIP_COMMENT_DELIMS)) { |
|
fScriptStripCommentDelims = state; |
|
} |
|
else if (featureId.equals(STYLE_STRIP_CDATA_DELIMS)) { |
|
fStyleStripCDATADelims = state; |
|
} |
|
else if (featureId.equals(STYLE_STRIP_COMMENT_DELIMS)) { |
|
fStyleStripCommentDelims = state; |
|
} |
|
else if (featureId.equals(PARSE_NOSCRIPT_CONTENT)) { |
|
fParseNoScriptContent = state; |
|
} |
|
else if (featureId.equals(ALLOW_SELFCLOSING_IFRAME)) { |
|
fAllowSelfclosingIframe = state; |
|
} |
|
else if (featureId.equals(ALLOW_SELFCLOSING_TAGS)) { |
|
fAllowSelfclosingTags = state; |
|
} |
|
|
|
} |
|
|
|
|
|
public void setProperty(String propertyId, Object value) |
|
throws XMLConfigurationException { |
|
|
|
if (propertyId.equals(NAMES_ELEMS)) { |
|
fNamesElems = getNamesValue(String.valueOf(value)); |
|
return; |
|
} |
|
|
|
if (propertyId.equals(NAMES_ATTRS)) { |
|
fNamesAttrs = getNamesValue(String.valueOf(value)); |
|
return; |
|
} |
|
|
|
if (propertyId.equals(DEFAULT_ENCODING)) { |
|
fDefaultIANAEncoding = String.valueOf(value); |
|
return; |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setInputSource(XMLInputSource source) throws IOException { |
|
|
|
|
|
fElementCount = 0; |
|
fElementDepth = -1; |
|
fByteStream = null; |
|
fCurrentEntityStack.removeAllElements(); |
|
|
|
fBeginLineNumber = 1; |
|
fBeginColumnNumber = 1; |
|
fBeginCharacterOffset = 0; |
|
fEndLineNumber = fBeginLineNumber; |
|
fEndColumnNumber = fBeginColumnNumber; |
|
fEndCharacterOffset = fBeginCharacterOffset; |
|
|
|
|
|
fIANAEncoding = fDefaultIANAEncoding; |
|
fJavaEncoding = fIANAEncoding; |
|
|
|
|
|
String encoding = source.getEncoding(); |
|
String publicId = source.getPublicId(); |
|
String baseSystemId = source.getBaseSystemId(); |
|
String literalSystemId = source.getSystemId(); |
|
String expandedSystemId = expandSystemId(literalSystemId, baseSystemId); |
|
|
|
|
|
Reader reader = source.getCharacterStream(); |
|
if (reader == null) { |
|
InputStream inputStream = source.getByteStream(); |
|
if (inputStream == null) { |
|
URL url = new URL(expandedSystemId); |
|
inputStream = url.openStream(); |
|
} |
|
fByteStream = new PlaybackInputStream(inputStream); |
|
String[] encodings = new String[2]; |
|
if (encoding == null) { |
|
fByteStream.detectEncoding(encodings); |
|
} |
|
else { |
|
encodings[0] = encoding; |
|
} |
|
if (encodings[0] == null) { |
|
encodings[0] = fDefaultIANAEncoding; |
|
if (fReportErrors) { |
|
fErrorReporter.reportWarning("HTML1000", null); |
|
} |
|
} |
|
if (encodings[1] == null) { |
|
encodings[1] = EncodingMap.getIANA2JavaMapping(encodings[0].toUpperCase(Locale.ENGLISH)); |
|
if (encodings[1] == null) { |
|
encodings[1] = encodings[0]; |
|
if (fReportErrors) { |
|
fErrorReporter.reportWarning("HTML1001", new Object[]{encodings[0]}); |
|
} |
|
} |
|
} |
|
fIANAEncoding = encodings[0]; |
|
fJavaEncoding = encodings[1]; |
|
|
|
fIso8859Encoding = fIANAEncoding == null |
|
|| fIANAEncoding.toUpperCase(Locale.ENGLISH).startsWith("ISO-8859") |
|
|| fIANAEncoding.equalsIgnoreCase(fDefaultIANAEncoding); |
|
encoding = fIANAEncoding; |
|
reader = new InputStreamReader(fByteStream, fJavaEncoding); |
|
} |
|
fCurrentEntity = new CurrentEntity(reader, encoding, |
|
publicId, baseSystemId, |
|
literalSystemId, expandedSystemId); |
|
|
|
|
|
setScanner(fContentScanner); |
|
setScannerState(STATE_START_DOCUMENT); |
|
|
|
} |
|
|
|
|
|
public boolean scanDocument(boolean complete) throws XNIException, IOException { |
|
do { |
|
if (!fScanner.scan(complete)) { |
|
return false; |
|
} |
|
} while (complete); |
|
return true; |
|
} |
|
|
|
|
|
public void setDocumentHandler(XMLDocumentHandler handler) { |
|
fDocumentHandler = handler; |
|
} |
|
|
|
|
|
|
|
|
|
public XMLDocumentHandler getDocumentHandler() { |
|
return fDocumentHandler; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static String getValue(XMLAttributes attrs, String aname) { |
|
int length = attrs != null ? attrs.getLength() : 0; |
|
for (int i = 0; i < length; i++) { |
|
if (attrs.getQName(i).equalsIgnoreCase(aname)) { |
|
return attrs.getValue(i); |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String expandSystemId(String systemId, String baseSystemId) { |
|
|
|
|
|
if (systemId == null || systemId.length() == 0) { |
|
return systemId; |
|
} |
|
|
|
try { |
|
URI uri = new URI(systemId); |
|
if (uri != null) { |
|
return systemId; |
|
} |
|
} |
|
catch (URI.MalformedURIException e) { |
|
|
|
} |
|
|
|
String id = fixURI(systemId); |
|
|
|
|
|
URI base = null; |
|
URI uri = null; |
|
try { |
|
if (baseSystemId == null || baseSystemId.length() == 0 || |
|
baseSystemId.equals(systemId)) { |
|
String dir; |
|
try { |
|
dir = fixURI(System.getProperty("user.dir")); |
|
} |
|
catch (SecurityException se) { |
|
dir = ""; |
|
} |
|
if (!dir.endsWith("/")) { |
|
dir = dir + "/"; |
|
} |
|
base = new URI("file", "", dir, null, null); |
|
} |
|
else { |
|
try { |
|
base = new URI(fixURI(baseSystemId)); |
|
} |
|
catch (URI.MalformedURIException e) { |
|
String dir; |
|
try { |
|
dir = fixURI(System.getProperty("user.dir")); |
|
} |
|
catch (SecurityException se) { |
|
dir = ""; |
|
} |
|
if (baseSystemId.indexOf(':') != -1) { |
|
|
|
|
|
base = new URI("file", "", fixURI(baseSystemId), null, null); |
|
} |
|
else { |
|
if (!dir.endsWith("/")) { |
|
dir = dir + "/"; |
|
} |
|
dir = dir + fixURI(baseSystemId); |
|
base = new URI("file", "", dir, null, null); |
|
} |
|
} |
|
} |
|
|
|
uri = new URI(base, id); |
|
} |
|
catch (URI.MalformedURIException e) { |
|
|
|
} |
|
|
|
if (uri == null) { |
|
return systemId; |
|
} |
|
return uri.toString(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static String fixURI(String str) { |
|
|
|
|
|
str = str.replace(java.io.File.separatorChar, '/'); |
|
|
|
|
|
if (str.length() >= 2) { |
|
char ch1 = str.charAt(1); |
|
|
|
if (ch1 == ':') { |
|
final char ch0 = String.valueOf(str.charAt(0)).toUpperCase(Locale.ENGLISH).charAt(0); |
|
if (ch0 >= 'A' && ch0 <= 'Z') { |
|
str = "/" + str; |
|
} |
|
} |
|
|
|
else if (ch1 == '/' && str.charAt(0) == '/') { |
|
str = "file:" + str; |
|
} |
|
} |
|
|
|
|
|
return str; |
|
|
|
} |
|
|
|
|
|
protected static final String modifyName(String name, short mode) { |
|
switch (mode) { |
|
case NAMES_UPPERCASE: return name.toUpperCase(Locale.ENGLISH); |
|
case NAMES_LOWERCASE: return name.toLowerCase(Locale.ENGLISH); |
|
} |
|
return name; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static final short getNamesValue(String value) { |
|
if (value.equals("lower")) { |
|
return NAMES_LOWERCASE; |
|
} |
|
if (value.equals("upper")) { |
|
return NAMES_UPPERCASE; |
|
} |
|
return NAMES_NO_CHANGE; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected int fixWindowsCharacter(int origChar) { |
|
|
|
switch(origChar) { |
|
case 130: return 8218; |
|
case 131: return 402; |
|
case 132: return 8222; |
|
case 133: return 8230; |
|
case 134: return 8224; |
|
case 135: return 8225; |
|
case 136: return 710; |
|
case 137: return 8240; |
|
case 138: return 352; |
|
case 139: return 8249; |
|
case 140: return 338; |
|
case 145: return 8216; |
|
case 146: return 8217; |
|
case 147: return 8220; |
|
case 148: return 8221; |
|
case 149: return 8226; |
|
case 150: return 8211; |
|
case 151: return 8212; |
|
case 152: return 732; |
|
case 153: return 8482; |
|
case 154: return 353; |
|
case 155: return 8250; |
|
case 156: return 339; |
|
case 159: return 376; |
|
} |
|
return origChar; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected int read() throws IOException { |
|
return fCurrentEntity.read(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void setScanner(Scanner scanner) { |
|
fScanner = scanner; |
|
if (DEBUG_SCANNER) { |
|
System.out.print("$$$ setScanner("); |
|
System.out.print(scanner!=null?scanner.getClass().getName():"null"); |
|
System.out.println(");"); |
|
} |
|
} |
|
|
|
|
|
protected void setScannerState(short state) { |
|
fScannerState = state; |
|
if (DEBUG_SCANNER_STATE) { |
|
System.out.print("$$$ setScannerState("); |
|
switch (fScannerState) { |
|
case STATE_CONTENT: { System.out.print("STATE_CONTENT"); break; } |
|
case STATE_MARKUP_BRACKET: { System.out.print("STATE_MARKUP_BRACKET"); break; } |
|
case STATE_START_DOCUMENT: { System.out.print("STATE_START_DOCUMENT"); break; } |
|
case STATE_END_DOCUMENT: { System.out.print("STATE_END_DOCUMENT"); break; } |
|
} |
|
System.out.println(");"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
protected void scanDoctype() throws IOException { |
|
String root = null; |
|
String pubid = null; |
|
String sysid = null; |
|
|
|
if (skipSpaces()) { |
|
root = scanName(true); |
|
if (root == null) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1014", null); |
|
} |
|
} |
|
else { |
|
root = modifyName(root, fNamesElems); |
|
} |
|
if (skipSpaces()) { |
|
if (skip("PUBLIC", false)) { |
|
skipSpaces(); |
|
pubid = scanLiteral(); |
|
if (skipSpaces()) { |
|
sysid = scanLiteral(); |
|
} |
|
} |
|
else if (skip("SYSTEM", false)) { |
|
skipSpaces(); |
|
sysid = scanLiteral(); |
|
} |
|
} |
|
} |
|
int c; |
|
while ((c = fCurrentEntity.read()) != -1) { |
|
if (c == '<') { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
if (c == '>') { |
|
break; |
|
} |
|
if (c == '[') { |
|
skipMarkup(true); |
|
break; |
|
} |
|
} |
|
|
|
if (fDocumentHandler != null) { |
|
if (fOverrideDoctype) { |
|
pubid = fDoctypePubid; |
|
sysid = fDoctypeSysid; |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.doctypeDecl(root, pubid, sysid, locationAugs()); |
|
} |
|
|
|
} |
|
|
|
|
|
protected String scanLiteral() throws IOException { |
|
int quote = fCurrentEntity.read(); |
|
if (quote == '\'' || quote == '"') { |
|
StringBuffer str = new StringBuffer(); |
|
int c; |
|
while ((c = fCurrentEntity.read()) != -1) { |
|
if (c == quote) { |
|
break; |
|
} |
|
if (c == '\r' || c == '\n') { |
|
fCurrentEntity.rewind(); |
|
|
|
|
|
skipNewlines(); |
|
str.append(' '); |
|
} |
|
else if (c == '<') { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
else { |
|
appendChar(str, c); |
|
} |
|
} |
|
if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
throw new EOFException(); |
|
} |
|
return str.toString(); |
|
} |
|
fCurrentEntity.rewind(); |
|
return null; |
|
} |
|
|
|
|
|
protected String scanName(final boolean strict) throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(scanName: "); |
|
} |
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
if (fCurrentEntity.load(0) == -1) { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")scanName: "); |
|
} |
|
return null; |
|
} |
|
} |
|
int offset = fCurrentEntity.offset; |
|
while (true) { |
|
while (fCurrentEntity.hasNext()) { |
|
char c = fCurrentEntity.getNextChar(); |
|
if ((strict && (!Character.isLetterOrDigit(c) && c != '-' && c != '.' && c != ':' && c != '_')) |
|
|| (!strict && (Character.isWhitespace(c) || c == '=' || c == '/' || c == '>'))) { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
} |
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
int length = fCurrentEntity.length - offset; |
|
System.arraycopy(fCurrentEntity.buffer, offset, fCurrentEntity.buffer, 0, length); |
|
int count = fCurrentEntity.load(length); |
|
offset = 0; |
|
if (count == -1) { |
|
break; |
|
} |
|
} |
|
else { |
|
break; |
|
} |
|
} |
|
int length = fCurrentEntity.offset - offset; |
|
String name = length > 0 ? new String(fCurrentEntity.buffer, offset, length) : null; |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")scanName: ", " -> \"" + name + '"'); |
|
} |
|
return name; |
|
} |
|
|
|
|
|
protected int scanEntityRef(final XMLStringBuffer str, final boolean content) |
|
throws IOException { |
|
str.clear(); |
|
str.append('&'); |
|
boolean endsWithSemicolon = false; |
|
while (true) { |
|
int c = fCurrentEntity.read(); |
|
if (c == ';') { |
|
str.append(';'); |
|
endsWithSemicolon = true; |
|
break; |
|
} |
|
else if (c == -1) { |
|
break; |
|
} |
|
else if (!ENTITY_CHARS.get(c) && c != '#') { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
appendChar(str, c); |
|
} |
|
|
|
if (!endsWithSemicolon) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportWarning("HTML1004", null); |
|
} |
|
} |
|
if (str.length == 1) { |
|
if (content && fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.characters(str, locationAugs()); |
|
} |
|
return -1; |
|
} |
|
|
|
final String name; |
|
if (endsWithSemicolon) |
|
name = str.toString().substring(1, str.length -1); |
|
else |
|
name = str.toString().substring(1); |
|
|
|
if (name.startsWith("#")) { |
|
int value = -1; |
|
try { |
|
if (name.startsWith("#x") || name.startsWith("#X")) { |
|
value = Integer.parseInt(name.substring(2), 16); |
|
} |
|
else { |
|
value = Integer.parseInt(name.substring(1)); |
|
} |
|
|
|
if (fFixWindowsCharRefs && fIso8859Encoding) { |
|
value = fixWindowsCharacter(value); |
|
} |
|
if (content && fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
if (fNotifyCharRefs) { |
|
XMLResourceIdentifier id = resourceId(); |
|
String encoding = null; |
|
fDocumentHandler.startGeneralEntity(name, id, encoding, locationAugs()); |
|
} |
|
str.clear(); |
|
try { |
|
appendChar(str, value); |
|
} |
|
catch (final IllegalArgumentException e) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1005", new Object[]{name}); |
|
} |
|
str.append(REPLACEMENT_CHARACTER); |
|
} |
|
fDocumentHandler.characters(str, locationAugs()); |
|
if (fNotifyCharRefs) { |
|
fDocumentHandler.endGeneralEntity(name, locationAugs()); |
|
} |
|
} |
|
} |
|
catch (NumberFormatException e) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1005", new Object[]{name}); |
|
} |
|
if (content && fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.characters(str, locationAugs()); |
|
} |
|
} |
|
return value; |
|
} |
|
|
|
int c = HTMLEntities.get(name); |
|
|
|
|
|
|
|
final boolean invalidEntityInAttribute = !content && !endsWithSemicolon && c > 256; |
|
if (c == -1 || invalidEntityInAttribute) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportWarning("HTML1006", new Object[]{name}); |
|
} |
|
if (content && fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.characters(str, locationAugs()); |
|
} |
|
return -1; |
|
} |
|
if (content && fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
boolean notify = fNotifyHtmlBuiltinRefs || (fNotifyXmlBuiltinRefs && builtinXmlRef(name)); |
|
if (notify) { |
|
XMLResourceIdentifier id = resourceId(); |
|
String encoding = null; |
|
fDocumentHandler.startGeneralEntity(name, id, encoding, locationAugs()); |
|
} |
|
str.clear(); |
|
appendChar(str, c); |
|
fDocumentHandler.characters(str, locationAugs()); |
|
if (notify) { |
|
fDocumentHandler.endGeneralEntity(name, locationAugs()); |
|
} |
|
} |
|
return c; |
|
|
|
} |
|
|
|
|
|
protected boolean skip(String s, boolean caseSensitive) throws IOException { |
|
int length = s != null ? s.length() : 0; |
|
for (int i = 0; i < length; i++) { |
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
System.arraycopy(fCurrentEntity.buffer, fCurrentEntity.offset - i, fCurrentEntity.buffer, 0, i); |
|
if (fCurrentEntity.load(i) == -1) { |
|
fCurrentEntity.offset = 0; |
|
return false; |
|
} |
|
} |
|
char c0 = s.charAt(i); |
|
char c1 = fCurrentEntity.getNextChar(); |
|
if (!caseSensitive) { |
|
c0 = String.valueOf(c0).toUpperCase(Locale.ENGLISH).charAt(0); |
|
c1 = String.valueOf(c1).toUpperCase(Locale.ENGLISH).charAt(0); |
|
} |
|
if (c0 != c1) { |
|
fCurrentEntity.rewind(i + 1); |
|
return false; |
|
} |
|
} |
|
return true; |
|
} |
|
|
|
|
|
protected boolean skipMarkup(boolean balance) throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(skipMarkup: "); |
|
} |
|
int depth = 1; |
|
boolean slashgt = false; |
|
OUTER: while (true) { |
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
if (fCurrentEntity.load(0) == -1) { |
|
break OUTER; |
|
} |
|
} |
|
while (fCurrentEntity.hasNext()) { |
|
char c = fCurrentEntity.getNextChar(); |
|
if (balance && c == '<') { |
|
depth++; |
|
} |
|
else if (c == '>') { |
|
depth--; |
|
if (depth == 0) { |
|
break OUTER; |
|
} |
|
} |
|
else if (c == '/') { |
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
if (fCurrentEntity.load(0) == -1) { |
|
break OUTER; |
|
} |
|
} |
|
c = fCurrentEntity.getNextChar(); |
|
if (c == '>') { |
|
slashgt = true; |
|
depth--; |
|
if (depth == 0) { |
|
break OUTER; |
|
} |
|
} |
|
else { |
|
fCurrentEntity.rewind(); |
|
} |
|
} |
|
else if (c == '\r' || c == '\n') { |
|
fCurrentEntity.rewind(); |
|
skipNewlines(); |
|
} |
|
} |
|
} |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")skipMarkup: ", " -> " + slashgt); |
|
} |
|
return slashgt; |
|
} |
|
|
|
|
|
protected boolean skipSpaces() throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(skipSpaces: "); |
|
} |
|
boolean spaces = false; |
|
while (true) { |
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
if (fCurrentEntity.load(0) == -1) { |
|
break; |
|
} |
|
} |
|
char c = fCurrentEntity.getNextChar(); |
|
if (!Character.isWhitespace(c)) { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
spaces = true; |
|
if (c == '\r' || c == '\n') { |
|
fCurrentEntity.rewind(); |
|
skipNewlines(); |
|
continue; |
|
} |
|
} |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")skipSpaces: ", " -> " + spaces); |
|
} |
|
return spaces; |
|
} |
|
|
|
|
|
protected int skipNewlines() throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(skipNewlines: "); |
|
} |
|
|
|
if (!fCurrentEntity.hasNext()) { |
|
if (fCurrentEntity.load(0) == -1) { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")skipNewlines: "); |
|
} |
|
return 0; |
|
} |
|
} |
|
char c = fCurrentEntity.getCurrentChar(); |
|
int newlines = 0; |
|
int offset = fCurrentEntity.offset; |
|
if (c == '\n' || c == '\r') { |
|
do { |
|
c = fCurrentEntity.getNextChar(); |
|
if (c == '\r') { |
|
newlines++; |
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
offset = 0; |
|
fCurrentEntity.offset = newlines; |
|
if (fCurrentEntity.load(newlines) == -1) { |
|
break; |
|
} |
|
} |
|
if (fCurrentEntity.getCurrentChar() == '\n') { |
|
fCurrentEntity.offset++; |
|
fCurrentEntity.characterOffset_++; |
|
offset++; |
|
} |
|
} |
|
else if (c == '\n') { |
|
newlines++; |
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
offset = 0; |
|
fCurrentEntity.offset = newlines; |
|
if (fCurrentEntity.load(newlines) == -1) { |
|
break; |
|
} |
|
} |
|
} |
|
else { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
} while (fCurrentEntity.offset < fCurrentEntity.length - 1); |
|
fCurrentEntity.incLine(newlines); |
|
} |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")skipNewlines: ", " -> " + newlines); |
|
} |
|
return newlines; |
|
} |
|
|
|
|
|
|
|
|
|
protected final Augmentations locationAugs() { |
|
HTMLAugmentations augs = null; |
|
if (fAugmentations) { |
|
fLocationItem.setValues(fBeginLineNumber, fBeginColumnNumber, |
|
fBeginCharacterOffset, fEndLineNumber, |
|
fEndColumnNumber, fEndCharacterOffset); |
|
augs = fInfosetAugs; |
|
augs.removeAllItems(); |
|
augs.putItem(AUGMENTATIONS, fLocationItem); |
|
} |
|
return augs; |
|
} |
|
|
|
|
|
protected final Augmentations synthesizedAugs() { |
|
HTMLAugmentations augs = null; |
|
if (fAugmentations) { |
|
augs = fInfosetAugs; |
|
augs.removeAllItems(); |
|
augs.putItem(AUGMENTATIONS, SYNTHESIZED_ITEM); |
|
} |
|
return augs; |
|
} |
|
|
|
|
|
protected final XMLResourceIdentifier resourceId() { |
|
|
|
fResourceId.clear(); |
|
return fResourceId; |
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static boolean builtinXmlRef(String name) { |
|
return name.equals("amp") || name.equals("lt") || name.equals("gt") || |
|
name.equals("quot") || name.equals("apos"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void appendChar( XMLStringBuffer str, int value ) |
|
{ |
|
if ( value > Character.MAX_VALUE ) |
|
{ |
|
char[] chars = Character.toChars( value ); |
|
|
|
str.append( chars, 0, chars.length ); |
|
} |
|
else |
|
{ |
|
str.append( (char) value ); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void appendChar( StringBuffer str, int value ) |
|
{ |
|
if ( value > Character.MAX_VALUE ) |
|
{ |
|
char[] chars = Character.toChars( value ); |
|
|
|
str.append( chars, 0, chars.length ); |
|
} |
|
else |
|
{ |
|
str.append( (char) value ); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface Scanner { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean scan(boolean complete) throws IOException; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class CurrentEntity { |
|
|
|
|
|
|
|
|
|
|
|
|
|
private Reader stream_; |
|
|
|
|
|
private String encoding; |
|
|
|
|
|
public final String publicId; |
|
|
|
|
|
public final String baseSystemId; |
|
|
|
|
|
public final String literalSystemId; |
|
|
|
|
|
public final String expandedSystemId; |
|
|
|
|
|
public final String version = "1.0"; |
|
|
|
|
|
private int lineNumber_ = 1; |
|
|
|
|
|
private int columnNumber_ = 1; |
|
|
|
|
|
public int characterOffset_ = 0; |
|
|
|
|
|
|
|
|
|
public char[] buffer = new char[DEFAULT_BUFFER_SIZE]; |
|
|
|
|
|
public int offset = 0; |
|
|
|
|
|
public int length = 0; |
|
|
|
private boolean endReached_ = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
public CurrentEntity(Reader stream, String encoding, |
|
String publicId, String baseSystemId, |
|
String literalSystemId, String expandedSystemId) { |
|
stream_ = stream; |
|
this.encoding = encoding; |
|
this.publicId = publicId; |
|
this.baseSystemId = baseSystemId; |
|
this.literalSystemId = literalSystemId; |
|
this.expandedSystemId = expandedSystemId; |
|
} |
|
|
|
private char getCurrentChar() { |
|
return buffer[offset]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
private char getNextChar() { |
|
characterOffset_++; |
|
columnNumber_++; |
|
return buffer[offset++]; |
|
} |
|
private void closeQuietly() { |
|
try { |
|
stream_.close(); |
|
} |
|
catch (IOException e) { |
|
|
|
} |
|
} |
|
|
|
|
|
|
|
|
|
boolean hasNext() { |
|
return offset < length; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected int load(int offset) throws IOException { |
|
if (DEBUG_BUFFER) { |
|
debugBufferIfNeeded("(load: "); |
|
} |
|
|
|
if (offset == buffer.length) { |
|
int adjust = buffer.length / 4; |
|
char[] array = new char[buffer.length + adjust]; |
|
System.arraycopy(buffer, 0, array, 0, length); |
|
buffer = array; |
|
} |
|
|
|
int count = stream_.read(buffer, offset, buffer.length - offset); |
|
if (count == -1) { |
|
endReached_ = true; |
|
} |
|
length = count != -1 ? count + offset : offset; |
|
this.offset = offset; |
|
if (DEBUG_BUFFER) { |
|
debugBufferIfNeeded(")load: ", " -> " + count); |
|
} |
|
return count; |
|
} |
|
|
|
|
|
protected int read() throws IOException { |
|
if (DEBUG_BUFFER) { |
|
debugBufferIfNeeded("(read: "); |
|
} |
|
if (offset == length) { |
|
if (endReached_) { |
|
return -1; |
|
} |
|
if (load(0) == -1) { |
|
if (DEBUG_BUFFER) { |
|
System.out.println(")read: -> -1"); |
|
} |
|
return -1; |
|
} |
|
} |
|
final char c = buffer[offset++]; |
|
characterOffset_++; |
|
columnNumber_++; |
|
|
|
if (DEBUG_BUFFER) { |
|
debugBufferIfNeeded(")read: ", " -> " + c); |
|
} |
|
return c; |
|
} |
|
|
|
|
|
private void debugBufferIfNeeded(final String prefix) { |
|
debugBufferIfNeeded(prefix, ""); |
|
} |
|
|
|
private void debugBufferIfNeeded(final String prefix, final String suffix) { |
|
if (DEBUG_BUFFER) { |
|
System.out.print(prefix); |
|
System.out.print('['); |
|
System.out.print(length); |
|
System.out.print(' '); |
|
System.out.print(offset); |
|
if (length > 0) { |
|
System.out.print(" \""); |
|
for (int i = 0; i < length; i++) { |
|
if (i == offset) { |
|
System.out.print('^'); |
|
} |
|
char c = buffer[i]; |
|
switch (c) { |
|
case '\r': { |
|
System.out.print("\\r"); |
|
break; |
|
} |
|
case '\n': { |
|
System.out.print("\\n"); |
|
break; |
|
} |
|
case '\t': { |
|
System.out.print("\\t"); |
|
break; |
|
} |
|
case '"': { |
|
System.out.print("\\\""); |
|
break; |
|
} |
|
default: { |
|
System.out.print(c); |
|
} |
|
} |
|
} |
|
if (offset == length) { |
|
System.out.print('^'); |
|
} |
|
System.out.print('"'); |
|
} |
|
System.out.print(']'); |
|
System.out.print(suffix); |
|
System.out.println(); |
|
} |
|
} |
|
|
|
private void setStream(final InputStreamReader inputStreamReader) { |
|
stream_ = inputStreamReader; |
|
offset = length = characterOffset_ = 0; |
|
lineNumber_ = columnNumber_ = 1; |
|
encoding = inputStreamReader.getEncoding(); |
|
} |
|
|
|
|
|
|
|
|
|
private void rewind() { |
|
offset--; |
|
characterOffset_--; |
|
columnNumber_--; |
|
} |
|
private void rewind(int i) { |
|
offset -= i; |
|
characterOffset_ -= i; |
|
columnNumber_ -= i; |
|
} |
|
|
|
private void incLine() { |
|
lineNumber_++; |
|
columnNumber_ = 1; |
|
} |
|
|
|
private void incLine(int nbLines) { |
|
lineNumber_ += nbLines; |
|
columnNumber_ = 1; |
|
} |
|
|
|
public int getLineNumber() { |
|
return lineNumber_; |
|
} |
|
|
|
private void resetBuffer(final XMLStringBuffer buffer, final int lineNumber, |
|
final int columnNumber, final int characterOffset) { |
|
lineNumber_ = lineNumber; |
|
columnNumber_ = columnNumber; |
|
this.characterOffset_ = characterOffset; |
|
this.buffer = buffer.ch; |
|
this.offset = buffer.offset; |
|
this.length = buffer.length; |
|
} |
|
|
|
private int getColumnNumber() { |
|
return columnNumber_; |
|
} |
|
|
|
private void restorePosition(int originalOffset, |
|
int originalColumnNumber, int originalCharacterOffset) { |
|
this.offset = originalOffset; |
|
this.columnNumber_ = originalColumnNumber; |
|
this.characterOffset_ = originalCharacterOffset; |
|
} |
|
|
|
private int getCharacterOffset() { |
|
return characterOffset_; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ContentScanner |
|
implements Scanner { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final QName fQName = new QName(); |
|
|
|
|
|
private final XMLAttributesImpl fAttributes = new XMLAttributesImpl(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean scan(boolean complete) throws IOException { |
|
boolean next; |
|
do { |
|
try { |
|
next = false; |
|
switch (fScannerState) { |
|
case STATE_CONTENT: { |
|
fBeginLineNumber = fCurrentEntity.getLineNumber(); |
|
fBeginColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fBeginCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
int c = fCurrentEntity.read(); |
|
if (c == '<') { |
|
setScannerState(STATE_MARKUP_BRACKET); |
|
next = true; |
|
} |
|
else if (c == '&') { |
|
scanEntityRef(fStringBuffer, true); |
|
} |
|
else if (c == -1) { |
|
throw new EOFException(); |
|
} |
|
else { |
|
fCurrentEntity.rewind(); |
|
scanCharacters(); |
|
} |
|
break; |
|
} |
|
case STATE_MARKUP_BRACKET: { |
|
int c = fCurrentEntity.read(); |
|
if (c == '!') { |
|
if (skip("--", false)) { |
|
scanComment(); |
|
} |
|
else if (skip("[CDATA[", false)) { |
|
scanCDATA(); |
|
} |
|
else if (skip("DOCTYPE", false)) { |
|
scanDoctype(); |
|
} |
|
else { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1002", null); |
|
} |
|
skipMarkup(true); |
|
} |
|
} |
|
else if (c == '?') { |
|
scanPI(); |
|
} |
|
else if (c == '/') { |
|
scanEndElement(); |
|
} |
|
else if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1003", null); |
|
} |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fStringBuffer.clear(); |
|
fStringBuffer.append('<'); |
|
fDocumentHandler.characters(fStringBuffer, null); |
|
} |
|
throw new EOFException(); |
|
} |
|
else { |
|
fCurrentEntity.rewind(); |
|
fElementCount++; |
|
fSingleBoolean[0] = false; |
|
final String ename = scanStartElement(fSingleBoolean); |
|
final String enameLC = ename == null ? null : ename.toLowerCase(); |
|
fBeginLineNumber = fCurrentEntity.getLineNumber(); |
|
fBeginColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fBeginCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
if ("script".equals(enameLC)) { |
|
scanScriptContent(); |
|
} |
|
else if (!fAllowSelfclosingTags && !fAllowSelfclosingIframe && "iframe".equals(enameLC)) { |
|
scanUntilEndTag("iframe"); |
|
} |
|
else if (!fParseNoScriptContent && "noscript".equals(enameLC)) { |
|
scanUntilEndTag("noscript"); |
|
} |
|
else if (!fParseNoFramesContent && "noframes".equals(enameLC)) { |
|
scanUntilEndTag("noframes"); |
|
} |
|
else if (ename != null && !fSingleBoolean[0] |
|
&& HTMLElements.getElement(enameLC).isSpecial() |
|
&& (!ename.equalsIgnoreCase("TITLE") || isEnded(enameLC))) { |
|
setScanner(fSpecialScanner.setElementName(ename)); |
|
setScannerState(STATE_CONTENT); |
|
return true; |
|
} |
|
} |
|
setScannerState(STATE_CONTENT); |
|
break; |
|
} |
|
case STATE_START_DOCUMENT: { |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("startDocument()"); |
|
} |
|
XMLLocator locator = HTMLScanner.this; |
|
String encoding = fIANAEncoding; |
|
Augmentations augs = locationAugs(); |
|
NamespaceContext nscontext = new NamespaceSupport(); |
|
XercesBridge.getInstance().XMLDocumentHandler_startDocument(fDocumentHandler, locator, encoding, nscontext, augs); |
|
} |
|
if (fInsertDoctype && fDocumentHandler != null) { |
|
String root = HTMLElements.getElement(HTMLElements.HTML).name; |
|
root = modifyName(root, fNamesElems); |
|
String pubid = fDoctypePubid; |
|
String sysid = fDoctypeSysid; |
|
fDocumentHandler.doctypeDecl(root, pubid, sysid, |
|
synthesizedAugs()); |
|
} |
|
setScannerState(STATE_CONTENT); |
|
break; |
|
} |
|
case STATE_END_DOCUMENT: { |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth && complete) { |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("endDocument()"); |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.endDocument(locationAugs()); |
|
} |
|
return false; |
|
} |
|
default: { |
|
throw new RuntimeException("unknown scanner state: "+fScannerState); |
|
} |
|
} |
|
} |
|
catch (EOFException e) { |
|
if (fCurrentEntityStack.empty()) { |
|
setScannerState(STATE_END_DOCUMENT); |
|
} |
|
else { |
|
fCurrentEntity = (CurrentEntity)fCurrentEntityStack.pop(); |
|
} |
|
next = true; |
|
} |
|
} while (next || complete); |
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void scanUntilEndTag(final String tagName) throws IOException { |
|
final XMLStringBuffer buffer = new XMLStringBuffer(); |
|
final String end = "/" + tagName; |
|
final int lengthToScan = tagName.length() + 2; |
|
|
|
while (true) { |
|
int c = fCurrentEntity.read(); |
|
if (c == -1) { |
|
break; |
|
} |
|
if (c == '<') { |
|
final String next = nextContent(lengthToScan) + " "; |
|
if (next.length() >= lengthToScan && end.equalsIgnoreCase(next.substring(0, end.length())) |
|
&& ('>' == next.charAt(lengthToScan - 1) || Character.isWhitespace(next.charAt(lengthToScan - 1)))) { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
} |
|
if (c == '\r' || c == '\n') { |
|
fCurrentEntity.rewind(); |
|
int newlines = skipNewlines(); |
|
for (int i = 0; i < newlines; i++) { |
|
buffer.append('\n'); |
|
} |
|
} |
|
else { |
|
appendChar(buffer, c); |
|
} |
|
} |
|
if (buffer.length > 0 && fDocumentHandler != null) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.characters(buffer, locationAugs()); |
|
} |
|
} |
|
|
|
private void scanScriptContent() throws IOException { |
|
|
|
final XMLStringBuffer buffer = new XMLStringBuffer(); |
|
boolean waitForEndComment = false; |
|
while (true) { |
|
int c = fCurrentEntity.read(); |
|
if (c == -1) { |
|
break; |
|
} |
|
else if (c == '-' && endsWith(buffer, "<!-")) |
|
{ |
|
waitForEndComment = endCommentAvailable(); |
|
} |
|
else if (!waitForEndComment && c == '<') { |
|
final String next = nextContent(8) + " "; |
|
if (next.length() >= 8 && "/script".equalsIgnoreCase(next.substring(0, 7)) |
|
&& ('>' == next.charAt(7) || Character.isWhitespace(next.charAt(7)))) { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
} |
|
else if (c == '>' && endsWith(buffer, "--")) { |
|
waitForEndComment = false; |
|
} |
|
|
|
if (c == '\r' || c == '\n') { |
|
fCurrentEntity.rewind(); |
|
int newlines = skipNewlines(); |
|
for (int i = 0; i < newlines; i++) { |
|
buffer.append('\n'); |
|
} |
|
} |
|
else { |
|
appendChar(buffer, c); |
|
} |
|
} |
|
|
|
if (fScriptStripCommentDelims) { |
|
reduceToContent(buffer, "<!--", "-->"); |
|
} |
|
if (fScriptStripCDATADelims) { |
|
reduceToContent(buffer, "<![CDATA[", "]]>"); |
|
} |
|
|
|
if (buffer.length > 0 && fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("characters("+buffer+")"); |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.characters(buffer, locationAugs()); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected String nextContent(int len) throws IOException { |
|
final int originalOffset = fCurrentEntity.offset; |
|
final int originalColumnNumber = fCurrentEntity.getColumnNumber(); |
|
final int originalCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
|
|
char[] buff = new char[len]; |
|
int nbRead = 0; |
|
for (nbRead=0; nbRead<len; ++nbRead) { |
|
|
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
if (fCurrentEntity.length == fCurrentEntity.buffer.length) { |
|
fCurrentEntity.load(fCurrentEntity.buffer.length); |
|
} |
|
else { |
|
break; |
|
} |
|
} |
|
|
|
int c = fCurrentEntity.read(); |
|
if (c == -1) { |
|
break; |
|
} |
|
buff[nbRead] = (char) c; |
|
} |
|
fCurrentEntity.restorePosition(originalOffset, originalColumnNumber, originalCharacterOffset); |
|
return new String(buff, 0, nbRead); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void scanCharacters() throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(scanCharacters: "); |
|
} |
|
fStringBuffer.clear(); |
|
while(true) { |
|
int newlines = skipNewlines(); |
|
if (newlines == 0 && fCurrentEntity.offset == fCurrentEntity.length) { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")scanCharacters: "); |
|
} |
|
break; |
|
} |
|
char c; |
|
int offset = fCurrentEntity.offset - newlines; |
|
for (int i = offset; i < fCurrentEntity.offset; i++) { |
|
fCurrentEntity.buffer[i] = '\n'; |
|
} |
|
while (fCurrentEntity.hasNext()) { |
|
c = fCurrentEntity.getNextChar(); |
|
if (c == '<' || c == '&' || c == '\n' || c == '\r') { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
} |
|
if (fCurrentEntity.offset > offset && |
|
fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
if (DEBUG_CALLBACKS) { |
|
final XMLString xmlString = new XMLString(fCurrentEntity.buffer, offset, fCurrentEntity.offset - offset); |
|
System.out.println("characters(" + xmlString + ")"); |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fStringBuffer.append(fCurrentEntity.buffer, offset, fCurrentEntity.offset - offset); |
|
} |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")scanCharacters: "); |
|
} |
|
|
|
boolean hasNext = fCurrentEntity.offset < fCurrentEntity.buffer.length; |
|
int next = hasNext ? fCurrentEntity.getCurrentChar() : -1; |
|
|
|
if(next == '&' || next == '<' || next == -1) { |
|
break; |
|
} |
|
|
|
} |
|
|
|
if(fStringBuffer.length != 0) { |
|
fDocumentHandler.characters(fStringBuffer, locationAugs()); |
|
} |
|
|
|
} |
|
|
|
|
|
protected void scanCDATA() throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(scanCDATA: "); |
|
} |
|
fStringBuffer.clear(); |
|
if (fCDATASections) { |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("startCDATA()"); |
|
} |
|
fDocumentHandler.startCDATA(locationAugs()); |
|
} |
|
} |
|
else { |
|
fStringBuffer.append("[CDATA["); |
|
} |
|
boolean eof = scanMarkupContent(fStringBuffer, ']'); |
|
if (!fCDATASections) { |
|
fStringBuffer.append("]]"); |
|
} |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
if (fCDATASections) { |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("characters("+fStringBuffer+")"); |
|
} |
|
fDocumentHandler.characters(fStringBuffer, locationAugs()); |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("endCDATA()"); |
|
} |
|
fDocumentHandler.endCDATA(locationAugs()); |
|
} |
|
else { |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("comment("+fStringBuffer+")"); |
|
} |
|
fDocumentHandler.comment(fStringBuffer, locationAugs()); |
|
} |
|
} |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")scanCDATA: "); |
|
} |
|
if (eof) { |
|
throw new EOFException(); |
|
} |
|
} |
|
|
|
|
|
protected void scanComment() throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(scanComment: "); |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
XMLStringBuffer buffer = new XMLStringBuffer(); |
|
boolean eof = scanMarkupContent(buffer, '-'); |
|
|
|
if (eof) { |
|
fCurrentEntity.resetBuffer(buffer, fEndLineNumber, fEndColumnNumber, fEndCharacterOffset); |
|
buffer = new XMLStringBuffer(); |
|
while (true) { |
|
int c = fCurrentEntity.read(); |
|
if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
eof = true; |
|
break; |
|
} |
|
else if (c != '>') { |
|
appendChar(buffer, c); |
|
continue; |
|
} |
|
else if (c == '\n' || c == '\r') { |
|
fCurrentEntity.rewind(); |
|
int newlines = skipNewlines(); |
|
for (int i = 0; i < newlines; i++) { |
|
buffer.append('\n'); |
|
} |
|
continue; |
|
} |
|
eof = false; |
|
break; |
|
} |
|
} |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("comment(" + buffer + ")"); |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.comment(buffer, locationAugs()); |
|
} |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")scanComment: "); |
|
} |
|
if (eof) { |
|
throw new EOFException(); |
|
} |
|
} |
|
|
|
|
|
protected boolean scanMarkupContent(XMLStringBuffer buffer, |
|
char cend) throws IOException { |
|
int c = -1; |
|
OUTER: while (true) { |
|
c = fCurrentEntity.read(); |
|
if (c == cend) { |
|
int count = 1; |
|
while (true) { |
|
c = fCurrentEntity.read(); |
|
if (c == cend) { |
|
count++; |
|
continue; |
|
} |
|
break; |
|
} |
|
if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
break OUTER; |
|
} |
|
if (count < 2) { |
|
buffer.append(cend); |
|
|
|
fCurrentEntity.rewind(); |
|
|
|
continue; |
|
} |
|
if (c != '>') { |
|
for (int i = 0; i < count; i++) { |
|
buffer.append(cend); |
|
} |
|
fCurrentEntity.rewind(); |
|
continue; |
|
} |
|
for (int i = 0; i < count - 2; i++) { |
|
buffer.append(cend); |
|
} |
|
break; |
|
} |
|
else if (c == '\n' || c == '\r') { |
|
fCurrentEntity.rewind(); |
|
int newlines = skipNewlines(); |
|
for (int i = 0; i < newlines; i++) { |
|
buffer.append('\n'); |
|
} |
|
continue; |
|
} |
|
else if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
break; |
|
} |
|
appendChar(buffer, c); |
|
} |
|
return c == -1; |
|
} |
|
|
|
|
|
protected void scanPI() throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(scanPI: "); |
|
} |
|
if (fReportErrors) { |
|
fErrorReporter.reportWarning("HTML1008", null); |
|
} |
|
|
|
|
|
String target = scanName(true); |
|
if (target != null && !target.equalsIgnoreCase("xml")) { |
|
while (true) { |
|
int c = fCurrentEntity.read(); |
|
if (c == '\r' || c == '\n') { |
|
if (c == '\r') { |
|
c = fCurrentEntity.read(); |
|
if (c != '\n') { |
|
fCurrentEntity.offset--; |
|
fCurrentEntity.characterOffset_--; |
|
} |
|
} |
|
fCurrentEntity.incLine(); |
|
continue; |
|
} |
|
if (c == -1) { |
|
break; |
|
} |
|
if (c != ' ' && c != '\t') { |
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
} |
|
fStringBuffer.clear(); |
|
while (true) { |
|
int c = fCurrentEntity.read(); |
|
if (c == '?' || c == '/') { |
|
char c0 = (char)c; |
|
c = fCurrentEntity.read(); |
|
|
|
|
|
|
|
if (c == '>' || c == -1) { |
|
break; |
|
} |
|
fStringBuffer.append(c0); |
|
fCurrentEntity.rewind(); |
|
continue; |
|
} |
|
else if (c == '\r' || c == '\n') { |
|
fStringBuffer.append('\n'); |
|
if (c == '\r') { |
|
c = fCurrentEntity.read(); |
|
if (c != '\n') { |
|
fCurrentEntity.offset--; |
|
fCurrentEntity.characterOffset_--; |
|
} |
|
} |
|
fCurrentEntity.incLine(); |
|
continue; |
|
} |
|
else if (c == -1) { |
|
break; |
|
} |
|
else { |
|
appendChar(fStringBuffer, c); |
|
} |
|
} |
|
XMLString data = fStringBuffer; |
|
if (fDocumentHandler != null) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.processingInstruction(target, data, locationAugs()); |
|
} |
|
} |
|
|
|
|
|
else { |
|
int beginLineNumber = fBeginLineNumber; |
|
int beginColumnNumber = fBeginColumnNumber; |
|
int beginCharacterOffset = fBeginCharacterOffset; |
|
fAttributes.removeAllAttributes(); |
|
int aindex = 0; |
|
while (scanPseudoAttribute(fAttributes)) { |
|
|
|
if (fAttributes.getValue(aindex).length() == 0) { |
|
fAttributes.removeAttributeAt(aindex); |
|
} |
|
else { |
|
fAttributes.getName(aindex,fQName); |
|
fQName.rawname = fQName.rawname.toLowerCase(); |
|
fAttributes.setName(aindex,fQName); |
|
aindex++; |
|
} |
|
} |
|
if (fDocumentHandler != null) { |
|
String version = fAttributes.getValue("version"); |
|
String encoding = fAttributes.getValue("encoding"); |
|
String standalone = fAttributes.getValue("standalone"); |
|
|
|
|
|
|
|
final boolean xmlDeclNow = fIgnoreSpecifiedCharset || !changeEncoding(encoding); |
|
if (xmlDeclNow) { |
|
fBeginLineNumber = beginLineNumber; |
|
fBeginColumnNumber = beginColumnNumber; |
|
fBeginCharacterOffset = beginCharacterOffset; |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.xmlDecl(version, encoding, standalone, |
|
locationAugs()); |
|
} |
|
} |
|
} |
|
|
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")scanPI: "); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected String scanStartElement(boolean[] empty) throws IOException { |
|
String ename = scanName(true); |
|
int length = ename != null ? ename.length() : 0; |
|
int c = length > 0 ? ename.charAt(0) : -1; |
|
if (length == 0 || !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1009", null); |
|
} |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fStringBuffer.clear(); |
|
fStringBuffer.append('<'); |
|
if (length > 0) { |
|
fStringBuffer.append(ename); |
|
} |
|
fDocumentHandler.characters(fStringBuffer, null); |
|
} |
|
return null; |
|
} |
|
ename = modifyName(ename, fNamesElems); |
|
fAttributes.removeAllAttributes(); |
|
int beginLineNumber = fBeginLineNumber; |
|
int beginColumnNumber = fBeginColumnNumber; |
|
int beginCharacterOffset = fBeginCharacterOffset; |
|
while (scanAttribute(fAttributes, empty)) { |
|
|
|
} |
|
fBeginLineNumber = beginLineNumber; |
|
fBeginColumnNumber = beginColumnNumber; |
|
fBeginCharacterOffset = beginCharacterOffset; |
|
if (fByteStream != null && fElementDepth == -1) { |
|
if (ename.equalsIgnoreCase("META") && !fIgnoreSpecifiedCharset) { |
|
if (DEBUG_CHARSET) { |
|
System.out.println("+++ <META>"); |
|
} |
|
String httpEquiv = getValue(fAttributes, "http-equiv"); |
|
if (httpEquiv != null && httpEquiv.equalsIgnoreCase("content-type")) { |
|
if (DEBUG_CHARSET) { |
|
System.out.println("+++ @content-type: \""+httpEquiv+'"'); |
|
} |
|
String content = getValue(fAttributes, "content"); |
|
if (content != null) { |
|
content = removeSpaces(content); |
|
int index1 = content.toLowerCase().indexOf("charset="); |
|
if (index1 != -1) { |
|
final int index2 = content.indexOf(';', index1); |
|
final String charset = index2 != -1 ? content.substring(index1+8, index2) : content.substring(index1+8); |
|
changeEncoding(charset); |
|
} |
|
} |
|
} |
|
else { |
|
final String metaCharset = getValue(fAttributes, "charset"); |
|
if (metaCharset != null) { |
|
changeEncoding(metaCharset); |
|
} |
|
} |
|
} |
|
else if (ename.equalsIgnoreCase("BODY")) { |
|
fByteStream.clear(); |
|
fByteStream = null; |
|
} |
|
else { |
|
HTMLElements.Element element = HTMLElements.getElement(ename); |
|
if (element.parent != null && element.parent.length > 0) { |
|
if (element.parent[0].code == HTMLElements.BODY) { |
|
fByteStream.clear(); |
|
fByteStream = null; |
|
} |
|
} |
|
} |
|
} |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fQName.setValues(null, ename, ename, null); |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("startElement("+fQName+','+fAttributes+")"); |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
if (empty[0]) { |
|
fDocumentHandler.emptyElement(fQName, fAttributes, locationAugs()); |
|
} |
|
else { |
|
fDocumentHandler.startElement(fQName, fAttributes, locationAugs()); |
|
} |
|
} |
|
return ename; |
|
} |
|
|
|
|
|
|
|
|
|
private String removeSpaces(final String content) { |
|
StringBuffer sb = null; |
|
for (int i=content.length()-1; i>=0; --i) { |
|
if (Character.isWhitespace(content.charAt(i))) { |
|
if (sb == null) { |
|
sb = new StringBuffer(content); |
|
} |
|
sb.deleteCharAt(i); |
|
} |
|
} |
|
return (sb == null) ? content : sb.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean changeEncoding(String charset) { |
|
if (charset == null || fByteStream == null) { |
|
return false; |
|
} |
|
charset = charset.trim(); |
|
boolean encodingChanged = false; |
|
try { |
|
String ianaEncoding = charset; |
|
String javaEncoding = EncodingMap.getIANA2JavaMapping(ianaEncoding.toUpperCase(Locale.ENGLISH)); |
|
if (DEBUG_CHARSET) { |
|
System.out.println("+++ ianaEncoding: "+ianaEncoding); |
|
System.out.println("+++ javaEncoding: "+javaEncoding); |
|
} |
|
if (javaEncoding == null) { |
|
javaEncoding = ianaEncoding; |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1001", new Object[]{ianaEncoding}); |
|
} |
|
} |
|
|
|
if (!javaEncoding.equals(fJavaEncoding)) { |
|
if (!isEncodingCompatible(javaEncoding, fJavaEncoding)) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1015", new Object[]{javaEncoding,fJavaEncoding}); |
|
} |
|
} |
|
|
|
else { |
|
fIso8859Encoding = ianaEncoding == null |
|
|| ianaEncoding.toUpperCase(Locale.ENGLISH).startsWith("ISO-8859") |
|
|| ianaEncoding.equalsIgnoreCase(fDefaultIANAEncoding); |
|
fJavaEncoding = javaEncoding; |
|
fCurrentEntity.setStream(new InputStreamReader(fByteStream, javaEncoding)); |
|
fByteStream.playback(); |
|
fElementDepth = fElementCount; |
|
fElementCount = 0; |
|
encodingChanged = true; |
|
} |
|
} |
|
} |
|
catch (UnsupportedEncodingException e) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1010", new Object[]{charset}); |
|
} |
|
|
|
|
|
|
|
fByteStream.clear(); |
|
fByteStream = null; |
|
} |
|
return encodingChanged; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected boolean scanAttribute(XMLAttributesImpl attributes, |
|
boolean[] empty) |
|
throws IOException { |
|
return scanAttribute(attributes,empty,'/'); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected boolean scanPseudoAttribute(XMLAttributesImpl attributes) |
|
throws IOException { |
|
return scanAttribute(attributes,fSingleBoolean,'?'); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected boolean scanAttribute(XMLAttributesImpl attributes, |
|
boolean[] empty, char endc) |
|
throws IOException { |
|
boolean skippedSpaces = skipSpaces(); |
|
fBeginLineNumber = fCurrentEntity.getLineNumber(); |
|
fBeginColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fBeginCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
int c = fCurrentEntity.read(); |
|
if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
return false; |
|
} |
|
else if (c == '>') { |
|
return false; |
|
} |
|
else if(c == '<') { |
|
fCurrentEntity.rewind(); |
|
if(fReportErrors) { |
|
fErrorReporter.reportError("HTML2001", null); |
|
} |
|
return false; |
|
} |
|
fCurrentEntity.rewind(); |
|
String aname = scanName(false); |
|
if(aname == null) { |
|
empty[0] = skipMarkup(false); |
|
return false; |
|
} |
|
if (!skippedSpaces && fReportErrors) { |
|
fErrorReporter.reportError("HTML1013", new Object[] { aname }); |
|
} |
|
aname = modifyName(aname, fNamesAttrs); |
|
skipSpaces(); |
|
c = fCurrentEntity.read(); |
|
if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
throw new EOFException(); |
|
} |
|
if (c == '/' || c == '>') { |
|
fQName.setValues(null, aname, aname, null); |
|
attributes.addAttribute(fQName, "CDATA", ""); |
|
attributes.setSpecified(attributes.getLength()-1, true); |
|
if (fAugmentations) { |
|
addLocationItem(attributes, attributes.getLength() - 1); |
|
} |
|
if (c == '/') { |
|
fCurrentEntity.rewind(); |
|
empty[0] = skipMarkup(false); |
|
} |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (c == '=') { |
|
skipSpaces(); |
|
c = fCurrentEntity.read(); |
|
if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
throw new EOFException(); |
|
} |
|
|
|
if (c == '>') { |
|
fQName.setValues(null, aname, aname, null); |
|
attributes.addAttribute(fQName, "CDATA", ""); |
|
attributes.setSpecified(attributes.getLength()-1, true); |
|
if (fAugmentations) { |
|
addLocationItem(attributes, attributes.getLength() - 1); |
|
} |
|
return false; |
|
} |
|
fStringBuffer.clear(); |
|
fNonNormAttr.clear(); |
|
if (c != '\'' && c != '"') { |
|
fCurrentEntity.rewind(); |
|
while (true) { |
|
c = fCurrentEntity.read(); |
|
|
|
if (Character.isWhitespace((char)c) || c == '>') { |
|
|
|
fCurrentEntity.rewind(); |
|
break; |
|
} |
|
if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
throw new EOFException(); |
|
} |
|
if (c == '&') { |
|
int ce = scanEntityRef(fStringBuffer2, false); |
|
if (ce != -1) { |
|
appendChar(fStringBuffer, ce); |
|
} |
|
else { |
|
fStringBuffer.append(fStringBuffer2); |
|
} |
|
fNonNormAttr.append(fStringBuffer2); |
|
} |
|
else { |
|
appendChar(fStringBuffer, c); |
|
appendChar(fNonNormAttr, c); |
|
} |
|
} |
|
fQName.setValues(null, aname, aname, null); |
|
String avalue = fStringBuffer.toString(); |
|
attributes.addAttribute(fQName, "CDATA", avalue); |
|
|
|
int lastattr = attributes.getLength()-1; |
|
attributes.setSpecified(lastattr, true); |
|
attributes.setNonNormalizedValue(lastattr, fNonNormAttr.toString()); |
|
if (fAugmentations) { |
|
addLocationItem(attributes, attributes.getLength() - 1); |
|
} |
|
return true; |
|
} |
|
char quote = (char)c; |
|
boolean isStart = true; |
|
boolean prevSpace = false; |
|
do { |
|
boolean acceptSpace = !fNormalizeAttributes || (!isStart && !prevSpace); |
|
c = fCurrentEntity.read(); |
|
if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
break; |
|
|
|
} |
|
if (c == '&') { |
|
isStart = false; |
|
int ce = scanEntityRef(fStringBuffer2, false); |
|
if (ce != -1) { |
|
appendChar(fStringBuffer, ce); |
|
} |
|
else { |
|
fStringBuffer.append(fStringBuffer2); |
|
} |
|
fNonNormAttr.append(fStringBuffer2); |
|
} |
|
else if (c == ' ' || c == '\t') { |
|
if (acceptSpace) { |
|
fStringBuffer.append(fNormalizeAttributes ? ' ' : (char)c); |
|
} |
|
fNonNormAttr.append((char)c); |
|
} |
|
else if (c == '\r' || c == '\n') { |
|
if (c == '\r') { |
|
int c2 = fCurrentEntity.read(); |
|
if (c2 == '\n') { |
|
fNonNormAttr.append('\r'); |
|
c = c2; |
|
} |
|
else if (c2 != -1) { |
|
fCurrentEntity.rewind(); |
|
} |
|
} |
|
if (acceptSpace) { |
|
fStringBuffer.append(fNormalizeAttributes ? ' ' : '\n'); |
|
} |
|
fCurrentEntity.incLine(); |
|
fNonNormAttr.append((char)c); |
|
} |
|
else if (c != quote) { |
|
isStart = false; |
|
appendChar(fStringBuffer, c); |
|
appendChar(fNonNormAttr, c); |
|
} |
|
prevSpace = c == ' ' || c == '\t' || c == '\r' || c == '\n'; |
|
isStart = isStart && prevSpace; |
|
} while (c != quote); |
|
|
|
if (fNormalizeAttributes && fStringBuffer.length > 0) { |
|
|
|
if (fStringBuffer.ch[fStringBuffer.length - 1] == ' ') { |
|
fStringBuffer.length--; |
|
} |
|
} |
|
|
|
fQName.setValues(null, aname, aname, null); |
|
String avalue = fStringBuffer.toString(); |
|
attributes.addAttribute(fQName, "CDATA", avalue); |
|
|
|
int lastattr = attributes.getLength()-1; |
|
attributes.setSpecified(lastattr, true); |
|
attributes.setNonNormalizedValue(lastattr, fNonNormAttr.toString()); |
|
if (fAugmentations) { |
|
addLocationItem(attributes, attributes.getLength() - 1); |
|
} |
|
} |
|
else { |
|
fQName.setValues(null, aname, aname, null); |
|
attributes.addAttribute(fQName, "CDATA", ""); |
|
attributes.setSpecified(attributes.getLength()-1, true); |
|
fCurrentEntity.rewind(); |
|
if (fAugmentations) { |
|
addLocationItem(attributes, attributes.getLength() - 1); |
|
} |
|
} |
|
return true; |
|
} |
|
|
|
|
|
protected void addLocationItem(XMLAttributes attributes, int index) { |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
LocationItem locationItem = new LocationItem(); |
|
locationItem.setValues(fBeginLineNumber, fBeginColumnNumber, |
|
fBeginCharacterOffset, fEndLineNumber, |
|
fEndColumnNumber, fEndCharacterOffset); |
|
Augmentations augs = attributes.getAugmentations(index); |
|
augs.putItem(AUGMENTATIONS, locationItem); |
|
} |
|
|
|
|
|
protected void scanEndElement() throws IOException { |
|
String ename = scanName(true); |
|
if (fReportErrors && ename == null) { |
|
fErrorReporter.reportError("HTML1012", null); |
|
} |
|
skipMarkup(false); |
|
if (ename != null) { |
|
ename = modifyName(ename, fNamesElems); |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fQName.setValues(null, ename, ename, null); |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("endElement("+fQName+")"); |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.endElement(fQName, locationAugs()); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean isEnded(String ename) { |
|
String content = new String(fCurrentEntity.buffer, fCurrentEntity.offset, |
|
fCurrentEntity.length - fCurrentEntity.offset); |
|
return content.toLowerCase().indexOf("</" + ename.toLowerCase() + ">") != -1; |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SpecialScanner |
|
implements Scanner { |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected String fElementName; |
|
|
|
|
|
protected boolean fStyle; |
|
|
|
|
|
protected boolean fTextarea; |
|
|
|
|
|
protected boolean fTitle; |
|
|
|
|
|
|
|
|
|
private final QName fQName = new QName(); |
|
|
|
|
|
private final XMLStringBuffer fStringBuffer = new XMLStringBuffer(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
public Scanner setElementName(String ename) { |
|
fElementName = ename; |
|
fStyle = fElementName.equalsIgnoreCase("STYLE"); |
|
fTextarea = fElementName.equalsIgnoreCase("TEXTAREA"); |
|
fTitle = fElementName.equalsIgnoreCase("TITLE"); |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean scan(boolean complete) throws IOException { |
|
boolean next; |
|
do { |
|
try { |
|
next = false; |
|
switch (fScannerState) { |
|
case STATE_CONTENT: { |
|
fBeginLineNumber = fCurrentEntity.getLineNumber(); |
|
fBeginColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fBeginCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
int c = fCurrentEntity.read(); |
|
if (c == '<') { |
|
setScannerState(STATE_MARKUP_BRACKET); |
|
continue; |
|
} |
|
if (c == '&') { |
|
if (fTextarea || fTitle) { |
|
scanEntityRef(fStringBuffer, true); |
|
continue; |
|
} |
|
fStringBuffer.clear(); |
|
fStringBuffer.append('&'); |
|
} |
|
else if (c == -1) { |
|
if (fReportErrors) { |
|
fErrorReporter.reportError("HTML1007", null); |
|
} |
|
throw new EOFException(); |
|
} |
|
else { |
|
fCurrentEntity.rewind(); |
|
fStringBuffer.clear(); |
|
} |
|
scanCharacters(fStringBuffer, -1); |
|
break; |
|
} |
|
case STATE_MARKUP_BRACKET: { |
|
int delimiter = -1; |
|
int c = fCurrentEntity.read(); |
|
if (c == '/') { |
|
String ename = scanName(true); |
|
if (ename != null) { |
|
if (ename.equalsIgnoreCase(fElementName)) { |
|
if (fCurrentEntity.read() == '>') { |
|
ename = modifyName(ename, fNamesElems); |
|
if (fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
fQName.setValues(null, ename, ename, null); |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("endElement("+fQName+")"); |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.endElement(fQName, locationAugs()); |
|
} |
|
setScanner(fContentScanner); |
|
setScannerState(STATE_CONTENT); |
|
return true; |
|
} |
|
fCurrentEntity.rewind(); |
|
} |
|
fStringBuffer.clear(); |
|
fStringBuffer.append("</"); |
|
fStringBuffer.append(ename); |
|
} |
|
else { |
|
fStringBuffer.clear(); |
|
fStringBuffer.append("</"); |
|
} |
|
} |
|
else { |
|
fStringBuffer.clear(); |
|
fStringBuffer.append('<'); |
|
appendChar(fStringBuffer, c); |
|
} |
|
scanCharacters(fStringBuffer, delimiter); |
|
setScannerState(STATE_CONTENT); |
|
break; |
|
} |
|
} |
|
} |
|
catch (EOFException e) { |
|
setScanner(fContentScanner); |
|
if (fCurrentEntityStack.empty()) { |
|
setScannerState(STATE_END_DOCUMENT); |
|
} |
|
else { |
|
fCurrentEntity = (CurrentEntity)fCurrentEntityStack.pop(); |
|
setScannerState(STATE_CONTENT); |
|
} |
|
return true; |
|
} |
|
} |
|
while (next || complete); |
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void scanCharacters(XMLStringBuffer buffer, |
|
int delimiter) throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(scanCharacters, delimiter="+delimiter+": "); |
|
} |
|
|
|
while (true) { |
|
int c = fCurrentEntity.read(); |
|
|
|
if (c == -1 || (c == '<' || c == '&')) { |
|
if (c != -1) { |
|
fCurrentEntity.rewind(); |
|
} |
|
break; |
|
} |
|
|
|
else if (c == '\r' || c == '\n') { |
|
fCurrentEntity.rewind(); |
|
int newlines = skipNewlines(); |
|
for (int i = 0; i < newlines; i++) { |
|
buffer.append('\n'); |
|
} |
|
} |
|
else { |
|
appendChar(buffer, c); |
|
if (c == '\n') { |
|
fCurrentEntity.incLine(); |
|
} |
|
} |
|
} |
|
|
|
if (fStyle) { |
|
if (fStyleStripCommentDelims) { |
|
reduceToContent(buffer, "<!--", "-->"); |
|
} |
|
if (fStyleStripCDATADelims) { |
|
reduceToContent(buffer, "<![CDATA[", "]]>"); |
|
} |
|
} |
|
|
|
if (buffer.length > 0 && fDocumentHandler != null && fElementCount >= fElementDepth) { |
|
if (DEBUG_CALLBACKS) { |
|
System.out.println("characters("+buffer+")"); |
|
} |
|
fEndLineNumber = fCurrentEntity.getLineNumber(); |
|
fEndColumnNumber = fCurrentEntity.getColumnNumber(); |
|
fEndCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
fDocumentHandler.characters(buffer, locationAugs()); |
|
} |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")scanCharacters: "); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class PlaybackInputStream |
|
extends FilterInputStream { |
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final boolean DEBUG_PLAYBACK = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected boolean fPlayback = false; |
|
|
|
|
|
protected boolean fCleared = false; |
|
|
|
|
|
protected boolean fDetected = false; |
|
|
|
|
|
|
|
|
|
protected byte[] fByteBuffer = new byte[1024]; |
|
|
|
|
|
protected int fByteOffset = 0; |
|
|
|
|
|
protected int fByteLength = 0; |
|
|
|
|
|
public int fPushbackOffset = 0; |
|
|
|
|
|
public int fPushbackLength = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
public PlaybackInputStream(InputStream in) { |
|
super(in); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public void detectEncoding(String[] encodings) throws IOException { |
|
if (fDetected) { |
|
throw new IOException("Should not detect encoding twice."); |
|
} |
|
fDetected = true; |
|
int b1 = read(); |
|
if (b1 == -1) { |
|
return; |
|
} |
|
int b2 = read(); |
|
if (b2 == -1) { |
|
fPushbackLength = 1; |
|
return; |
|
} |
|
|
|
if (b1 == 0xEF && b2 == 0xBB) { |
|
int b3 = read(); |
|
if (b3 == 0xBF) { |
|
fPushbackOffset = 3; |
|
encodings[0] = "UTF-8"; |
|
encodings[1] = "UTF8"; |
|
return; |
|
} |
|
fPushbackLength = 3; |
|
} |
|
|
|
if (b1 == 0xFF && b2 == 0xFE) { |
|
encodings[0] = "UTF-16"; |
|
encodings[1] = "UnicodeLittleUnmarked"; |
|
return; |
|
} |
|
|
|
else if (b1 == 0xFE && b2 == 0xFF) { |
|
encodings[0] = "UTF-16"; |
|
encodings[1] = "UnicodeBigUnmarked"; |
|
return; |
|
} |
|
|
|
fPushbackLength = 2; |
|
} |
|
|
|
|
|
public void playback() { |
|
fPlayback = true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void clear() { |
|
if (!fPlayback) { |
|
fCleared = true; |
|
fByteBuffer = null; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public int read() throws IOException { |
|
if (DEBUG_PLAYBACK) { |
|
System.out.println("(read"); |
|
} |
|
if (fPushbackOffset < fPushbackLength) { |
|
return fByteBuffer[fPushbackOffset++]; |
|
} |
|
if (fCleared) { |
|
return in.read(); |
|
} |
|
if (fPlayback) { |
|
int c = fByteBuffer[fByteOffset++]; |
|
if (fByteOffset == fByteLength) { |
|
fCleared = true; |
|
fByteBuffer = null; |
|
} |
|
if (DEBUG_PLAYBACK) { |
|
System.out.println(")read -> "+(char)c); |
|
} |
|
return c; |
|
} |
|
int c = in.read(); |
|
if (c != -1) { |
|
if (fByteLength == fByteBuffer.length) { |
|
byte[] newarray = new byte[fByteLength + 1024]; |
|
System.arraycopy(fByteBuffer, 0, newarray, 0, fByteLength); |
|
fByteBuffer = newarray; |
|
} |
|
fByteBuffer[fByteLength++] = (byte)c; |
|
} |
|
if (DEBUG_PLAYBACK) { |
|
System.out.println(")read -> "+(char)c); |
|
} |
|
return c; |
|
} |
|
|
|
|
|
public int read(byte[] array) throws IOException { |
|
return read(array, 0, array.length); |
|
} |
|
|
|
|
|
public int read(byte[] array, int offset, int length) throws IOException { |
|
if (DEBUG_PLAYBACK) { |
|
System.out.println(")read("+offset+','+length+')'); |
|
} |
|
if (fPushbackOffset < fPushbackLength) { |
|
int count = fPushbackLength - fPushbackOffset; |
|
if (count > length) { |
|
count = length; |
|
} |
|
System.arraycopy(fByteBuffer, fPushbackOffset, array, offset, count); |
|
fPushbackOffset += count; |
|
return count; |
|
} |
|
if (fCleared) { |
|
return in.read(array, offset, length); |
|
} |
|
if (fPlayback) { |
|
if (fByteOffset + length > fByteLength) { |
|
length = fByteLength - fByteOffset; |
|
} |
|
System.arraycopy(fByteBuffer, fByteOffset, array, offset, length); |
|
fByteOffset += length; |
|
if (fByteOffset == fByteLength) { |
|
fCleared = true; |
|
fByteBuffer = null; |
|
} |
|
return length; |
|
} |
|
int count = in.read(array, offset, length); |
|
if (count != -1) { |
|
if (fByteLength + count > fByteBuffer.length) { |
|
byte[] newarray = new byte[fByteLength + count + 512]; |
|
System.arraycopy(fByteBuffer, 0, newarray, 0, fByteLength); |
|
fByteBuffer = newarray; |
|
} |
|
System.arraycopy(array, offset, fByteBuffer, fByteLength, count); |
|
fByteLength += count; |
|
} |
|
if (DEBUG_PLAYBACK) { |
|
System.out.println(")read("+offset+','+length+") -> "+count); |
|
} |
|
return count; |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static class LocationItem implements HTMLEventInfo, Cloneable { |
|
|
|
|
|
|
|
|
|
|
|
|
|
protected int fBeginLineNumber; |
|
|
|
|
|
protected int fBeginColumnNumber; |
|
|
|
|
|
protected int fBeginCharacterOffset; |
|
|
|
|
|
protected int fEndLineNumber; |
|
|
|
|
|
protected int fEndColumnNumber; |
|
|
|
|
|
protected int fEndCharacterOffset; |
|
|
|
|
|
|
|
|
|
public LocationItem() { |
|
|
|
} |
|
|
|
LocationItem(final LocationItem other) { |
|
setValues(other.fBeginLineNumber, other.fBeginColumnNumber, other.fBeginCharacterOffset, |
|
other.fEndLineNumber, other.fEndColumnNumber, other.fEndCharacterOffset); |
|
} |
|
|
|
|
|
public void setValues(int beginLine, int beginColumn, int beginOffset, |
|
int endLine, int endColumn, int endOffset) { |
|
fBeginLineNumber = beginLine; |
|
fBeginColumnNumber = beginColumn; |
|
fBeginCharacterOffset = beginOffset; |
|
fEndLineNumber = endLine; |
|
fEndColumnNumber = endColumn; |
|
fEndCharacterOffset = endOffset; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int getBeginLineNumber() { |
|
return fBeginLineNumber; |
|
} |
|
|
|
|
|
public int getBeginColumnNumber() { |
|
return fBeginColumnNumber; |
|
} |
|
|
|
|
|
public int getBeginCharacterOffset() { |
|
return fBeginCharacterOffset; |
|
} |
|
|
|
|
|
public int getEndLineNumber() { |
|
return fEndLineNumber; |
|
} |
|
|
|
|
|
public int getEndColumnNumber() { |
|
return fEndColumnNumber; |
|
} |
|
|
|
|
|
public int getEndCharacterOffset() { |
|
return fEndCharacterOffset; |
|
} |
|
|
|
|
|
|
|
|
|
public boolean isSynthesized() { |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public String toString() { |
|
StringBuffer str = new StringBuffer(); |
|
str.append(fBeginLineNumber); |
|
str.append(':'); |
|
str.append(fBeginColumnNumber); |
|
str.append(':'); |
|
str.append(fBeginCharacterOffset); |
|
str.append(':'); |
|
str.append(fEndLineNumber); |
|
str.append(':'); |
|
str.append(fEndColumnNumber); |
|
str.append(':'); |
|
str.append(fEndCharacterOffset); |
|
return str.toString(); |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean isEncodingCompatible(final String encoding1, final String encoding2) { |
|
try { |
|
try { |
|
return canRoundtrip(encoding1, encoding2); |
|
} |
|
catch (final UnsupportedOperationException e) { |
|
|
|
try { |
|
return canRoundtrip(encoding2, encoding1); |
|
} |
|
catch (final UnsupportedOperationException e1) { |
|
|
|
return false; |
|
} |
|
} |
|
} |
|
catch (final UnsupportedEncodingException e) { |
|
return false; |
|
} |
|
} |
|
|
|
private boolean canRoundtrip(final String encodeCharset, final String decodeCharset) throws UnsupportedEncodingException { |
|
final String reference = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset="; |
|
final byte[] bytesEncoding1 = reference.getBytes(encodeCharset); |
|
final String referenceWithEncoding2 = new String(bytesEncoding1, decodeCharset); |
|
return reference.equals(referenceWithEncoding2); |
|
} |
|
|
|
private boolean endsWith(final XMLStringBuffer buffer, final String string) { |
|
final int l = string.length(); |
|
if (buffer.length < l) { |
|
return false; |
|
} |
|
final String s = new String(buffer.ch, buffer.length-l, l); |
|
return string.equals(s); |
|
} |
|
|
|
|
|
protected int readPreservingBufferContent() throws IOException { |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded("(read: "); |
|
} |
|
if (fCurrentEntity.offset == fCurrentEntity.length) { |
|
if (fCurrentEntity.load(fCurrentEntity.length) < 1) { |
|
if (DEBUG_BUFFER) { |
|
System.out.println(")read: -> -1"); |
|
} |
|
return -1; |
|
} |
|
} |
|
final char c = fCurrentEntity.getNextChar(); |
|
if (DEBUG_BUFFER) { |
|
fCurrentEntity.debugBufferIfNeeded(")read: ", " -> " + c); |
|
} |
|
return c; |
|
} |
|
|
|
|
|
|
|
|
|
private boolean endCommentAvailable() throws IOException { |
|
int nbCaret = 0; |
|
final int originalOffset = fCurrentEntity.offset; |
|
final int originalColumnNumber = fCurrentEntity.getColumnNumber(); |
|
final int originalCharacterOffset = fCurrentEntity.getCharacterOffset(); |
|
|
|
while (true) { |
|
int c = readPreservingBufferContent(); |
|
if (c == -1) { |
|
fCurrentEntity.restorePosition(originalOffset, originalColumnNumber, originalCharacterOffset); |
|
return false; |
|
} |
|
else if (c == '>' && nbCaret >= 2) { |
|
fCurrentEntity.restorePosition(originalOffset, originalColumnNumber, originalCharacterOffset); |
|
return true; |
|
} |
|
else if (c == '-') { |
|
nbCaret++; |
|
} |
|
else { |
|
nbCaret = 0; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void reduceToContent(final XMLStringBuffer buffer, final String startMarker, final String endMarker) { |
|
int i = 0; |
|
int startContent = -1; |
|
final int l1 = startMarker.length(); |
|
final int l2 = endMarker.length(); |
|
while (i < buffer.length - l1 - l2) { |
|
final char c = buffer.ch[buffer.offset+i]; |
|
if (Character.isWhitespace(c)) { |
|
++i; |
|
} |
|
else if (c == startMarker.charAt(0) |
|
&& startMarker.equals(new String(buffer.ch, buffer.offset+i, l1))) { |
|
startContent = buffer.offset + i + l1; |
|
break; |
|
} |
|
else { |
|
return; |
|
} |
|
} |
|
if (startContent == -1) { |
|
return; |
|
} |
|
|
|
i = buffer.length - 1; |
|
while (i > startContent + l2) { |
|
final char c = buffer.ch[buffer.offset+i]; |
|
if (Character.isWhitespace(c)) { |
|
--i; |
|
} |
|
else if (c == endMarker.charAt(l2-1) |
|
&& endMarker.equals(new String(buffer.ch, buffer.offset+i-l2+1, l2))) { |
|
|
|
buffer.length = buffer.offset + i - startContent - 2; |
|
buffer.offset = startContent; |
|
return; |
|
} |
|
else { |
|
return; |
|
} |
|
} |
|
} |
|
} |
|
|