Skip to content

Commit 4896642

Browse files
committed
Improved entitity resolution from the web
1 parent 86266f4 commit 4896642

5 files changed

Lines changed: 37 additions & 3 deletions

File tree

lib/xmljava.jar

527 Bytes
Binary file not shown.

src/com/maxprograms/xml/Catalog.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
import java.io.IOException;
1717
import java.lang.System.Logger;
1818
import java.lang.System.Logger.Level;
19+
import java.net.HttpURLConnection;
1920
import java.net.MalformedURLException;
2021
import java.net.URI;
2122
import java.net.URISyntaxException;
23+
import java.net.URL;
2224
import java.text.MessageFormat;
2325
import java.util.Collection;
2426
import java.util.Hashtable;
@@ -29,6 +31,7 @@
2931
import java.util.TreeSet;
3032
import java.util.Vector;
3133

34+
import javax.net.ssl.HttpsURLConnection;
3235
import javax.xml.parsers.ParserConfigurationException;
3336

3437
import org.xml.sax.InputSource;
@@ -37,6 +40,8 @@
3740

3841
public class Catalog implements EntityResolver2 {
3942

43+
Logger logger = System.getLogger(Catalog.class.getName());
44+
4045
private Map<String, String> systemCatalog;
4146
private Map<String, String> publicCatalog;
4247
private Map<String, String> uriCatalog;
@@ -321,6 +326,9 @@ public InputSource resolveEntity(String name, String publicId, String baseURI, S
321326
try {
322327
URI uri = new URI(baseURI != null ? baseURI : "").resolve(systemId).normalize();
323328
if (uri.toURL().getProtocol() != null) {
329+
if (uri.toURL().getProtocol().startsWith("http")) {
330+
return resolveHttp(uri);
331+
}
324332
return new InputSource(uri.toURL().openStream());
325333
}
326334
return new InputSource(new FileInputStream(uri.toURL().toString()));
@@ -344,6 +352,27 @@ public InputSource resolveEntity(String name, String publicId, String baseURI, S
344352
return null;
345353
}
346354

355+
private InputSource resolveHttp(URI uri) throws MalformedURLException, IOException {
356+
MessageFormat mf = new MessageFormat(Messages.getString("Catalog.3"));
357+
logger.log(Level.WARNING, mf.format(new String[] { uri.toURL().toString() }));
358+
URL url = uri.toURL();
359+
try {
360+
HttpURLConnection con = url.getProtocol().equals("https") ? (HttpsURLConnection) url.openConnection()
361+
: (HttpURLConnection) url.openConnection();
362+
con.setReadTimeout(5000);
363+
con.connect();
364+
con.getResponseCode();
365+
if (con.getResponseCode() != 200) {
366+
mf = new MessageFormat(Messages.getString("Catalog.2"));
367+
throw new IOException(mf.format(new String[] { con.getResponseCode() + "", url.toString() }));
368+
}
369+
return new InputSource(con.getInputStream());
370+
} catch (IOException e) {
371+
mf = new MessageFormat(Messages.getString("Catalog.1"));
372+
throw new IOException(mf.format(new String[] { url.toString(), e.getMessage() }));
373+
}
374+
}
375+
347376
public String matchPublic(String publicId) {
348377
if (publicId != null) {
349378
if (publicId.startsWith("urn:publicid:")) {
@@ -478,7 +507,6 @@ public void parseDTD(String publicId) {
478507
}
479508
} catch (IOException | SAXException e) {
480509
// do nothing
481-
Logger logger = System.getLogger(Catalog.class.getName());
482510
MessageFormat mf = new MessageFormat(Messages.getString("Catalog.0"));
483511
logger.log(Level.WARNING, mf.format(new String[] { publicId }));
484512
}

src/com/maxprograms/xml/Constants.java

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

1515
public class Constants {
1616

17-
public static final String VERSION = "2.1.1";
18-
public static final String BUILD = "20250221_1014";
17+
public static final String VERSION = "2.2.0";
18+
public static final String BUILD = "20250227_1029";
1919

2020
private Constants() {
2121
// private for security

src/com/maxprograms/xml/xmljava.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Catalog.0=Error parsing DTD ''{0}''
2+
Calatalog.1=Error connecting to ''{0}'': {1}
3+
Calatalog.2=Error {0} for: {1}
4+
Catalog.3=Resolving from URL ''{0}''
25
CustomContentHandler.0=Malformed content found
36
Document.0=Prolog contains wrong content type
47
DTDParser.0=Malformed entity reference

src/com/maxprograms/xml/xmljava_es.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Catalog.0=Error al analizar DTD ''{0}''
2+
Calatalog.1=Error conectando a ''{0}'': {1}
3+
Calatalog.2=Error {0} para: {1}
4+
Catalog.3=Resolviendo desde URL ''{0}''
25
CustomContentHandler.0=Se ha encontrado contenido con formato incorrecto
36
Document.0=Prólogo contiene un tipo de contenido incorrecto
47
DTDParser.0=Referencia de entidad con formato incorrecto

0 commit comments

Comments
 (0)