Skip to content

Commit

Permalink
Merge pull request #301 from zimmi/module-resource-loading
Browse files Browse the repository at this point in the history
Make loading resources from classpath work when openhtmltopdf is a named module
  • Loading branch information
danfickle authored Dec 5, 2018
2 parents 536215e + 5b6eaef commit 506f84d
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 138 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ release.properties
tags
maven-eclipse.xml
Maven_Ant_Builder.launch
/openhtmltopdf-core/nbproject/

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -67,24 +65,12 @@ public FSCatalog() {
* @param catalogURI A String URI to a catalog XML file on the classpath.
*/
public Map<String, String> parseCatalog(String catalogURI) {
URL url;
Map<String, String> map = null;
InputStream s = null;
try {
url = FSCatalog.class.getClassLoader().getResource(catalogURI);
s = new BufferedInputStream(url.openStream());
map = parseCatalog(new InputSource(s));
try (InputStream in = FSCatalog.class.getResourceAsStream(catalogURI)){
map = parseCatalog(new InputSource(new BufferedInputStream(in)));
} catch (Exception ex) {
XRLog.xmlEntities(Level.WARNING, "Could not open XML catalog from URI '" + catalogURI + "'", ex);
map = Collections.emptyMap();
} finally {
try {
if (s != null) {
s.close();
}
} catch (IOException e) {
// ignore..
}
}
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.openhtmltopdf.util.GeneralUtil;
import com.openhtmltopdf.util.XRLog;

import java.io.IOException;
Expand Down Expand Up @@ -69,7 +68,7 @@ public class FSEntityResolver implements EntityResolver {
private FSEntityResolver() {
FSCatalog catalog = new FSCatalog();

entities.putAll(catalog.parseCatalog("resources/schema/openhtmltopdf/catalog-special.xml"));
entities.putAll(catalog.parseCatalog("/resources/schema/openhtmltopdf/catalog-special.xml"));
}

@Override
Expand All @@ -80,7 +79,7 @@ public InputSource resolveEntity(String publicID,
String url = getEntities().get(publicID);

if (url != null) {
URL realUrl = GeneralUtil.getURLFromClasspath(this, url);
URL realUrl = FSEntityResolver.class.getResource(url);
InputStream is = null;
try {
is = realUrl.openStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
package com.openhtmltopdf.util;

import com.openhtmltopdf.DefaultCSSMarker;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
Expand All @@ -31,7 +29,6 @@
import java.net.URL;
import java.net.MalformedURLException;


/**
* <p>Stores runtime configuration information for application parameters that may
* vary on restarting. This implements the Singleton pattern, but through static
Expand Down Expand Up @@ -101,7 +98,7 @@ public class Configuration {
/**
* The location of our default properties file; must be on the CLASSPATH.
*/
private final static String SF_FILE_NAME = "resources/conf/xhtmlrenderer.conf";
private final static String SF_FILE_NAME = "/resources/conf/xhtmlrenderer.conf";

/**
* Default constructor. Will parse default configuration file, system properties, override properties, etc. and
Expand Down Expand Up @@ -255,30 +252,21 @@ private void finer(String msg) {


/**
* Loads the default set of properties, which may be overridden.
* Loads the default set of properties.
*/
private void loadDefaultProperties() {
try {
InputStream readStream = GeneralUtil.openStreamFromClasspath(new DefaultCSSMarker(), SF_FILE_NAME);

try (InputStream readStream = Configuration.class.getResourceAsStream(SF_FILE_NAME)){
if (readStream == null) {
System.err.println("WARNING: Flying Saucer: No configuration files found in classpath using URL: " + SF_FILE_NAME + ", resorting to hard-coded fallback properties.");
this.properties = newFallbackProperties();
} else {
try {
this.properties = new Properties();
this.properties.load(readStream);
} finally {
readStream.close();
}
this.properties = new Properties();
this.properties.load(readStream);
info("Configuration loaded from " + SF_FILE_NAME);
}
} catch (RuntimeException rex) {
throw rex;
} catch (Exception ex) {
throw new RuntimeException("Could not load properties file for configuration.",
ex);
throw new RuntimeException("Could not load properties file for configuration.", ex);
}
info("Configuration loaded from " + SF_FILE_NAME);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
package com.openhtmltopdf.util;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
* Description of the Class
*
Expand All @@ -40,50 +38,6 @@ public class GeneralUtil {
*/
public final static java.text.DecimalFormat PADDED_HASH_FORMAT = new java.text.DecimalFormat("0000000000");

/**
* Description of the Method
*
* @param obj PARAM
* @param resource PARAM
* @return Returns
*/
public static InputStream openStreamFromClasspath(Object obj, String resource) {
InputStream readStream = null;
try {
ClassLoader loader = obj.getClass().getClassLoader();
if (loader == null) {
readStream = ClassLoader.getSystemResourceAsStream(resource);
} else {
readStream = loader.getResourceAsStream(resource);
}
if (readStream == null) {
URL stream = resource.getClass().getResource(resource);
if (stream != null) readStream = stream.openStream();
}
} catch (Exception ex) {
XRLog.exception("Could not open stream from CLASSPATH: " + resource, ex);
}
return readStream;
}

public static URL getURLFromClasspath(Object obj, String resource) {
URL url = null;
try {
ClassLoader loader = obj.getClass().getClassLoader();
if (loader == null) {
url = ClassLoader.getSystemResource(resource);
} else {
url = loader.getResource(resource);
}
if (url == null) {
url = resource.getClass().getResource(resource);
}
} catch (Exception ex) {
XRLog.exception("Could not get URL from CLASSPATH: " + resource, ex);
}
return url;
}

/**
* Dumps an exception to the console, only the last 5 lines of the stack
* trace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<!-- XML Catalog data for special DTD created for OpenHTMLToPDF ................................ -->
<!-- ...................................................................... -->

<public publicId="-//OPENHTMLTOPDF//DOC XHTML Character Entities Only 1.0//EN" uri="resources/schema/openhtmltopdf/char-entities-xhtml-only.ent" />
<public publicId="-//OPENHTMLTOPDF//MATH XHTML Character Entities With MathML 1.0//EN" uri="resources/schema/openhtmltopdf/char-entities-xhtml-mathml.ent" />
<public publicId="-//OPENHTMLTOPDF//DOC XHTML Character Entities Only 1.0//EN" uri="/resources/schema/openhtmltopdf/char-entities-xhtml-only.ent" />
<public publicId="-//OPENHTMLTOPDF//MATH XHTML Character Entities With MathML 1.0//EN" uri="/resources/schema/openhtmltopdf/char-entities-xhtml-mathml.ent" />

<!-- ...................................................................... -->

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.openhtmltopdf.resource;

import java.util.Map;
import org.junit.Assert;
import org.junit.Test;

public class FSCatalogTest {

@Test
public void testParseCatalog() {
FSCatalog instance = new FSCatalog();
String catalogUri = "/resources/schema/openhtmltopdf/catalog-special.xml";
Map<String, String> catalog = instance.parseCatalog(catalogUri);
String publicId = "-//OPENHTMLTOPDF//DOC XHTML Character Entities Only 1.0//EN";
Assert.assertTrue(catalog.containsKey(publicId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.openhtmltopdf.resource;

import java.io.InputStream;
import java.util.Map;
import static org.hamcrest.CoreMatchers.notNullValue;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.InputSource;

public class FSEntityResolverTest {

@Test
public void testResolveEntity() throws Exception {
FSEntityResolver instance = FSEntityResolver.instance();
String publicId = "-//OPENHTMLTOPDF//DOC XHTML Character Entities Only 1.0//EN";
InputSource resolvedEntity = instance.resolveEntity(publicId, null);
try (InputStream in = resolvedEntity.getByteStream()) {
Assert.assertThat(in, notNullValue());
}
}

@Test
public void testGetEntities() {
FSEntityResolver instance = FSEntityResolver.instance();
Map<String, String> entities = instance.getEntities();
String publicId = "-//OPENHTMLTOPDF//DOC XHTML Character Entities Only 1.0//EN";
Assert.assertTrue(entities.containsKey(publicId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.openhtmltopdf.util;

import static org.hamcrest.CoreMatchers.is;
import org.junit.Test;
import static org.junit.Assert.*;

public class ConfigurationTest {

@Test
public void testValueFor() {
String key = "xr.test-config-byte";
String value = Configuration.valueFor(key);
assertThat(value, is("8"));
}

@Test
public void testValueAsByte() {
String key = "xr.test-config-byte";
int value = Configuration.valueAsByte(key, (byte) 0);
assertThat(value, is(8));
}

@Test
public void testValueAsShort() {
String key = "xr.test-config-short";
int value = Configuration.valueAsShort(key, (short) 0);
assertThat(value, is(16));
}

@Test
public void testValueAsInt() {
String key = "xr.test-config-int";
int value = Configuration.valueAsInt(key, (int) 0);
assertThat(value, is(100));
}

@Test
public void testValueAsLong() {
String key = "xr.test-config-long";
long value = Configuration.valueAsLong(key, (long) 0);
assertThat(value, is(2000L));
}

@Test
public void testValueAsFloat() {
String key = "xr.test-config-float";
float value = Configuration.valueAsFloat(key, (float) 0);
assertThat(value, is(3000.25F));
}

@Test
public void testValueAsDouble() {
String key = "xr.test-config-double";
double value = Configuration.valueAsDouble(key, (double) 0);
assertThat(value, is(4000.50D));
}

@Test
public void testIsTrue() {
String key = "xr.test-config-boolean";
boolean value = Configuration.isTrue(key, false);
assertThat(value, is(true));
}

@Test
public void testIsFalse() {
String key = "xr.test-config-boolean";
boolean value = Configuration.isFalse(key, true);
assertThat(value, is(false));
}

}

0 comments on commit 506f84d

Please sign in to comment.