source: java/main/src/main/java/com/framsticks/parsers/XmlLoader.java @ 90

Last change on this file since 90 was 90, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

CHANGELOG:
Make ProcedureParam? hold only ValueParams?.

Use id instead of names when naming gui components internally.

Basic procedure calling in GUI.

The actual procedure call is currently only backed
by the ObjectInstance?.

Add UnimplementedException?.

Improve naming of various gui elements.

Allow easy navigating in FEST Swing testing.

Add optional explicit order attribute to FramsClassAnnotation?.

That's because java reflection does return declared members
in any specific order. That ordering is needed only for
classes that have no representation in framsticks and need
a deterministic ordering of params.

Add ControlOwner? interface.

Add test for procedure calling in Browser.

First version of ParamAnnotation? for procedures.

Development of ProcedureParam?.

Add draft version of ProcedureParam? implementation in ReflectionAccess?.

Allow viewing FramsClasses? in gui Browser.

Extract ResourceBuilder? from ModelBuilder?.

Remove internalId from Param.

It was currently completely not utilised. Whether it is still needed
after introduction of ParamAnnotation? is arguable.

Add remaining param attributes to ParamAnnotation?.

Change AutoBuilder? semantics.

AutoBuilder? returns list of objects that are to be appended
with methods @AutoAppendAnnotation?.

This allows to omit explicit addition of ModelPackage? to instance
if the instance uses ModelBuilder? (registration of ModelPackage? comes
from schema).

Fix params ordering problem in auto created FramsClasses?.

Improve ObjectInstance?.

Several fixes to ModelBuilder?.

Improve test for ObjectInstance? in Browser.

Make initialization of robot static.

With robot recreated for second browser test, the test hanged
deep in AWT.

Add base convenience base test for Browser tests.

More tests to ObjectInstance?.

Rename Dispatcher.invokeLater() to dispatch().

Add assertDispatch.

It allows assertions in other threads, than TestNGInvoker.
Assertions are gathered after each method invocation and rethrown.

Use timeOut annotation attribute for tests involving some waiting.

Remove firstTask method (merge with joinableStart).

Clean up leftovers.

Remove unused FavouritesXMLFactory (the reading part is already
completely done with generic XmlLoader?, and writing part will be done
based on the same approach if needed).
Move UserFavourite? to the com.framsticks.gui.configuration package.

Remove GenotypeBrowser? as to specific.

This functionality will be available in ObjectInstance?.

Add interface ParamsPackage?.

Package containing registration of Java classes meant to use with
ReflectionAccess? may be in Instance using configuration.

Minor changes.

Make Group immutable.

Add AutoBuilder? interface extending Builder - only those would
be used to automatically build from XML.

Fix groups in FramsClass?.

Minor naming cleanup in Registry.

Add ModelComponent? interface.

All class creating the Model are implementing that interface.

Extract Model.build into ModelBuilder?.

ModelBuilder? will be compatible with other builders
and allow using it from configuration.

Fix NeuroConnection?.

Add synchronous get operation for dispatchers.

Rename JoinableMonitor? to Monitor.

Add ObjectInstance?.

This class is mainly for demonstration
and testing purposes.

Improve FramsServer? runner.

  • improve ExternalProcess? runner,
  • runner can kill the server but also react properly, when the server exists on it's own,
  • set default path to search for framsticks server installation,
  • add LoggingOutputListener?.
File size: 3.5 KB
Line 
1package com.framsticks.parsers;
2
3import java.io.InputStream;
4import java.util.LinkedList;
5import java.util.List;
6
7import javax.xml.parsers.DocumentBuilder;
8import javax.xml.parsers.DocumentBuilderFactory;
9
10import org.apache.log4j.Logger;
11import org.w3c.dom.Document;
12import org.w3c.dom.Element;
13import org.w3c.dom.NamedNodeMap;
14import org.w3c.dom.Node;
15import org.w3c.dom.NodeList;
16
17import com.framsticks.params.AccessInterface;
18import com.framsticks.params.Registry;
19import com.framsticks.util.AutoBuilder;
20import com.framsticks.util.FramsticksException;
21
22public class XmlLoader {
23        private static final Logger log = Logger.getLogger(XmlLoader.class);
24
25        protected Registry registry = new Registry();
26
27        /**
28         *
29         */
30        public XmlLoader() {
31        }
32
33        /**
34         * @return the registry
35         */
36        public Registry getRegistry() {
37                return registry;
38        }
39
40        boolean useLowerCase = false;
41
42        /**
43         * @param useLowerCase the useLowerCase to set
44         */
45        public void setUseLowerCase(boolean useLowerCase) {
46                this.useLowerCase = useLowerCase;
47        }
48
49        public Object processElement(Element element) {
50                String name = element.getNodeName();
51                if (useLowerCase) {
52                        name = name.toLowerCase();
53                }
54                if (name.equals("import")) {
55                        String className = element.getAttribute("class");
56                        try {
57                                registry.registerAndBuild(Class.forName(className));
58                                return null;
59                        } catch (ClassNotFoundException e) {
60                                throw new FramsticksException().msg("failed to import class").arg("name", name).cause(e);
61                        }
62                }
63
64                AccessInterface access = registry.createAccess(name);
65
66                if (access == null) {
67                        throw new FramsticksException().msg("failed to find access interface").arg("name", name);
68                }
69                Object object = access.createAccessee();
70                assert object != null;
71                access.select(object);
72
73                NamedNodeMap attributes = element.getAttributes();
74                for (int i = 0; i < attributes.getLength(); ++i) {
75                        Node attributeNode = attributes.item(i);
76                        access.set(attributeNode.getNodeName().toLowerCase(), attributeNode.getNodeValue());
77                }
78
79                NodeList children = element.getChildNodes();
80                log.debug("found " + children.getLength() + " children in " + object);
81                for (int i = 0; i < children.getLength(); ++i) {
82                        Node childNode = children.item(i);
83                        if (!(childNode instanceof Element)) {
84                                continue;
85                        }
86                        Object childObject = processElement((Element) childNode);
87                        if (childObject == null) {
88                                continue;
89                        }
90
91                        List<Object> childrenObjects = new LinkedList<>();
92
93                        if (childObject instanceof AutoBuilder) {
94                                childrenObjects.addAll(((AutoBuilder) childObject).autoFinish());
95                        } else {
96                                childrenObjects.add(childObject);
97                        }
98
99                        for (Object child : childrenObjects) {
100                                if (!access.tryAutoAppend(child)) {
101                                        throw new FramsticksException().msg("failed to auto append").arg("child", child).arg("parent", object);
102                                }
103                        }
104                }
105                log.debug("loaded " + object);
106
107                return object;
108        }
109
110        public Object load(InputStream stream) {
111                try {
112                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
113                        DocumentBuilder db = factory.newDocumentBuilder();
114
115                        Document document = db.parse(stream);
116                        document.getDocumentElement().normalize();
117                        Element element = document.getDocumentElement();
118                        assert element != null;
119
120                        return processElement(element);
121
122                } catch (Exception e) {
123                        throw new FramsticksException().msg("failed to load").cause(e);
124                }
125        }
126
127        public <T> T load(Class<T> type, InputStream stream) {
128                registry.registerAndBuild(type);
129
130                Object object = load(stream);
131                if (type.isAssignableFrom(object.getClass())) {
132                        return type.cast(object);
133                }
134                throw new FramsticksException().msg("invalid type has been loaded");
135        }
136}
137
Note: See TracBrowser for help on using the repository browser.