Permalink
Browse files

Close #210 by adding code to search JAR files for catalog.xml files a…

…nd using them.
  • Loading branch information...
ndw committed May 20, 2015
1 parent 8ca4ca5 commit 64d2152bafc97cc1e7e973190a0691545f6546eb
@@ -17,7 +17,10 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;
@@ -28,6 +31,8 @@
import java.net.URISyntaxException;
import java.io.InputStream;
import java.io.File;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import com.xmlcalabash.io.ReadablePipe;
import com.xmlcalabash.io.DocumentSequence;
@@ -107,6 +112,7 @@
public String jsonFlavor = JSONtoXML.MARKLOGIC;
public boolean useXslt10 = false;
public boolean htmlSerializer = false;
public Vector<String> catalogs = new Vector<String> ();
public int piperackPort = 8088;
public int piperackDefaultExpires = 300;
@@ -119,6 +125,7 @@
public XProcConfiguration() {
logger = LoggerFactory.getLogger(this.getClass());
initSaxonProcessor("he", false, null);
init();
}
// This constructor is historical, the (String, boolean) constructor is preferred
@@ -202,6 +209,20 @@ private void init() {
saxonProcessor = Configuration.softwareEdition.toLowerCase();
findStepClasses();
findExtensionFunctions();
String classPath = System.getProperty("java.class.path");
String[] pathElements = classPath.split(System.getProperty("path.separator"));
for (String s : pathElements) {
try {
JarFile jar = new JarFile(s);
ZipEntry catalog = jar.getEntry("catalog.xml");
if (catalog != null) {
catalogs.add("jar:file://" + s + "!/catalog.xml");
}
} catch (IOException e) {
// nevermind
}
}
}
private void createSaxonProcessor(String proctype, boolean schemaAware, Input saxoncfg) {
@@ -216,7 +216,9 @@ public XProcRuntime(XProcConfiguration config) {
throw new XProcException(e);
}
processor.getUnderlyingConfiguration().setURIResolver(uriResolver);
if (config.catalogs.size() > 0) {
uriResolver.addCatalogs(config.catalogs);
}
StepErrorListener errListener = new StepErrorListener(this);
saxonConfig.setErrorListener(errListener);
@@ -28,6 +28,8 @@
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcRuntime;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -38,7 +40,11 @@
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable;
import java.util.List;
import org.slf4j.Logger;
import org.xmlresolver.CatalogSource;
import org.xmlresolver.Resolver;
/**
* Created by IntelliJ IDEA.
@@ -93,6 +99,29 @@ public ModuleURIResolver getUnderlyingModuleURIResolver() {
return moduleURIResolver;
}
public void addCatalogs(List<String> catalogs) {
if (uriResolver instanceof org.xmlresolver.Resolver) {
Resolver res = (org.xmlresolver.Resolver) uriResolver;
for (String catalog : catalogs) {
logger.debug("Adding catalog to resolver: " + catalog);
try {
URL cat = new URL(catalog);
InputSource source = new InputSource(cat.openStream());
source.setSystemId(catalog);
CatalogSource catsource = new CatalogSource.InputSourceCatalogSource(source);
res.getCatalog().addSource(catsource);
} catch (MalformedURLException e) {
logger.info("Malformed catalog URI in jar file: " + catalog);
} catch (IOException e) {
logger.info("I/O error reading catalog URI in jar file: " + catalog);
}
}
} else {
logger.debug("Not adding catalogs to resolver, uriResolver is not an org.xmlresolver.Resolver");
}
}
public void cache(XdmNode doc, URI baseURI) {
XdmNode root = S9apiUtils.getDocumentElement(doc);
@@ -184,6 +213,25 @@ public XdmNode parse(String href, String base, boolean dtdValidate) {
try {
URI baseURI = new URI(base);
URI resURI = baseURI.resolve(href);
String path = baseURI.toASCIIString();
int pos = path.indexOf("!");
if (pos > 0 && (path.startsWith("jar:file:") || path.startsWith("jar:http:") || path.startsWith("jar:https:"))) {
// You can't resolve() against jar: scheme URIs because they appear to be opaque.
// I wonder if what follows is kosher...
String fakeURIstr = "http://example.com";
String subpath = path.substring(pos+1);
if (subpath.startsWith("/")) {
fakeURIstr += subpath;
} else {
fakeURIstr += "/" + subpath;
}
URI fakeURI = new URI(fakeURIstr);
resURI = fakeURI.resolve(href);
fakeURIstr = path.substring(0,pos+1) + resURI.getPath();
resURI = new URI(fakeURIstr);
}
source = new SAXSource(new InputSource(resURI.toASCIIString()));
XMLReader reader = ((SAXSource) source).getXMLReader();

0 comments on commit 64d2152

Please sign in to comment.