Changeset 78 for java/main/src/main/java/com/framsticks/parsers/Schema.java
- Timestamp:
- 01/09/13 00:09:10 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/parsers/Schema.java
r77 r78 1 1 package com.framsticks.parsers; 2 2 3 import java.io.File; 4 import java.io.FileInputStream; 3 5 import java.io.IOException; 4 6 import java.io.InputStream; … … 8 10 import java.util.Map.Entry; 9 11 10 import com.framsticks.params.FramsClass; 11 import com.framsticks.params.Param; 12 import com.framsticks.params.*; 12 13 import com.framsticks.params.types.DecimalParam; 14 import com.framsticks.params.types.FloatParam; 13 15 import com.framsticks.params.types.StringParam; 14 16 import org.apache.log4j.Logger; … … 19 21 20 22 import com.framsticks.leftovers.f0.NeuroClass; 21 import com.framsticks.params.Group;22 import com.framsticks.params.ParamBuilder;23 23 import org.w3c.dom.Document; 24 24 import org.w3c.dom.NamedNodeMap; … … 39 39 private final static Logger logger = Logger.getLogger(Schema.class); 40 40 41 /** The main classes (such as part, joint, etc.) */ 42 private Map<String, FramsClass> mainClasses = new HashMap<String, FramsClass>(); 41 protected final Registry registry = new Registry(); 43 42 44 43 /** The neuro classes (classess representing different types of neurons). */ 45 44 private Map<String, NeuroClass> neuroClasses = new HashMap<String, NeuroClass>(); 46 45 47 // TODO: definition can be changed 48 /** 49 * Instantiates a new schema with usage of f0def.xml file stored in 50 * resources. 51 * 52 * @throws Exception 53 * the exception if one occured while reading the stream 54 */ 55 public Schema() throws Exception { 56 this(Thread.currentThread().getContextClassLoader() 57 .getResourceAsStream("f0def.xml")); 46 public static InputStream getDefaultDefinitionAsStream() { 47 //return new FileInputStream(new File(Schema.class.getResource("/parsers/f0def.xml").getPath())); 48 return Schema.class.getResourceAsStream("/parsers/f0def.xml"); 58 49 } 59 50 … … 61 52 * Instantiates a new schema. 62 53 * 63 * @param xmlStream54 * @param inputStream 64 55 * the xml stream with schema 65 56 * @throws Exception 66 * the exception if one occur ed while reading the stream67 */ 68 public Schema(InputStream xmlStream) throws Exception {57 * the exception if one occurred while reading the stream 58 */ 59 public Schema(InputStream inputStream) throws Exception { 69 60 70 61 DocumentBuilderFactory factory; … … 75 66 db = factory.newDocumentBuilder(); 76 67 77 Document document = db.parse( xmlStream);68 Document document = db.parse(inputStream); 78 69 NodeList classes = document.getElementsByTagName("CLASS"); 79 70 … … 81 72 Node classNode = classes.item(i); 82 73 FramsClass framsClass = processClass(classNode); 83 mainClasses.put(framsClass.getId(),framsClass);74 registry.putInfoIntoCache(framsClass); 84 75 } 85 76 … … 106 97 symbolGlymph[j] = Integer.parseInt(sgha[j]); 107 98 } catch (NumberFormatException e) { 108 logger.error("an error occur ed while parsing symbol glymph, class getId: "99 logger.error("an error occurred while parsing symbol glymph, class getId: " 109 100 + framsClass.getId() 110 101 + ", glymph offset: " + j); … … 119 110 120 111 } catch (IOException e) { 121 logger.fatal("unexpected exception occur ed: ", e);112 logger.fatal("unexpected exception occurred: ", e); 122 113 throw e; 123 114 } catch (ParserConfigurationException e) { 124 logger.fatal("unexpected exception occur ed: ", e);115 logger.fatal("unexpected exception occurred: ", e); 125 116 throw e; 126 117 } catch (SAXException e) { 127 logger.fatal("unexpected exception occur ed: ", e);118 logger.fatal("unexpected exception occurred: ", e); 128 119 throw e; 129 120 } … … 140 131 */ 141 132 private static int getIntAttribute(NamedNodeMap attributes, String name) { 133 String v = getAttribute(attributes, name); 134 if (v == null) { 135 return 0; 136 } 142 137 try { 143 return Integer.parseInt( getAttribute(attributes, name));138 return Integer.parseInt(v); 144 139 } catch (NullPointerException e) { 145 140 return 0; 146 141 } catch (NumberFormatException e) { 147 142 logger.fatal("attribute " + name 148 + " should be numeric (it is not)");143 + " should be numeric: " + v); 149 144 return 0; 150 145 } … … 186 181 * the exception in case of any error 187 182 */ 188 private FramsClass processClass(Node classNode) throws Exception {183 private static FramsClass processClass(Node classNode) throws Exception { 189 184 String classId = null; 190 185 String className = ""; … … 200 195 classDescription = getAttributeFromNode("DESCRIPTION", classNode); 201 196 202 FramsClass framsClass = new FramsClass(classId, className, 203 classDescription); 197 FramsClass framsClass = new FramsClass(classId, className, classDescription); 204 198 205 199 NodeList classProperties = classNode.getChildNodes(); … … 210 204 if ("GROUP".equals(node.getNodeName())) { 211 205 NamedNodeMap attributes = node.getAttributes(); 212 String name = attributes.getNamedItem("NAME") == null ? null 213 : attributes.getNamedItem("NAME").getNodeValue(); 214 if (name == null) 215 logger.warn("Group getName in class \"" + classId + "\" (" 206 String name = getAttribute(attributes, "NAME"); 207 if (name == null) { 208 logger.warn("Group name in class \"" + classId + "\" (" 216 209 + className + ") is undefined"); 217 else210 } else { 218 211 framsClass.appendGroup(new Group(name)); 219 212 } 220 213 } else if ("PROP".equals(node.getNodeName()) 221 214 || "NEUROPROP".equals(node.getNodeName())) { … … 232 225 233 226 /** 234 * It analyses the single property within the clas 227 * It analyses the single property within the class 235 228 * 236 229 * @param attributes … … 242 235 * the exception in case of any error 243 236 */ 244 private Param processParameter(NamedNodeMap attributes, String classId)237 private static Param processParameter(NamedNodeMap attributes, String classId) 245 238 throws Exception { 246 239 … … 315 308 } 316 309 } 317 builder.setType( DecimalParam.class);310 builder.setType(FloatParam.class); 318 311 builder.setMin(minMaxDefDouble.get("MIN")); 319 312 builder.setMax(minMaxDefDouble.get("MAX")); … … 330 323 } 331 324 332 public Map<String, FramsClass> getMainClasses() {333 return Collections.unmodifiableMap(mainClasses);334 }335 325 336 326 public Map<String, NeuroClass> getNeuroClasses() { … … 338 328 } 339 329 330 public final Registry getRegistry() { 331 return registry; 332 } 333 334 340 335 }
Note: See TracChangeset
for help on using the changeset viewer.