Skip to content

Commit

Permalink
Reworked configuration with an eye towards issue #117. Missing jar fi…
Browse files Browse the repository at this point in the history
…les and other problems that prevent a class from being instantiated will now cause p:step-available to return false. There's also a static isAvailable() method that steps can use for finer control.
  • Loading branch information
ndw committed Sep 23, 2013
1 parent c56d5c7 commit 2733096
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/com/xmlcalabash/core/XProcConfiguration.java
Expand Up @@ -17,6 +17,7 @@

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Vector;
import java.util.HashSet;
Expand Down Expand Up @@ -82,7 +83,7 @@ public class XProcConfiguration {
public String entityResolver = null;
public String uriResolver = null;
public String errorListener = null;
public Hashtable<QName,String> implementations = new Hashtable<QName,String> ();
public Hashtable<QName,Class> implementations = new Hashtable<QName,Class> ();
public Hashtable<String,String> serializationOptions = new Hashtable<String,String>();
public LogOptions logOpt = LogOptions.WRAPPED;
public Vector<String> extensionFunctions = new Vector<String>();
Expand Down Expand Up @@ -442,15 +443,31 @@ public void parse(XdmNode doc) {


public boolean isStepAvailable(QName type) {
return implementations.containsKey(type);
if (implementations.containsKey(type)) {
Class klass = implementations.get(type);
try {
Method method = klass.getMethod("isAvailable");
Boolean available = (Boolean) method.invoke(null);
return available.booleanValue();
} catch (NoSuchMethodException e) {
return true; // Failure to implement the method...
} catch (InvocationTargetException e) {
return true; // ...or to implement it...
} catch (IllegalAccessException e) {
return true; // ...badly doesn't mean it's not available.
}
} else {
return false;
}
}

public XProcStep newStep(XProcRuntime runtime,XAtomicStep step){
String className = implementations.get(step.getType());
if (className == null) {
throw new UnsupportedOperationException("Misconfigured. No 'class' in configuration for " + step.getType());
Class klass = implementations.get(step.getType());
if (klass == null) {
throw new XProcException("Misconfigured. No 'class' in configuration for " + step.getType());
}

String className = klass.getName();
// FIXME: This isn't really very secure...
if (runtime.getSafeMode() && !className.startsWith("com.xmlcalabash.")) {
throw XProcException.dynamicError(21);
Expand Down Expand Up @@ -810,7 +827,14 @@ private void parseImplementation(XdmNode node) {

for (String tname : nameStr.split("\\s+")) {
QName name = new QName(tname,node);
implementations.put(name, value);
try {
Class klass = Class.forName(value);
implementations.put(name, klass);
} catch (ClassNotFoundException e) {
// nop
} catch (NoClassDefFoundError e) {
// nop
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/com/xmlcalabash/library/DefaultStep.java
Expand Up @@ -63,6 +63,10 @@ public XAtomicStep getStep() {
return step;
}

public static boolean isAvailable() {
return true;
}

public void setInput(String port, ReadablePipe pipe) {
throw new XProcException("No inputs allowed.");
}
Expand Down

0 comments on commit 2733096

Please sign in to comment.