Skip to content

Commit

Permalink
Basic support for parameter entities
Browse files Browse the repository at this point in the history
Fixes #1167

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
azerr authored and angelozerr committed Feb 7, 2022
1 parent ba438fe commit e4ed8e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Expand Up @@ -210,15 +210,17 @@ public static Range selectAttributeFromGivenNameAt(String attrName, int offset,
}

/**
* Returns the range of the attribute value of a specific child node, if it exists
* Returns the range of the attribute value of a specific child node, if it
* exists
*
* @param childNodeName the tag name of the child node/tag
* @param attrName the attribute name
* @param offset text offset from beginning of document
* @param document the DOM document.
* @param attrName the attribute name
* @param offset text offset from beginning of document
* @param document the DOM document.
* @return the child node attribute value range and null otherwise.
*/
public static Range selectChildNodeAttributeValueFromGivenNameAt(String childNodeName, String attrName, int offset, DOMDocument document) {
public static Range selectChildNodeAttributeValueFromGivenNameAt(String childNodeName, String attrName, int offset,
DOMDocument document) {
List<DOMNode> childNodes = document.findNodeAt(offset).getChildren();
if (childNodes.size() == 0) {
return null;
Expand Down Expand Up @@ -553,7 +555,7 @@ public static EntityReferenceRange selectEntityReference(int offset, DOMDocument
public static EntityReferenceRange selectEntityReference(int offset, DOMDocument document,
boolean endsWithSemicolon) {
String text = document.getText();
// Search '&' character on the left of the offset
// Search '&' or '%' character on the left of the offset
int entityReferenceStart = getEntityReferenceStartOffset(text, offset);
if (entityReferenceStart == -1) {
return null;
Expand Down Expand Up @@ -587,8 +589,9 @@ public static int getEntityReferenceStartOffset(String text, int offset) {
// case where offset is on the first character
return -1;
}
if (text.charAt(offset) == '&') {
// case with &|abcd
char c = text.charAt(offset);
if (c == '&' || c == '%') {
// case with &|abcd or with %|abcd
return offset;
}
if (offset == 0) {
Expand All @@ -599,8 +602,9 @@ public static int getEntityReferenceStartOffset(String text, int offset) {
if (startEntityOffset <= 0) {
return -1;
}
// check if the left character is '&'
if (text.charAt(startEntityOffset - 1) != '&') {
// check if the left character is '&' or '%'
c = text.charAt(startEntityOffset - 1);
if (c != '&' && c != '%') {
return -1;
}
return startEntityOffset - 1;
Expand Down Expand Up @@ -1117,4 +1121,4 @@ public static boolean isInAttributeValue(DOMDocument xmlDocument, Position posit
}
}

}
}
Expand Up @@ -222,6 +222,18 @@ public void externalWithSYSTEM() throws BadLocationException, MalformedURIExcept

}

@Test
public void parameterEntity() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<!DOCTYPE root [\r\n" + //
" <!ENTITY % document SYSTEM \"document.ent\">\r\n" + //
" %docu|ment;" + // <- here definition for mdash parameter entity
"]>\r\n" + //
"<root>\r\n" + //
"</root>";
testDefinitionFor(xml, "test.xml", ll("test.xml", r(3, 2, 3, 12), r(2, 13, 2, 21)));
}

private static String getDTDFileURI(String dtdURI) throws MalformedURIException {
return XMLEntityManager.expandSystemId(dtdURI, "test.xml", true).replace("///", "/");
}
Expand Down

0 comments on commit e4ed8e1

Please sign in to comment.