15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
20 import java.text.DateFormat;
21 import java.text.ParseException;
22 import java.util.ArrayList;
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.List;
27 import javax.xml.XMLConstants;
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.transform.dom.DOMSource;
32 import javax.xml.transform.stream.StreamSource;
33 import javax.xml.validation.Schema;
34 import javax.xml.validation.SchemaFactory;
35 import javax.xml.validation.Validator;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Node;
40 import org.w3c.dom.NodeList;
41 import org.xml.sax.SAXException;
71 private static final long serialVersionUID = 7411589173897801550L;
102 private DocumentBuilder db = null;
103 private Validator dv = null;
104 private String mdir =
"notset";
105 private List<IMessageHandler> mhandlers;
106 private Node modelnode = null;
107 private Document modelxml = null;
125 mhandlers =
new ArrayList<IMessageHandler>();
128 DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
129 bf.setIgnoringElementContentWhitespace(
true);
131 db = bf.newDocumentBuilder();
133 InputStream in = getClass().getResourceAsStream(
"/model.xsd");
138 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
139 Schema s = sf.newSchema(
new StreamSource(in));
141 }
catch (IllegalArgumentException e) {
149 Log.e(
"AModelManager",
"No model.xsd validation resource found!");
150 }
catch (ParserConfigurationException e) {
151 throw new RuntimeException(
"Error creating a XML document builder", e);
152 }
catch (SAXException e) {
153 throw new RuntimeException(
"Error creating a XML schema validator", e);
177 Class<?> af = cl.loadClass(pkg + name);
178 return af.newInstance();
179 }
catch (Exception e) {
181 + (pkg !=
"" ? pkg :
"(default)") +
"'.", e);
192 public BufferedReader
getBufReader(String filename)
throws IOException {
193 int buffer_size = 8192;
195 InputStreamReader isr =
new InputStreamReader(
getInStream(filename));
196 return new BufferedReader(isr, buffer_size);
212 return ClassLoader.getSystemClassLoader();
223 protected abstract String[]
getFolderList() throws IOException;
240 public final InputStream
getInStream(String filename) throws IOException {
241 int pos = filename.indexOf(
".bin");
242 sendMessage((pos > -1) ? filename.substring(0, pos) : filename);
257 protected abstract InputStream
getInStreamImpl(String filename)
throws IOException;
291 ArrayList<ModelDescriptor> res =
new ArrayList<ModelDescriptor>();
292 String[] list = null;
295 }
catch (IOException e) {
301 for (String modeldir : list) {
310 InputStream img = null;
315 }
catch (IOException e) {
319 img = getClass().getClassLoader().getResourceAsStream(
"notfound.png");
324 }
catch (ParseException e) {
325 d = Calendar.getInstance().getTime();
347 Element params = getModelXMLElement(
"visual.fields");
348 if (params != null) {
349 NodeList nl = params.getElementsByTagName(
"field");
353 for (
int i = 0; i < nl.getLength(); i++) {
356 f.Name = n.getTextContent();
357 hlp = getNodeAttributeValue(n,
"mapping");
390 assert attrib_name != null;
392 if (modelnode != null) {
393 return getNodeAttributeValue(modelnode, attrib_name);
409 assert attrib_name != null;
410 assert tagname != null;
412 Element e = getModelXMLElement(tagname);
413 return (e != null) ? getNodeAttributeValue(e, attrib_name) : null;
428 private Element getModelXMLElement(String tags) {
429 if (modelxml != null) {
430 Element cur = modelxml.getDocumentElement();
431 String[] elems = tags.split(
"\\.");
432 for (String elem : elems) {
433 NodeList nl = cur.getElementsByTagName(elem);
434 if (nl != null && nl.getLength() > 0) {
435 cur = (Element) nl.item(0);
472 Element res = getModelXMLElement(tagname);
473 return (res != null) ? res.getTextContent() : default_value;
484 return thepackage != null ? thepackage +
"." :
"";
493 private String getNodeAttributeValue(Node n, String attrib_name) {
495 assert attrib_name != null;
497 Node a = n.getAttributes().getNamedItem(attrib_name);
499 return a.getNodeValue();
510 Element params = getModelXMLElement(
"parameters");
511 if (params != null) {
513 NodeList nl = params.getElementsByTagName(
"param");
514 double[] cur =
new double[nl.getLength()];
515 for (
int i = 0; i < nl.getLength(); i++) {
517 String name = getNodeAttributeValue(n,
"name");
520 name =
"\u00B5_" + i;
523 p.addParam(name, Double.parseDouble(getNodeAttributeValue(n,
"min")),
524 Double.parseDouble(getNodeAttributeValue(n,
"max")));
527 String def = getNodeAttributeValue(n,
"default");
528 cur[i] = def != null ? Double.parseDouble(def) : p.
getMinValue(i);
555 String olddir = mdir;
562 }
catch (SAXException e) {
567 dv.validate(
new DOMSource(modelxml));
569 }
catch (SAXException se) {
570 Log.e(
"AModelManager",
"Invalid model.xml: " + se.getMessage(), se);
573 }
catch (IOException e) {
574 throw new RuntimeException(
"I/O error while checking if model directory is valid.", e);
618 this.mdir = location;
619 Log.d(
"ModelManager",
"Loading model from " +
getModelURI());
622 }
catch (Exception e) {
623 throw new ModelManagerException(
"Unexpected Exception in AModelManager.setModelDir: " + e.getMessage(), e);
626 modelxml.normalize();
627 modelnode = modelxml.getElementsByTagName(
"model").item(0);
630 mtype = ModelType.parse(type);
637 if (
"le".equals(machformat)) {
652 return getModelXMLElement(tagname) != null;
abstract String getLoadingMessage()
A short message that writes "loading SD models" dependent on the actual instance. ...
An interface for classes capable of sending messages.
Unknown field mapping type.
MathObjectReader getMathObjReader()
Use this method in order to get a MathObjectReader instance fitted for the current selected model...
void removeMessageHandler(IMessageHandler h)
Parameters getParameters()
Reads the parameters from the model XML file and returns a Parameters object.
ModelManagerException(String msg, Exception inner)
void useModel(String location)
Sets the specified source as current model path.
Reading matrices and vectors with a bunch of convenient overloads for different sources and output fo...
BufferedReader getBufReader(String filename)
abstract boolean modelFileExists(String filename)
Returns whether the specified file exists in the current model folder.
boolean isValidModelDir(String dir)
Checks if a model.xml file exists in the specified directory and performs xsd-validation.
double getMinValue(int i)
final InputStream getInStream(String filename)
Returns an InputStream instance streaming the contents of the file given by filename.
static final String CLASSES_JARFILE
The name of the jar file inside a models directory containing .class files in java bytecode...
List< ModelDescriptor > getModelDescriptors(IProgressReporter pr)
Scans all directories given by getFolderList() for valid models and returns a list of model descripto...
FieldDescriptor[] getModelFieldTypes()
Known model types within the JaRMoSBase project.
This class serves as base class for accessing various types of models at different locations...
static final String info_filename
The model's info html file name (imported from rbappmit, might change later)
abstract URI getModelURI()
Returns an URI for the current model location/directory.
A class for model parameters.
This enum contains all so far known (to JaRMoSBase) types of logical solution fields.
Object loadModelClass(String name)
Loads a class available in the precompiled classes associated with the current model.
Unknown
This model type is unknown to JaRMoSBase.
Represents a short description of a model managed by a model manager.
void sendMessage(String msg)
String getModelPackageStr()
Returns the package of any java source files associated with this model.
The field mapping determines how the DoF of the solution are mapped into the given geometry...
ModelType getModelType()
Returns the model type as given in the model.xml attribute "type" of the "model" tag.
Provides a Log class imitating the Android Log class when not used on android systems.
This Exception gets thrown when an error occurs regarding the functionality of the ModelManager...
String getModelXMLTagValue(String tagname)
Works as the overload with default value, but returns null if no matchin element is found...
abstract String[] getFolderList()
Returns the list of all models directories available at the ModelManagers source location.
String getModelXMLTagValue(String tagname, String default_value)
Returns the text content of a tag inside the model.xml file.
Simple progress reporting implementation using the Java standard console.
AModelManager()
Constructs a new ModelManager and a private DocumentBuilder and SchemaFactory.
String getModelXMLAttribute(String attrib_name)
Returns the attribute value of any attributes of the "model" tag in the model.xml file...
List< ModelDescriptor > getModelDescriptors()
Scans all directories given by getFolderList() for valid models and returns a list of model descripto...
String getModelXMLAttribute(String attrib_name, String tagname)
Returns the attribute value of any attributes of the tag given by tagname in the model.xml file.
Contains information about the logical solution fields.
void addMessageHandler(IMessageHandler h)
abstract InputStream getInStreamImpl(String filename)
Template method.
ClassLoader getClassLoader()
This method yields access to any specialized class loaders in subclasses.
boolean xmlTagExists(String tagname)
Checks if a specified tag exists inside the current models model.xml file.
ModelManagerException(String msg)
Simple interface for progress reporting.