Skip to content

Commit e682909

Browse files
committed
Handled SYSTEM in external entities
1 parent 389cb97 commit e682909

6 files changed

Lines changed: 83 additions & 13 deletions

File tree

lib/xmljava.jar

1.25 KB
Binary file not shown.

src/com/maxprograms/xml/Catalog.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public class Catalog implements EntityResolver2 {
4040
private Map<String, String> publicCatalog;
4141
private Map<String, String> uriCatalog;
4242
private Map<String, String> dtdCatalog;
43-
private Map<String, String> dtdEntities;
43+
private Map<String, String> dtdPublicEntities;
44+
private Map<String, String> dtdSystemEntities;
4445
private Set<String> parsedDTDs;
4546
private List<String[]> uriRewrites;
4647
private List<String[]> systemRewrites;
@@ -327,8 +328,11 @@ public InputSource resolveEntity(String name, String publicId, String baseURI, S
327328
} catch (IOException | URISyntaxException | IllegalArgumentException | NullPointerException e) {
328329
// ignore
329330
}
330-
if (dtdEntities != null && publicId != null && dtdEntities.containsKey(publicId)) {
331-
return new InputSource(new FileInputStream(dtdEntities.get(publicId)));
331+
if (dtdPublicEntities != null && publicId != null && dtdPublicEntities.containsKey(publicId)) {
332+
return new InputSource(new FileInputStream(dtdPublicEntities.get(publicId)));
333+
}
334+
if (dtdSystemEntities != null && systemId != null && dtdSystemEntities.containsKey(systemId)) {
335+
return new InputSource(new FileInputStream(dtdSystemEntities.get(systemId)));
332336
}
333337
return null;
334338
}
@@ -423,12 +427,21 @@ public String getDTD(String name) {
423427
return null;
424428
}
425429

426-
public void addDtdEntity(String publicId, String path) {
427-
if (dtdEntities == null) {
428-
dtdEntities = new Hashtable<>();
430+
public void addDtdPublicEntity(String publicId, String path) {
431+
if (dtdPublicEntities == null) {
432+
dtdPublicEntities = new Hashtable<>();
433+
}
434+
if (!dtdPublicEntities.containsKey(publicId)) {
435+
dtdPublicEntities.put(publicId, path);
436+
}
437+
}
438+
439+
public void addDtdSystemEntity(String systemId, String path) {
440+
if (dtdSystemEntities == null) {
441+
dtdSystemEntities = new Hashtable<>();
429442
}
430-
if (!dtdEntities.containsKey(publicId)) {
431-
dtdEntities.put(publicId, path);
443+
if (!dtdSystemEntities.containsKey(systemId)) {
444+
dtdSystemEntities.put(systemId, path);
432445
}
433446
}
434447

@@ -448,7 +461,13 @@ public void parseDTD(String publicId) {
448461
EntityDecl entity = it.next();
449462
String path = XMLUtils.getAbsolutePath(dtdFile.getParentFile().getAbsolutePath(),
450463
entity.getValue());
451-
addDtdEntity(entity.getPublicId(), path);
464+
addDtdPublicEntity(entity.getPublicId(), path);
465+
}
466+
entities = grammar.getSytemEntities();
467+
it = entities.iterator();
468+
while (it.hasNext()) {
469+
EntityDecl entity = it.next();
470+
addDtdSystemEntity(entity.getValue(), entity.getSystemId());
452471
}
453472
} catch (IOException | SAXException e) {
454473
// do nothing

src/com/maxprograms/xml/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
public class Constants {
1616

17-
public static final String VERSION = "1.2.0";
18-
public static final String BUILD = "20230606_0952";
17+
public static final String VERSION = "1.3.0";
18+
public static final String BUILD = "20230609_1500";
1919
}

src/com/maxprograms/xml/DTDParser.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,28 @@ public Grammar parse(File file) throws SAXException, IOException {
103103
}
104104
String entityDeclText = source.substring(pointer, index + ">".length());
105105
EntityDecl entityDecl = new EntityDecl(entityDeclText);
106+
if (entityDecl.getType() == EntityDecl.SYSTEM) {
107+
String module = entityDecl.getValue();
108+
if (module == null || module.isBlank()) {
109+
MessageFormat mf = new MessageFormat(Messages.getString("DTDParser.2"));
110+
throw new IOException(mf.format(new String[] { entityDecl.getName() }));
111+
}
112+
String path = XMLUtils.getAbsolutePath(file.getParentFile().getAbsolutePath(), module);
113+
File mod = new File(path);
114+
if (mod.exists()) {
115+
entityDecl.setSystemId(mod.getAbsolutePath() );
116+
Grammar moduleGrammar = parse(mod);
117+
elementDeclMap.putAll(moduleGrammar.getElementDeclMap());
118+
attributeListMap.putAll(moduleGrammar.getAttributeListMap());
119+
entitiesMap.putAll(moduleGrammar.getEntitiesMap());
120+
notationsMap.putAll(moduleGrammar.getNotationsMap());
121+
} else {
122+
if (debug) {
123+
MessageFormat mf = new MessageFormat(Messages.getString("DTDParser.3"));
124+
logger.log(Level.WARNING, mf.format(new String[] { mod.getAbsolutePath() }));
125+
}
126+
}
127+
}
106128
if (!entitiesMap.containsKey(entityDecl.getName())) {
107129
entitiesMap.put(entityDecl.getName(), entityDecl);
108130
} else {

src/com/maxprograms/xml/EntityDecl.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ public class EntityDecl implements XMLNode {
2626
private String type;
2727
private String value;
2828
private String publicId;
29+
private String systemId;
2930
private String ndataValue;
3031
boolean parameterEntity = false;
3132

32-
public EntityDecl(String declaration) throws IndexOutOfBoundsException{
33+
public EntityDecl(String declaration) throws IndexOutOfBoundsException {
3334
int i = "<!ENTITY".length();
3435
char c = declaration.charAt(i);
3536
while (XMLUtils.isXmlSpace(c)) {
@@ -132,6 +133,10 @@ public String getType() {
132133
return type;
133134
}
134135

136+
protected void setValue(String value) {
137+
this.value = value;
138+
}
139+
135140
public String getValue() {
136141
return value;
137142
}
@@ -140,6 +145,14 @@ public String getPublicId() {
140145
return publicId;
141146
}
142147

148+
public void setSystemId(String systemId) {
149+
this.systemId = systemId;
150+
}
151+
152+
public String getSystemId() {
153+
return systemId;
154+
}
155+
143156
public String getNDATA() {
144157
return ndataValue;
145158
}

src/com/maxprograms/xml/Grammar.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public List<EntityDecl> getEntities() {
6262
List<EntityDecl> result = new Vector<>();
6363
result.addAll(entitiesMap.values());
6464
return result;
65-
}
65+
}
6666

6767
public List<ElementDecl> getElements() {
6868
List<ElementDecl> result = new Vector<>();
@@ -73,4 +73,20 @@ public List<ElementDecl> getElements() {
7373
}
7474
return result;
7575
}
76+
77+
public Map<String, ElementDecl> getElementDeclMap() {
78+
return elementDeclMap;
79+
}
80+
81+
public Map<String, AttlistDecl> getAttributeListMap() {
82+
return attributeListMap;
83+
}
84+
85+
public Map<String, EntityDecl> getEntitiesMap() {
86+
return entitiesMap;
87+
}
88+
89+
public Map<String, NotationDecl> getNotationsMap() {
90+
return notationsMap;
91+
}
7692
}

0 commit comments

Comments
 (0)