Ignore:
Timestamp:
01/09/13 00:09:10 (11 years ago)
Author:
psniegowski
Message:

Add f0 parsing and f0->Model transformation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • java/main/src/main/java/com/framsticks/parsers/Schema.java

    r77 r78  
    11package com.framsticks.parsers;
    22
     3import java.io.File;
     4import java.io.FileInputStream;
    35import java.io.IOException;
    46import java.io.InputStream;
     
    810import java.util.Map.Entry;
    911
    10 import com.framsticks.params.FramsClass;
    11 import com.framsticks.params.Param;
     12import com.framsticks.params.*;
    1213import com.framsticks.params.types.DecimalParam;
     14import com.framsticks.params.types.FloatParam;
    1315import com.framsticks.params.types.StringParam;
    1416import org.apache.log4j.Logger;
     
    1921
    2022import com.framsticks.leftovers.f0.NeuroClass;
    21 import com.framsticks.params.Group;
    22 import com.framsticks.params.ParamBuilder;
    2323import org.w3c.dom.Document;
    2424import org.w3c.dom.NamedNodeMap;
     
    3939        private final static Logger logger = Logger.getLogger(Schema.class);
    4040
    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();
    4342
    4443        /** The neuro classes (classess representing different types of neurons). */
    4544        private Map<String, NeuroClass> neuroClasses = new HashMap<String, NeuroClass>();
    4645
    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");
    5849        }
    5950
     
    6152         * Instantiates a new schema.
    6253         *
    63          * @param xmlStream
     54         * @param inputStream
    6455         *            the xml stream with schema
    6556         * @throws Exception
    66          *             the exception if one occured while reading the stream
    67          */
    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 {
    6960
    7061                DocumentBuilderFactory factory;
     
    7566                        db = factory.newDocumentBuilder();
    7667
    77                         Document document = db.parse(xmlStream);
     68                        Document document = db.parse(inputStream);
    7869                        NodeList classes = document.getElementsByTagName("CLASS");
    7970
     
    8172                                Node classNode = classes.item(i);
    8273                                FramsClass framsClass = processClass(classNode);
    83                                 mainClasses.put(framsClass.getId(), framsClass);
     74                                registry.putInfoIntoCache(framsClass);
    8475                        }
    8576
     
    10697                                                        symbolGlymph[j] = Integer.parseInt(sgha[j]);
    10798                                                } catch (NumberFormatException e) {
    108                                                         logger.error("an error occured while parsing symbol glymph, class getId: "
     99                                                        logger.error("an error occurred while parsing symbol glymph, class getId: "
    109100                                                                        + framsClass.getId()
    110101                                                                        + ", glymph offset: " + j);
     
    119110
    120111                } catch (IOException e) {
    121                         logger.fatal("unexpected exception occured: ", e);
     112                        logger.fatal("unexpected exception occurred: ", e);
    122113                        throw e;
    123114                } catch (ParserConfigurationException e) {
    124                         logger.fatal("unexpected exception occured: ", e);
     115                        logger.fatal("unexpected exception occurred: ", e);
    125116                        throw e;
    126117                } catch (SAXException e) {
    127                         logger.fatal("unexpected exception occured: ", e);
     118                        logger.fatal("unexpected exception occurred: ", e);
    128119                        throw e;
    129120                }
     
    140131         */
    141132        private static int getIntAttribute(NamedNodeMap attributes, String name) {
     133                String v = getAttribute(attributes, name);
     134                if (v == null) {
     135                        return 0;
     136                }
    142137                try {
    143                         return Integer.parseInt(getAttribute(attributes, name));
     138                        return Integer.parseInt(v);
    144139                } catch (NullPointerException e) {
    145140                        return 0;
    146141                } catch (NumberFormatException e) {
    147142                        logger.fatal("attribute " + name
    148                                         + " should be numeric (it is not)");
     143                                        + " should be numeric: " + v);
    149144                        return 0;
    150145                }
     
    186181         *             the exception in case of any error
    187182         */
    188         private FramsClass processClass(Node classNode) throws Exception {
     183        private static FramsClass processClass(Node classNode) throws Exception {
    189184                String classId = null;
    190185                String className = "";
     
    200195                classDescription = getAttributeFromNode("DESCRIPTION", classNode);
    201196
    202                 FramsClass framsClass = new FramsClass(classId, className,
    203                                 classDescription);
     197                FramsClass framsClass = new FramsClass(classId, className, classDescription);
    204198
    205199                NodeList classProperties = classNode.getChildNodes();
     
    210204                        if ("GROUP".equals(node.getNodeName())) {
    211205                                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 + "\" ("
    216209                                                        + className + ") is undefined");
    217                                 else
     210                                } else {
    218211                                        framsClass.appendGroup(new Group(name));
    219 
     212                                }
    220213                        } else if ("PROP".equals(node.getNodeName())
    221214                                        || "NEUROPROP".equals(node.getNodeName())) {
     
    232225
    233226        /**
    234          * It analyses the single property within the clas
     227         * It analyses the single property within the class
    235228         *
    236229         * @param attributes
     
    242235         *             the exception in case of any error
    243236         */
    244         private Param processParameter(NamedNodeMap attributes, String classId)
     237        private static Param processParameter(NamedNodeMap attributes, String classId)
    245238                        throws Exception {
    246239
     
    315308                                }
    316309                        }
    317                         builder.setType(DecimalParam.class);
     310                        builder.setType(FloatParam.class);
    318311                        builder.setMin(minMaxDefDouble.get("MIN"));
    319312                        builder.setMax(minMaxDefDouble.get("MAX"));
     
    330323        }
    331324
    332         public Map<String, FramsClass> getMainClasses() {
    333                 return Collections.unmodifiableMap(mainClasses);
    334         }
    335325
    336326        public Map<String, NeuroClass> getNeuroClasses() {
     
    338328        }
    339329
     330        public final Registry getRegistry() {
     331                return registry;
     332        }
     333
     334
    340335}
Note: See TracChangeset for help on using the changeset viewer.