lcsim/src/org/lcsim/contrib/seedtracker/strategybuilder
diff -u -r1.2 -r1.3
--- LayerWeight.java 17 Jul 2008 00:52:39 -0000 1.2
+++ LayerWeight.java 17 Jul 2008 21:51:08 -0000 1.3
@@ -8,6 +8,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -78,6 +79,57 @@
}
+ /**
+ * Returns the prefix of the default resource path to where the
+ * layer weight XML files are stored.
+ * @return default resource path to XML weights
+ */
+ public static String getDefaultResourcePrefix(){
+ return "org/lcsim/contrib/seedtracker/strategybuilder/weights/";
+ }
+
+ /**
+ * Loads LayerWeight definitions from the resource with the specified name.
+ * The name should be something like /org/lcsim/contrib/seedtracker/strategybuilder/weights/weights.xml
+ *
+ * getDefaultResourcePrefix() may be helpful.
+ *
+ * @param resourceName the full name of the resource file to read
+ * @return the read LayerWeight definitions
+ */
+ public static LayerWeight getLayerWeightFromResource(String resourceName) {
+ return getLayerWeightFromInputStream(LayerWeight.class.getClassLoader().getResourceAsStream(resourceName));
+
+ }
+
+
+ /**
+ * Loads LayerWeight definitions from the specified input stream
+ * @param in an input stream corresponding to a valid XML file
+ * @return the read LayerWeight definitions
+ */
+ public static LayerWeight getLayerWeightFromInputStream(InputStream in){
+
+ Document doc;
+ SAXBuilder builder = new SAXBuilder();
+ try {
+ doc = builder.build(in);
+ } catch (JDOMException jdom) {
+ jdom.printStackTrace();
+ throw new RuntimeException("JDOM exception occurred");
+ } catch (IOException io ) {
+ io.printStackTrace();
+ throw new RuntimeException("IO Exception occurred");
+ }
+
+ return getLayerWeightFromDocument(doc);
+
+ }
+ /**
+ * Loads LayerWeight definitions from the specified file.
+ * @param file a valid XML file specifying layer weights
+ * @return the read LayerWeight definitions
+ */
public static LayerWeight getLayerWeightFromFile(File file){
if (! file.exists() )
@@ -95,6 +147,10 @@
throw new RuntimeException("IO Exception occurred");
}
+ return getLayerWeightFromDocument(doc);
+ }
+
+ private static LayerWeight getLayerWeightFromDocument(Document doc) {
Element root = doc.getRootElement();
LayerWeight lw = new LayerWeight();
@@ -119,7 +175,12 @@
return lw;
}
-
+ /**
+ * Writes this LayerWeight to an XML file with a location specified
+ * by the given file object.
+ * @param file output File object
+ * @return true if succesful, false otherwise.
+ */
public boolean writeToFile(File file){
Element root = new Element("LayerWeight");
Document doc = new Document(root);
lcsim/src/org/lcsim/contrib/seedtracker/strategybuilder
diff -u -r1.2 -r1.3
--- DefaultLayerWeight.java 17 Jul 2008 00:52:39 -0000 1.2
+++ DefaultLayerWeight.java 17 Jul 2008 21:51:08 -0000 1.3
@@ -5,18 +5,16 @@
package org.lcsim.contrib.seedtracker.strategybuilder;
-import java.io.File;
-
/**
*The layer weighting scheme used by default by StrategyBuilder if no other is
- * provided. It's loaded from the file specified in filename.
+ * provided. It's loaded from the specified resource.
*
* @author cozzy
*/
public class DefaultLayerWeight extends LayerWeight {
- private static final String filename = "resources/org/lcsim/contrib/seedtracker/strategybuilder/weights/default_weights.xml";
+ private static final String resource = "default_weights.xml";
public DefaultLayerWeight(){
- super(LayerWeight.getLayerWeightFromFile(new File(filename)));
+ super( LayerWeight.getLayerWeightFromResource(LayerWeight.getDefaultResourcePrefix()+resource) );
}
}