Skip to content

Commit

Permalink
Don't store the owner document instance in each nodes.
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr authored and fbricon committed May 27, 2019
1 parent b69c38e commit 12e3956
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AttrNameOrValue extends DOMNode {
private final DOMAttr ownerAttr;

public AttrNameOrValue(int start, int end, DOMAttr ownerAttr) {
super(start, end, ownerAttr.getOwnerDocument());
super(start, end);
this.ownerAttr = ownerAttr;
}

Expand All @@ -64,7 +64,7 @@ public DOMAttr(String name, DOMNode ownerElement) {
}

public DOMAttr(String name, int start, int end, DOMNode ownerElement) {
super(-1, -1, ownerElement.getOwnerDocument());
super(-1, -1);
this.name = name;
this.nodeAttrName = start != -1 ? new AttrNameOrValue(start, end, this) : null;
this.ownerElement = ownerElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class DOMCDATASection extends DOMText implements CDATASection {
int startContent;
int endContent;

public DOMCDATASection(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public DOMCDATASection(int start, int end) {
super(start, end);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public abstract class DOMCharacterData extends DOMNode implements org.w3c.dom.Ch

private String delimiter;

public DOMCharacterData(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public DOMCharacterData(int start, int end) {
super(start, end);
}

public boolean hasMultiLine() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class DOMComment extends DOMCharacterData implements org.w3c.dom.Comment

int endContent;

public DOMComment(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public DOMComment(int start, int end) {
super(start, end);
}

public boolean isCommentSameLineEndTag() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class DOMDocument extends DOMNode implements Document {
private boolean hasExternalGrammar;

public DOMDocument(TextDocument textDocument, URIResolverExtensionManager resolverExtensionManager) {
super(0, textDocument.getText().length(), null);
super(0, textDocument.getText().length());
this.textDocument = textDocument;
this.resolverExtensionManager = resolverExtensionManager;
resetGrammar();
Expand Down Expand Up @@ -364,23 +364,23 @@ private static String getPrefixedName(String prefix, String localName) {
}

public DOMElement createElement(int start, int end) {
return new DOMElement(start, end, this);
return new DOMElement(start, end);
}

public DOMCDATASection createCDataSection(int start, int end) {
return new DOMCDATASection(start, end, this);
return new DOMCDATASection(start, end);
}

public DOMProcessingInstruction createProcessingInstruction(int start, int end) {
return new DOMProcessingInstruction(start, end, this);
return new DOMProcessingInstruction(start, end);
}

public DOMComment createComment(int start, int end) {
return new DOMComment(start, end, this);
return new DOMComment(start, end);
}

public DOMText createText(int start, int end) {
return new DOMText(start, end, this);
return new DOMText(start, end);
}

public DOMDocumentType createDocumentType(int start, int end) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class DOMElement extends DOMNode implements org.w3c.dom.Element {
int endTagCloseOffset = NULL_VALUE;// <root> </root |>
//DomElement.end = <root> </root>| , is always scanner.getTokenEnd()

public DOMElement(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public DOMElement(int start, int end) {
super(start, end);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public abstract class DOMNode implements Node {
int end; // <root> </root>|

DOMNode parent;
private final DOMDocument ownerDocument;

class XMLNodeList<T extends DOMNode> extends ArrayList<T> implements NodeList {

Expand Down Expand Up @@ -135,15 +134,26 @@ public T setNamedItemNS(org.w3c.dom.Node arg0) throws DOMException {

}

public DOMNode(int start, int end, DOMDocument ownerDocument) {
public DOMNode(int start, int end) {
this.start = start;
this.end = end;
this.ownerDocument = ownerDocument;
this.closed = false;
}

/**
* Returns the owner document and null otherwise.
*
* @return the owner document and null otherwise.
*/
public DOMDocument getOwnerDocument() {
return ownerDocument;
Node node = parent;
while (node != null) {
if (node.getNodeType() == Node.DOCUMENT_NODE) {
return (DOMDocument) node;
}
node = node.getParentNode();
}
return null;
}

@Override
Expand Down Expand Up @@ -409,7 +419,8 @@ public boolean isClosed() {

public DOMElement getParentElement() {
DOMNode parent = getParentNode();
while (parent != null && parent != getOwnerDocument()) {
DOMDocument ownerDocument = getOwnerDocument();
while (parent != null && parent != ownerDocument) {
if (parent.isElement()) {
return (DOMElement) parent;
}
Expand All @@ -418,10 +429,6 @@ public DOMElement getParentElement() {
return null;
}

public String getNodeAsString() {
return ownerDocument.getText().substring(start, end);
}

public boolean isComment() {
return getNodeType() == DOMNode.COMMENT_NODE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class DOMProcessingInstruction extends DOMCharacterData implements org.w3
int endContent;
int endTagOpenOffset = NULL_VALUE;

public DOMProcessingInstruction(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public DOMProcessingInstruction(int start, int end) {
super(start, end);
}

public boolean isProlog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
public class DOMText extends DOMCharacterData implements org.w3c.dom.Text {

public DOMText(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public DOMText(int start, int end) {
super(start, end);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public class DTDDeclNode extends DOMNode{
ArrayList<DTDDeclParameter> parameters;

public DTDDeclNode(int start, int end, DOMDocumentType parentDocumentType) {
super(start, end, parentDocumentType != null ? parentDocumentType.getOwnerDocument() : null);
super(start, end);
this.parentDocumentType = parentDocumentType;
this.parentDocument = null;
}

public DTDDeclNode(int start, int end, DOMDocument parentDocumentType) {
super(start, end, parentDocumentType != null ? parentDocumentType : null);
super(start, end);
this.parentDocument = parentDocumentType;
this.parentDocumentType = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,8 @@ private static class MockProcessingInstruction extends DOMProcessingInstruction

public String content;

public MockProcessingInstruction(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public MockProcessingInstruction(int start, int end) {
super(start, end);
}

@Override
Expand All @@ -923,8 +923,8 @@ private static class MockCDataSection extends DOMCDATASection {

public String content;

public MockCDataSection(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public MockCDataSection(int start, int end) {
super(start, end);
}

@Override
Expand All @@ -937,8 +937,8 @@ private static class MockText extends DOMText {

public String content;

public MockText(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public MockText(int start, int end) {
super(start, end);
}

@Override
Expand All @@ -951,8 +951,8 @@ private static class MockComment extends DOMComment {

public String content;

public MockComment(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public MockComment(int start, int end) {
super(start, end);
}

@Override
Expand All @@ -963,8 +963,8 @@ public String getData() {

private static class MockNode extends DOMNode {

public MockNode(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
public MockNode(int start, int end) {
super(start, end);
}

@Override
Expand All @@ -982,17 +982,17 @@ public short getNodeType() {
private static DOMNode createNode(short nodeType, int start, int end) {
switch (nodeType) {
case DOMNode.ELEMENT_NODE:
return new DOMElement(start, end, null);
return new DOMElement(start, end);
case DOMNode.PROCESSING_INSTRUCTION_NODE:
return new MockProcessingInstruction(start, end, null);
return new MockProcessingInstruction(start, end);
case DOMNode.CDATA_SECTION_NODE:
return new MockCDataSection(start, end, null);
return new MockCDataSection(start, end);
case DOMNode.TEXT_NODE:
return new MockText(start, end, null);
return new MockText(start, end);
case DOMNode.COMMENT_NODE:
return new MockComment(start, end, null);
return new MockComment(start, end);
}
return new MockNode(start, end, null);
return new MockNode(start, end);
}

private static void setRestOfNode(DOMNode n, String tag, Integer endTagStart, boolean closed) {
Expand Down

0 comments on commit 12e3956

Please sign in to comment.