Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTD Error Ranges Adjusted #278

Merged
merged 1 commit into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ else if((curr.isClosed())) {
}
if (curr instanceof DTDDeclNode) {
curr.end = scanner.getTokenOffset() - 1;
if(!curr.isDoctype()) {
while(!curr.isDoctype()) {
curr = curr.getParentNode();
}
}
Expand Down Expand Up @@ -368,7 +368,7 @@ else if((curr.isClosed())) {
}

case DTDEndInternalSubset: {
if (!curr.isDoctype()) {
while (!curr.isDoctype()) {
curr.end = scanner.getTokenOffset() - 1;
curr = curr.getParentNode();
}
Expand All @@ -380,7 +380,7 @@ else if((curr.isClosed())) {

case DTDStartElement: {
//If previous 'curr' was an unclosed DTD Declaration
if (!curr.isDoctype()) {
while (!curr.isDoctype()) {
curr.end = scanner.getTokenOffset();
curr = curr.getParentNode();
}
Expand Down Expand Up @@ -423,7 +423,7 @@ else if((curr.isClosed())) {
}

case DTDStartAttlist: {
if (!curr.isDoctype()) { // If previous DTD Decl was unclosed
while (!curr.isDoctype()) { // If previous DTD Decl was unclosed
curr.end = scanner.getTokenOffset();
curr = curr.getParentNode();
}
Expand All @@ -446,7 +446,7 @@ else if((curr.isClosed())) {
DTDAttlistDecl attribute = (DTDAttlistDecl) curr;
if(isInitialDeclaration == false) {
// All additional declarations are created as new DTDAttlistDecl's
DTDAttlistDecl child = new DTDAttlistDecl(-1, -1, attribute.getParentDocumentType()); // Wont use these values
DTDAttlistDecl child = new DTDAttlistDecl(attribute.getStart(), attribute.getEnd(), attribute.getParentDocumentType());
attribute.addAdditionalAttDecl(child);
child.parent = attribute;

Expand Down Expand Up @@ -477,7 +477,7 @@ else if((curr.isClosed())) {
}

case DTDStartEntity: {
if (!curr.isDoctype()) { // If previous DTD Decl was unclosed
while (!curr.isDoctype()) { // If previous DTD Decl was unclosed
curr.end = scanner.getTokenOffset();
curr = curr.getParentNode();
}
Expand Down Expand Up @@ -525,7 +525,7 @@ else if((curr.isClosed())) {
}

case DTDStartNotation: {
if (!curr.isDoctype()) { // If previous DTD Decl was unclosed
while (!curr.isDoctype()) { // If previous DTD Decl was unclosed
curr.end = scanner.getTokenOffset();
curr = curr.getParentNode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class DTDAttlistDecl extends DTDDeclNode {

public DTDAttlistDecl(int start, int end, DOMDocumentType parentDocumentType) {
super(start, end, parentDocumentType);
declType = new DTDDeclParameter(parentDocumentType, start + 2, start + 9);
setDeclType(start + 2, start + 9);
}

public DOMDocumentType getParentDocumentType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class DTDDeclNode extends DOMNode{
protected final DOMDocumentType parentDocumentType;
protected final DOMDocument parentDocument;

DTDDeclParameter unrecognized; // holds all content after parsing goes wrong in a DTD declaration (ENTITY, ATTLIST, ELEMENT).
DTDDeclParameter declType;
public DTDDeclParameter unrecognized; // holds all content after parsing goes wrong in a DTD declaration (ENTITY, ATTLIST, ...).
public DTDDeclParameter declType; // represents the actual name of the decl eg: ENTITY, ATTLIST, ...

ArrayList<DTDDeclParameter> parameters;

Expand Down Expand Up @@ -98,6 +98,10 @@ public ArrayList<DTDDeclParameter> getParameters() {
return parameters;
}

public void setDeclType(int start, int end) {
declType = new DTDDeclParameter(parentDocumentType, start, end);
}

public String getDeclType() {
if(declType != null) {
return declType.getParameter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public DTDDeclParameter(DOMDocumentType doctype, int start, int end) {
this.end = end;
}

public int getStart() {
return start;
}

public int getEnd() {
return end;
}

public String getParameter() {
if (parameter == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public class DTDElementDecl extends DTDDeclNode {

*/

DTDDeclParameter name;
DTDDeclParameter category;
DTDDeclParameter content;
public DTDDeclParameter name;
public DTDDeclParameter category;
public DTDDeclParameter content;


public DTDElementDecl(int start, int end, DOMDocumentType parentDocumentType) {
super(start, end, parentDocumentType);
declType = new DTDDeclParameter(parentDocumentType, start + 2, start + 9);
setDeclType(start + 2, start + 9);
}

public DOMDocumentType getParentDocumentType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class DTDEntityDecl extends DTDDeclNode implements Entity {

public DTDEntityDecl(int start, int end, DOMDocumentType parentDocumentType) {
super(start, end, parentDocumentType);
declType = new DTDDeclParameter(parentDocumentType, start + 2, start + 8);
setDeclType(start + 2, start + 8);
}

public String getPercent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class DTDNotationDecl extends DTDDeclNode {

public DTDNotationDecl(int start, int end, DOMDocumentType parentDocumentType) {
super(start, end, parentDocumentType);
declType = new DTDDeclParameter(parentDocumentType, start + 2, start + 10);
setDeclType(start + 2, start + 10);
}

void setName(int start, int end) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ else if(c == -1) {
state = ScannerState.DTDAfterNotationName;
return finishToken(offset, TokenType.DTDNotationSystemId);
}
state = ScannerState.DTDWithinNotation;
state = ScannerState.DTDUnrecognizedParameters;
return internalScan();

case DTDAfterNotationPublicId:
Expand All @@ -855,6 +855,8 @@ else if(c == -1) {
isDeclCompleted = true;
return finishToken(offset, TokenType.DTDNotationSystemId);
}

state = ScannerState.DTDUnrecognizedParameters;
return internalScan();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import java.util.Map;

import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DOMElement;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions.ElementDeclUnterminatedCodeAction;
Expand All @@ -30,12 +32,40 @@
*/
public enum DTDErrorCode implements IXMLErrorCode {

MSG_ELEMENT_NOT_DECLARED, MSG_CONTENT_INCOMPLETE, MSG_CONTENT_INVALID, MSG_REQUIRED_ATTRIBUTE_NOT_SPECIFIED,
MSG_ATTRIBUTE_NOT_DECLARED, MSG_ATTRIBUTE_VALUE_NOT_IN_LIST, MSG_FIXED_ATTVALUE_INVALID,
MSG_ELEMENT_WITH_ID_REQUIRED, IDInvalidWithNamespaces, IDREFInvalidWithNamespaces, IDREFSInvalid,

MSG_ELEMENT_TYPE_REQUIRED_IN_ELEMENTDECL, MSG_MARKUP_NOT_RECOGNIZED_IN_DTD, ElementDeclUnterminated,
MSG_OPEN_PAREN_OR_ELEMENT_TYPE_REQUIRED_IN_CHILDREN;


AttNameRequiredInAttDef,
AttTypeRequiredInAttDef,
ElementDeclUnterminated,
EntityDeclUnterminated,
ExternalIDorPublicIDRequired,
IDInvalidWithNamespaces,
IDREFInvalidWithNamespaces,
IDREFSInvalid,
LessthanInAttValue,
MSG_ATTRIBUTE_NOT_DECLARED,
MSG_ATTRIBUTE_VALUE_NOT_IN_LIST,
MSG_CONTENT_INCOMPLETE,
MSG_CONTENT_INVALID,
MSG_ELEMENT_ALREADY_DECLARED,
MSG_ELEMENT_NOT_DECLARED,
MSG_ELEMENT_TYPE_REQUIRED_IN_ATTLISTDECL,
MSG_ELEMENT_TYPE_REQUIRED_IN_ELEMENTDECL,
MSG_ELEMENT_WITH_ID_REQUIRED,
MSG_ENTITY_NAME_REQUIRED_IN_ENTITYDECL,
MSG_FIXED_ATTVALUE_INVALID,
MSG_MARKUP_NOT_RECOGNIZED_IN_DTD,
MSG_NOTATION_NAME_REQUIRED_IN_NOTATIONDECL,
MSG_OPEN_PAREN_OR_ELEMENT_TYPE_REQUIRED_IN_CHILDREN,
MSG_REQUIRED_ATTRIBUTE_NOT_SPECIFIED,
MSG_SPACE_REQUIRED_AFTER_NOTATION_NAME_IN_NOTATIONDECL,
NotationDeclUnterminated,
OpenQuoteExpected,
OpenQuoteMissingInDecl,
PEReferenceWithinMarkup,
QuoteRequiredInPublicID,
QuoteRequiredInSystemID,
SpaceRequiredAfterSYSTEM;

private final String code;

Expand Down Expand Up @@ -84,10 +114,12 @@ public static Range toLSPRange(XMLLocator location, DTDErrorCode code, Object[]
case MSG_CONTENT_INCOMPLETE:
case MSG_REQUIRED_ATTRIBUTE_NOT_SPECIFIED:
case MSG_ELEMENT_NOT_DECLARED:
case MSG_CONTENT_INVALID:
case MSG_CONTENT_INVALID: {
return XMLPositionUtility.selectStartTag(offset, document);
case MSG_ATTRIBUTE_NOT_DECLARED:
return XMLPositionUtility.selectAttributeNameAt(offset, document);
}
case MSG_ATTRIBUTE_NOT_DECLARED: {
return XMLPositionUtility.selectAttributeValueAt((String)arguments[1], offset, document);
}
case MSG_FIXED_ATTVALUE_INVALID: {
String attrName = (String) arguments[1];
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
Expand All @@ -96,6 +128,7 @@ public static Range toLSPRange(XMLLocator location, DTDErrorCode code, Object[]
String attrName = (String) arguments[0];
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}

case MSG_ELEMENT_WITH_ID_REQUIRED: {
DOMElement element = document.getDocumentElement();
if (element != null) {
Expand All @@ -109,14 +142,52 @@ public static Range toLSPRange(XMLLocator location, DTDErrorCode code, Object[]
return XMLPositionUtility.selectAttributeValueByGivenValueAt(attrValue, offset, document);
}

case MSG_MARKUP_NOT_RECOGNIZED_IN_DTD: {
return XMLPositionUtility.selectWholeTag(offset + 2, document);
}

// ---------- DTD Doc type
case MSG_OPEN_PAREN_OR_ELEMENT_TYPE_REQUIRED_IN_CHILDREN:

case ExternalIDorPublicIDRequired: {
return XMLPositionUtility.getLastValidDTDDeclParameter(offset, document);
}

case PEReferenceWithinMarkup: {
return XMLPositionUtility.getLastValidDTDDeclParameter(offset, document, true);
}

case QuoteRequiredInPublicID:
case QuoteRequiredInSystemID:
case OpenQuoteMissingInDecl:
case SpaceRequiredAfterSYSTEM:
case MSG_SPACE_REQUIRED_AFTER_NOTATION_NAME_IN_NOTATIONDECL:
case AttTypeRequiredInAttDef:
case LessthanInAttValue:
case OpenQuoteExpected:
case AttNameRequiredInAttDef:
case EntityDeclUnterminated:
case NotationDeclUnterminated:
case ElementDeclUnterminated: {
return XMLPositionUtility.selectDTDElementDeclAt(offset, document);
return XMLPositionUtility.getLastValidDTDDeclParameterOrUnrecognized(offset, document);
}

case MSG_OPEN_PAREN_OR_ELEMENT_TYPE_REQUIRED_IN_CHILDREN: {
return XMLPositionUtility.getElementDeclMissingContentOrCategory(offset, document);
}

case MSG_ELEMENT_ALREADY_DECLARED:
case MSG_NOTATION_NAME_REQUIRED_IN_NOTATIONDECL:
case MSG_ENTITY_NAME_REQUIRED_IN_ENTITYDECL:
case MSG_ELEMENT_TYPE_REQUIRED_IN_ATTLISTDECL:
case MSG_ELEMENT_TYPE_REQUIRED_IN_ELEMENTDECL: {
return XMLPositionUtility.selectDTDElementDeclTagAt(offset, document);
return XMLPositionUtility.selectDTDDeclTagNameAt(offset, document);
}
default:
try {
return new Range(new Position(0, 0), document.positionAt(document.getEnd()));
} catch (BadLocationException e) {

}
}
return null;
}
Expand Down
Loading