[77] | 1 | package com.framsticks.parsers; |
---|
| 2 | |
---|
| 3 | import java.io.IOException; |
---|
| 4 | import java.io.InputStream; |
---|
| 5 | import java.util.Collections; |
---|
| 6 | import java.util.HashMap; |
---|
| 7 | import java.util.Map; |
---|
| 8 | |
---|
[78] | 9 | import com.framsticks.params.*; |
---|
[84] | 10 | import com.framsticks.util.lang.Numbers; |
---|
[77] | 11 | import org.apache.log4j.Logger; |
---|
| 12 | |
---|
| 13 | import javax.xml.parsers.DocumentBuilder; |
---|
| 14 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
| 15 | import javax.xml.parsers.ParserConfigurationException; |
---|
| 16 | |
---|
| 17 | import com.framsticks.leftovers.f0.NeuroClass; |
---|
| 18 | import org.w3c.dom.Document; |
---|
| 19 | import org.w3c.dom.NamedNodeMap; |
---|
| 20 | import org.w3c.dom.Node; |
---|
| 21 | import org.w3c.dom.NodeList; |
---|
| 22 | import org.xml.sax.SAXException; |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * The Class Schema, which represent f0 schema (it contains all the possible |
---|
| 26 | * classes definitions that can be used in f0 representation). Definitions are |
---|
| 27 | * loaded from XML stream. |
---|
| 28 | * |
---|
| 29 | * @author Jarek Szymczak <name.surname@gmail.com> |
---|
| 30 | * (please replace name and surname with my personal data) |
---|
| 31 | */ |
---|
| 32 | public class Schema { |
---|
| 33 | |
---|
| 34 | private final static Logger logger = Logger.getLogger(Schema.class); |
---|
| 35 | |
---|
[78] | 36 | protected final Registry registry = new Registry(); |
---|
[77] | 37 | |
---|
| 38 | /** The neuro classes (classess representing different types of neurons). */ |
---|
| 39 | private Map<String, NeuroClass> neuroClasses = new HashMap<String, NeuroClass>(); |
---|
| 40 | |
---|
[78] | 41 | public static InputStream getDefaultDefinitionAsStream() { |
---|
| 42 | //return new FileInputStream(new File(Schema.class.getResource("/parsers/f0def.xml").getPath())); |
---|
| 43 | return Schema.class.getResourceAsStream("/parsers/f0def.xml"); |
---|
[77] | 44 | } |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Instantiates a new schema. |
---|
| 48 | * |
---|
[78] | 49 | * @param inputStream |
---|
[77] | 50 | * the xml stream with schema |
---|
| 51 | * @throws Exception |
---|
[78] | 52 | * the exception if one occurred while reading the stream |
---|
[77] | 53 | */ |
---|
[78] | 54 | public Schema(InputStream inputStream) throws Exception { |
---|
[77] | 55 | |
---|
| 56 | DocumentBuilderFactory factory; |
---|
| 57 | DocumentBuilder db; |
---|
| 58 | |
---|
| 59 | try { |
---|
| 60 | factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); |
---|
| 61 | db = factory.newDocumentBuilder(); |
---|
| 62 | |
---|
[78] | 63 | Document document = db.parse(inputStream); |
---|
[77] | 64 | NodeList classes = document.getElementsByTagName("CLASS"); |
---|
| 65 | |
---|
| 66 | for (int i = 0; i < classes.getLength(); i++) { |
---|
| 67 | Node classNode = classes.item(i); |
---|
| 68 | FramsClass framsClass = processClass(classNode); |
---|
[78] | 69 | registry.putInfoIntoCache(framsClass); |
---|
[77] | 70 | } |
---|
| 71 | |
---|
| 72 | classes = document.getElementsByTagName("NEUROCLASS"); |
---|
| 73 | |
---|
| 74 | for (int i = 0; i < classes.getLength(); i++) { |
---|
| 75 | Node classNode = classes.item(i); |
---|
| 76 | FramsClass framsClass = processClass(classNode); |
---|
| 77 | |
---|
| 78 | NamedNodeMap attributes = classNode.getAttributes(); |
---|
| 79 | int prefInputs = getIntAttribute(attributes, "INPUTS"); |
---|
| 80 | int prefOutput = getIntAttribute(attributes, "OUTPUT"); |
---|
| 81 | int prefLocation = getIntAttribute(attributes, "LOCATION"); |
---|
| 82 | int visualHints = getIntAttribute(attributes, "VISUALHINTS"); |
---|
| 83 | String symbolGlymphString = getAttribute(attributes, "SYMBOL"); |
---|
| 84 | int[] symbolGlymph = null; |
---|
| 85 | |
---|
| 86 | if (symbolGlymphString != null) { |
---|
| 87 | String[] sgha = symbolGlymphString.split(","); |
---|
| 88 | int length = sgha.length; |
---|
| 89 | symbolGlymph = new int[length]; |
---|
| 90 | for (int j = 0; j < length; j++) { |
---|
| 91 | try { |
---|
| 92 | symbolGlymph[j] = Integer.parseInt(sgha[j]); |
---|
| 93 | } catch (NumberFormatException e) { |
---|
[78] | 94 | logger.error("an error occurred while parsing symbol glymph, class getId: " |
---|
[77] | 95 | + framsClass.getId() |
---|
| 96 | + ", glymph offset: " + j); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | neuroClasses.put(framsClass.getId(), new NeuroClass( |
---|
| 102 | framsClass, prefInputs, prefOutput, prefLocation, |
---|
| 103 | visualHints, symbolGlymph)); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | } catch (IOException e) { |
---|
[78] | 107 | logger.fatal("unexpected exception occurred: ", e); |
---|
[77] | 108 | throw e; |
---|
| 109 | } catch (ParserConfigurationException e) { |
---|
[78] | 110 | logger.fatal("unexpected exception occurred: ", e); |
---|
[77] | 111 | throw e; |
---|
| 112 | } catch (SAXException e) { |
---|
[78] | 113 | logger.fatal("unexpected exception occurred: ", e); |
---|
[77] | 114 | throw e; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * Method used for convenience, it retrieves the Integer value stored in |
---|
| 121 | * node under certain attribute getName. If value is not present or is other |
---|
| 122 | * getType than integer 0 is returned. |
---|
| 123 | * |
---|
| 124 | * @return attribute value if value exists and it's integer (0 otherwise) |
---|
| 125 | * |
---|
| 126 | */ |
---|
| 127 | private static int getIntAttribute(NamedNodeMap attributes, String name) { |
---|
[78] | 128 | String v = getAttribute(attributes, name); |
---|
| 129 | if (v == null) { |
---|
| 130 | return 0; |
---|
| 131 | } |
---|
[77] | 132 | try { |
---|
[78] | 133 | return Integer.parseInt(v); |
---|
[77] | 134 | } catch (NullPointerException e) { |
---|
| 135 | return 0; |
---|
| 136 | } catch (NumberFormatException e) { |
---|
| 137 | logger.fatal("attribute " + name |
---|
[78] | 138 | + " should be numeric: " + v); |
---|
[77] | 139 | return 0; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | private static String getAttribute(NamedNodeMap attributes, String name) { |
---|
| 144 | Node item = attributes.getNamedItem(name); |
---|
| 145 | if (item == null) { |
---|
| 146 | return null; |
---|
| 147 | } |
---|
| 148 | return item.getNodeValue(); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Method used for convenience, it retrieves the value stored in node under |
---|
| 153 | * certain attribute getName. If value is not present method returns null. |
---|
| 154 | * |
---|
| 155 | * @param attributeName |
---|
| 156 | * the attribute getName |
---|
| 157 | * @param node |
---|
| 158 | * the node |
---|
| 159 | * @return attribute value if value exists (null otherwise) |
---|
| 160 | * |
---|
| 161 | */ |
---|
| 162 | private static String getAttributeFromNode(String attributeName, Node node) { |
---|
| 163 | if (node == null) { |
---|
| 164 | return null; |
---|
| 165 | } |
---|
| 166 | return getAttribute(node.getAttributes(), attributeName); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * In this method analysis of single class is performed. |
---|
| 171 | * |
---|
| 172 | * @param classNode |
---|
| 173 | * the class node |
---|
| 174 | * @return the param entry list as a class schema |
---|
| 175 | * @throws Exception |
---|
| 176 | * the exception in case of any error |
---|
| 177 | */ |
---|
[78] | 178 | private static FramsClass processClass(Node classNode) throws Exception { |
---|
[77] | 179 | String classId = null; |
---|
| 180 | String className = ""; |
---|
| 181 | String classDescription = ""; |
---|
| 182 | try { |
---|
| 183 | classId = classNode.getAttributes().getNamedItem("ID") |
---|
| 184 | .getNodeValue(); |
---|
| 185 | } catch (NullPointerException e) { |
---|
| 186 | throw new Exception("Class getId is not defined!"); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | className = getAttributeFromNode("NAME", classNode); |
---|
| 190 | classDescription = getAttributeFromNode("DESCRIPTION", classNode); |
---|
| 191 | |
---|
[78] | 192 | FramsClass framsClass = new FramsClass(classId, className, classDescription); |
---|
[77] | 193 | |
---|
| 194 | NodeList classProperties = classNode.getChildNodes(); |
---|
| 195 | |
---|
| 196 | for (int j = 0; j < classProperties.getLength(); j++) { |
---|
| 197 | Node node = classProperties.item(j); |
---|
| 198 | |
---|
| 199 | if ("GROUP".equals(node.getNodeName())) { |
---|
| 200 | NamedNodeMap attributes = node.getAttributes(); |
---|
[78] | 201 | String name = getAttribute(attributes, "NAME"); |
---|
| 202 | if (name == null) { |
---|
| 203 | logger.warn("Group name in class \"" + classId + "\" (" |
---|
[77] | 204 | + className + ") is undefined"); |
---|
[78] | 205 | } else { |
---|
[77] | 206 | framsClass.appendGroup(new Group(name)); |
---|
[78] | 207 | } |
---|
[77] | 208 | } else if ("PROP".equals(node.getNodeName()) |
---|
| 209 | || "NEUROPROP".equals(node.getNodeName())) { |
---|
| 210 | |
---|
| 211 | NamedNodeMap attributes = node.getAttributes(); |
---|
| 212 | Param param = processParameter(attributes, classId); |
---|
| 213 | framsClass.append(param); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | return framsClass; |
---|
| 219 | } |
---|
| 220 | |
---|
[84] | 221 | private static <T extends Number> T extractAttribute(NamedNodeMap attributes, String name, Class<T> type) { |
---|
| 222 | String value = getAttribute(attributes, name); |
---|
| 223 | if (value == null) { |
---|
| 224 | return null; |
---|
| 225 | } |
---|
| 226 | return Numbers.parse(value, type); |
---|
| 227 | } |
---|
[77] | 228 | /** |
---|
[78] | 229 | * It analyses the single property within the class |
---|
[77] | 230 | * |
---|
| 231 | * @param attributes |
---|
| 232 | * the attributes of property |
---|
| 233 | * @param classId |
---|
| 234 | * the class getId |
---|
| 235 | * @return the param entry representing single parameter |
---|
| 236 | * @throws Exception |
---|
| 237 | * the exception in case of any error |
---|
| 238 | */ |
---|
[78] | 239 | private static Param processParameter(NamedNodeMap attributes, String classId) |
---|
[77] | 240 | throws Exception { |
---|
| 241 | |
---|
| 242 | String id = getAttribute(attributes, "ID"); |
---|
| 243 | if (id == null) |
---|
| 244 | throw new Exception("Property ID in class \"" + classId |
---|
| 245 | + "\" is undefined"); |
---|
| 246 | String type = getAttribute(attributes, "TYPE"); |
---|
| 247 | if (type == null) |
---|
| 248 | throw new Exception("TYPE of property \"" + id + "\" is undefined"); |
---|
| 249 | |
---|
| 250 | String name = getAttribute(attributes, "NAME"); |
---|
| 251 | String description = getAttribute(attributes, "DESCRIPTION"); |
---|
| 252 | int group = getIntAttribute(attributes, "GROUP"); |
---|
| 253 | String flagsString = getAttribute(attributes, "FLAGS"); |
---|
| 254 | |
---|
| 255 | Integer flags = 0; |
---|
| 256 | |
---|
| 257 | try { |
---|
| 258 | if (flagsString != null) |
---|
| 259 | for (String flag : flagsString.split("[^0-9]")) { |
---|
| 260 | if (flag.trim().equals("")) |
---|
| 261 | continue; |
---|
| 262 | flags |= Integer.parseInt(flag); |
---|
| 263 | } |
---|
| 264 | } catch (NumberFormatException e) { |
---|
| 265 | logger.warn("FLAGS parameter should be an Integer value or separated Integer values. FLAGS are set to: " |
---|
| 266 | + flags); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | ParamBuilder builder = new ParamBuilder(); |
---|
| 270 | builder.setId(id).setName(name).setHelp(description).setGroup(group).setFlags(flags); |
---|
| 271 | |
---|
[84] | 272 | builder.setType(type); |
---|
| 273 | |
---|
[77] | 274 | if ("d".equals(type)) { |
---|
[84] | 275 | builder.setMin(extractAttribute(attributes, "MIN", Integer.class)); |
---|
| 276 | builder.setMax(extractAttribute(attributes, "MAX", Integer.class)); |
---|
| 277 | builder.setDef(extractAttribute(attributes, "DEF", Integer.class)); |
---|
[77] | 278 | } else if ("f".equals(type)) { |
---|
[84] | 279 | builder.setMin(extractAttribute(attributes, "MIN", Double.class)); |
---|
| 280 | builder.setMax(extractAttribute(attributes, "MAX", Double.class)); |
---|
| 281 | builder.setDef(extractAttribute(attributes, "DEF", Double.class)); |
---|
[77] | 282 | } else if ("s".equals(type)) { |
---|
[84] | 283 | builder.setMin(extractAttribute(attributes, "MIN", Integer.class)); |
---|
| 284 | builder.setMax(extractAttribute(attributes, "MAX", Integer.class)); |
---|
| 285 | builder.setDef(extractAttribute(attributes, "DEF", Integer.class)); |
---|
| 286 | builder.setDef(getAttribute(attributes, "DEF")); |
---|
[77] | 287 | } else { |
---|
| 288 | builder.setType(type); |
---|
| 289 | } |
---|
| 290 | return builder.build(); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | |
---|
| 294 | public Map<String, NeuroClass> getNeuroClasses() { |
---|
| 295 | return Collections.unmodifiableMap(neuroClasses); |
---|
| 296 | } |
---|
| 297 | |
---|
[78] | 298 | public final Registry getRegistry() { |
---|
| 299 | return registry; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | |
---|
[77] | 303 | } |
---|