Skip to content

Commit

Permalink
cleanup and simplify XMLAttributesImpl and XMLAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Oct 3, 2024
1 parent 288ceb1 commit 296f1da
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 329 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.htmlunit</groupId>
<artifactId>neko-htmlunit</artifactId>
<version>4.4.0</version>
<version>4.5.0-SNAPSHOT</version>
<name>HtmlUnit NekoHtml</name>
<organization>
<name>HtmlUnit</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.htmlunit.cyberneko.xerces.util.ErrorHandlerWrapper;
import org.htmlunit.cyberneko.xerces.util.SAXMessageFormatter;
import org.htmlunit.cyberneko.xerces.util.XMLAttributesImpl;
import org.htmlunit.cyberneko.xerces.xni.Augmentations;
import org.htmlunit.cyberneko.xerces.xni.NamespaceContext;
import org.htmlunit.cyberneko.xerces.xni.QName;
Expand Down Expand Up @@ -103,9 +104,6 @@ public abstract class AbstractSAXParser extends AbstractXMLDocumentParser implem
// track the version of the document being parsed
protected String fVersion;

// temp vars
private final AttributesProxy fAttributesProxy = new AttributesProxy();

// Default constructor.
protected AbstractSAXParser(final XMLParserConfiguration config) {
super(config);
Expand Down Expand Up @@ -235,8 +233,7 @@ public void startElement(final QName element, final XMLAttributes attributes, fi

final String uri = element.getUri() != null ? element.getUri() : "";
final String localpart = fNamespaces ? element.getLocalpart() : "";
fAttributesProxy.setAttributes(attributes);
fContentHandler.startElement(uri, localpart, element.getRawname(), fAttributesProxy);
fContentHandler.startElement(uri, localpart, element.getRawname(), attributes);
}
}
catch (final SAXException e) {
Expand Down Expand Up @@ -1071,121 +1068,4 @@ public String getEncoding() {
}

}

protected static final class AttributesProxy implements Attributes2 {

/** XML attributes. */
private XMLAttributes fAttributes;

// Sets the XML attributes.
public void setAttributes(final XMLAttributes attributes) {
fAttributes = attributes;
}

@Override
public int getLength() {
return fAttributes.getLength();
}

@Override
public String getQName(final int index) {
return fAttributes.getQName(index);
}

@Override
public String getURI(final int index) {
// REVISIT: this hides the fact that internally we use
// null instead of empty string
// SAX requires URI to be a string or an empty string
final String uri = fAttributes.getURI(index);
return uri != null ? uri : "";
}

@Override
public String getLocalName(final int index) {
return fAttributes.getLocalName(index);
}

@Override
public String getType(final int i) {
return fAttributes.getType(i);
}

@Override
public String getType(final String name) {
return fAttributes.getType(name);
}

@Override
public String getType(final String uri, final String localName) {
return uri.length() == 0 ? fAttributes.getType(null, localName) : fAttributes.getType(uri, localName);
}

@Override
public String getValue(final int i) {
return fAttributes.getValue(i);
}

@Override
public String getValue(final String name) {
return fAttributes.getValue(name);
}

@Override
public String getValue(final String uri, final String localName) {
return uri.length() == 0 ? fAttributes.getValue(null, localName) : fAttributes.getValue(uri, localName);
}

@Override
public int getIndex(final String qName) {
return fAttributes.getIndex(qName);
}

@Override
public int getIndex(final String uri, final String localPart) {
return uri.length() == 0 ? fAttributes.getIndex(null, localPart) : fAttributes.getIndex(uri, localPart);
}

@Override
public boolean isDeclared(final int index) {
return false;
}

@Override
public boolean isDeclared(final String qName) {
return false;
}

@Override
public boolean isDeclared(final String uri, final String localName) {
return false;
}

@Override
public boolean isSpecified(final int index) {
if (index < 0 || index >= fAttributes.getLength()) {
throw new ArrayIndexOutOfBoundsException(index);
}
return fAttributes.isSpecified(index);
}

@Override
public boolean isSpecified(final String qName) {
final int index = getIndex(qName);
if (index == -1) {
throw new IllegalArgumentException(qName);
}
return fAttributes.isSpecified(index);
}

@Override
public boolean isSpecified(final String uri, final String localName) {
final int index = getIndex(uri, localName);
if (index == -1) {
throw new IllegalArgumentException(localName);
}
return fAttributes.isSpecified(index);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,6 @@ public void getName(final int attrIndex, final QName attrName) {
attrName.setValues(attributes_.get(attrIndex).name_);
}

/**
* Sets the type of the attribute at the specified index.
*
* @param attrIndex The attribute index.
* @param attrType The attribute type. The type name is determined by the type
* specified for this attribute in the DTD. For example:
* "CDATA", "ID", "NMTOKEN", etc. However, attributes of type
* enumeration will have the type value specified as the pipe
* ('|') separated list of the enumeration values prefixed by
* an open parenthesis and suffixed by a close parenthesis. For
* example: "(true|false)".
*/
@Override
public void setType(final int attrIndex, final String attrType) {
attributes_.get(attrIndex).type_ = attrType;
}

/**
* Sets the value of the attribute at the specified index. This method will
* overwrite the non-normalized value of the attribute.
Expand Down Expand Up @@ -487,6 +470,52 @@ public String getNonNormalizedValue(int index) {
return attributes_.get(index).getNonNormalizedValue();
}

// Attributes2 impl

@Override
public boolean isDeclared(final int index) {
return false;
}

@Override
public boolean isDeclared(final String qName) {
return false;
}

@Override
public boolean isDeclared(final String uri, final String localName) {
return false;
}

@Override
public boolean isSpecified(final String qName) {
final int index = getIndex(qName);
if (index == -1) {
throw new IllegalArgumentException(qName);
}
return isSpecified(index);
}

@Override
public boolean isSpecified(final String uri, final String localName) {
final int index = getIndex(uri, localName);
if (index == -1) {
throw new IllegalArgumentException(localName);
}
return isSpecified(index);
}

// end Attributes2 impl

/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span>
*
* @return the internal attributes_ {@link ArrayList} to allow some performance optimizations.
*/
public ArrayList<Attribute> getAttributes() {
return attributes_;
}

/**
* Returns the value passed in or NMTOKEN if it's an enumerated type.
*
Expand All @@ -502,18 +531,30 @@ private static String getReportableType(final String type) {
}

/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span>
*
* Attribute information.
* Only public to allow some performance optimizations.
*/
static class Attribute {
public static class Attribute {
/** Name. */
final QName name_ = new QName();

public QName getQName() {
return name_;
}

/** Type. */
String type_;

/** Value. */
String value_;

public String getValue() {
return value_;
}


/** Specified. */
boolean specified_;

Expand Down
Loading

0 comments on commit 296f1da

Please sign in to comment.