Skip to content

Commit

Permalink
DTD Formatting implemented
Browse files Browse the repository at this point in the history
Fixes eclipse#221 and eclipse#268

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Jan 11, 2019
1 parent 23f8355 commit b2c2a08
Show file tree
Hide file tree
Showing 30 changed files with 1,645 additions and 455 deletions.
2 changes: 2 additions & 0 deletions org.eclipse.lsp4xml/delete.dtd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!ENTITY asdasds
<!ELEMENT asdasd>
1 change: 1 addition & 0 deletions org.eclipse.lsp4xml/delete.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<aplsepgflespglse </aplsepgflespglse>
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ public boolean isDTD() {
*/
public Collection<DOMNode> findDTDAttrList(String elementName) {
DOMDocumentType docType = getDoctype();
if (docType == null) {
if (docType == null || elementName == null) {
return Collections.emptyList();
}
return docType.getChildren().stream().filter(DOMNode::isDTDAttListDecl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,20 @@
* A doctype node.
*
*/
public class DOMDocumentType extends DOMNode implements org.w3c.dom.DocumentType {
public class DOMDocumentType extends DTDDeclNode implements org.w3c.dom.DocumentType {

public enum DocumentTypeKind {
PUBLIC, SYSTEM, INVALID
}

// Offset values relative to start of the XML Document
Integer nameStart, nameEnd;
Integer kindStart, kindEnd;
Integer publicIdStart, publicIdEnd;
Integer systemIdStart, systemIdEnd;
Integer internalSubsetStart, internalSubsetEnd;


private String name;
private String kind; // SYSTEM || PUBLIC
private String publicId;
private String systemId;
private String internalSubset;
DTDDeclParameter name;
DTDDeclParameter kind; // SYSTEM || PUBLIC
DTDDeclParameter publicId;
DTDDeclParameter systemId;
DTDDeclParameter internalSubset;

private String content; // |<!DOCTYPE ... >|
//private String unrecognizedParameters;

public DOMDocumentType(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
Expand All @@ -49,43 +42,30 @@ public String getContent() {
return content;
}

void setEnd(int end) {
this.end = end;
this.content = getOwnerDocument().getText().substring(start, end);
}

/**
* The text immediately after DOCTYPE, "<!DOCTYPE this_is_the_name ..."
*/
@Override
public String getName() {
if (name == null && this.nameStart != null && this.nameEnd != null) {
name = getSubstring(nameStart, nameEnd);
}
return name;
return name != null ? name.getParameter() : null;
}

void setName(int start, int end) {
nameStart = start;
nameEnd = end;
name = addNewParameter(start, end);
}

/**
* @return the DocumentTypeKind
*/
public String getKind() {
if (kind == null && kindStart != null && kindEnd != null) {
kind = getSubstring(kindStart, kindEnd);
}
return kind;
return kind != null ? kind.getParameter() : null;
}

/**
* @param kind the DocumentTypeKind to set
*/
void setKind(int start, int end) {
kindStart = start;
kindEnd = end;
kind = addNewParameter(start, end);
}

/*
Expand Down Expand Up @@ -125,28 +105,31 @@ public NamedNodeMap getEntities() {
*/
@Override
public String getInternalSubset() {
if (internalSubset == null && internalSubsetStart != null && internalSubsetEnd != null) {
internalSubset = getSubstring(internalSubsetStart + 1, internalSubsetEnd - 1);
String subset;
if(internalSubset != null) {
subset = internalSubset.getParameter();
subset = subset.substring(1, subset.length() - 1);
internalSubset.parameter = subset; // Set parameter to a value without '[' and ']'
return subset;

}
return internalSubset;
return null;
}

/**
* Returns the start offset of internal subset and null otherwise.
*
* @return the start offset of internal subset and null otherwise.
*/
public Integer getStartInternalSubset() {
return internalSubsetStart;

public void setStartInternalSubset(int start) {
internalSubset = addNewParameter(start, start + 1);
}

/**
* Returns the end offset of internal subset and null otherwise.
*
* @return the end offset of internal subset and null otherwise.
*/
public Integer getEndInternalSubset() {
return internalSubsetEnd;
public void setEndInternalSubset(int end) {
updateLastParameterEnd(end);
}

public boolean isInternalSubset(DTDDeclParameter parameter) {
if(this.internalSubset != null) {
return this.internalSubset.equals(parameter);
}
return false;
}

/*
Expand All @@ -166,18 +149,18 @@ public NamedNodeMap getNotations() {
*/
@Override
public String getPublicId() {
if (publicId == null && publicIdStart != null && publicIdEnd != null) {
publicId = cleanURL(getSubstring(publicIdStart, publicIdEnd));
}
return publicId;
return publicId != null ? publicId.getParameter() : null;
}

public String getPublicIdWithoutQuotes() {
return publicId != null ? publicId.getParameterWithoutFirstAndLastChar() : null;
}

/**
* @param publicId the publicId to set
*/
void setPublicId(int start, int end) {
publicIdStart = start;
publicIdEnd = end;
publicId = addNewParameter(start, end);
}

/*
Expand All @@ -187,18 +170,18 @@ void setPublicId(int start, int end) {
*/
@Override
public String getSystemId() {
if (systemId == null && systemIdStart != null && systemIdEnd != null) {
systemId = cleanURL(getSubstring(systemIdStart, systemIdEnd));
}
return systemId;
return systemId != null ? systemId.getParameter() : null;
}

public String getSystemIdWithoutQuotes() {
return systemId != null ? systemId.getParameterWithoutFirstAndLastChar() : null;
}

/**
* @param systemId the systemId to set
*/
void setSystemId(int start, int end) {
systemIdStart = start;
systemIdEnd = end;
systemId = addNewParameter(start, end);
}

/**
Expand All @@ -217,6 +200,9 @@ private static String cleanURL(String url) {
}

/**
* Returns a substring of the whole document.
*
*
* Since offset values are relative to 'this.start' we need to
* subtract getStart() to make them relative to 'content'
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ private String toString(int indent) {
return result.toString();
}

/**
* Returns the node before
*/
public DOMNode findNodeBefore(int offset) {
List<DOMNode> children = getChildren();
int idx = findFirst(children, c -> offset <= c.start) - 1;
Expand Down Expand Up @@ -394,6 +397,10 @@ public DOMElement getParentElement() {
return null;
}

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

public boolean isComment() {
return getNodeType() == DOMNode.COMMENT_NODE;
}
Expand Down
Loading

0 comments on commit b2c2a08

Please sign in to comment.