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

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

HIGHLIGHTS:

  • cleanup Instance management
    • extract Instance interface
    • extract Instance common algorithms to InstanceUtils?
  • fix closing issues: Ctrl+C or window close button

properly shutdown whole program

by Java Framsticks framework

  • fix parsing and printing of all request types
  • hide exception passing in special handle method of closures
    • substantially improve readability of closures
    • basically enable use of exception in asynchronous closures

(thrown exception is transported back to the caller)

  • implement call request on both sides

CHANGELOG:
Further improve calling.

Improve instance calling.

Calling is working on both sides.

Improve exception handling in testing.

Waiters do not supercede other apllication exception being thrown.

Finished parsing and printing of all request types (with tests).

Move implementation and tests of request parsing to Request.

Add tests for Requests.

Improve waits in asynchronours tests.

Extract more algorithms to InstanceUtils?.

Extract Instance.resolve to InstanceUtils?.

Improve naming.

Improve passing exception in InstanceClient?.

Hide calling of passed functor in StateCallback?.

Hide Exception passing in asynchronous closures.

Hide exception passing in Future.

Make ResponseCallback? an abstract class.

Make Future an abstract class.

Minor change.

Move getPath to Path.to()

Move bindAccess to InstanceUtils?.

Extract common things to InstanceUtils?.

Fix synchronization bug in Connection.

Move resolve to InstanceUtils?.

Allow names of Joinable to be dynamic.

Add support for set request server side.

More fixes in communication.

Fix issues with parsing in connection.

Cut new line characters when reading.

More improvements.

Migrate closures to FramsticksException?.

Several changes.

Extract resolveAndFetch to InstanceUtils? algorithms.

Test resolving and fetching.

More fixes with function signature deduction.

Do not print default values in SimpleAbstractAccess?.

Add test of FramsClass? printing.

Improve FramsticksException? messages.

Add explicit dispatcher synchronization feature.

Rework assertions in tests.

Previous solution was not generic enough.

Allow addition of joinables to collection after start.

Extract SimulatorInstance? from RemoteInstance?.

Remove PrivateJoinableCollection?.

Improve connections.

Move shutdown hook to inside the Monitor.

It should work in TestNG tests, but it seems that
hooks are not called.

In ServerTest? client connects to testing server.

Move socket initialization to receiver thread.

Add proper closing on Ctrl+C (don't use signals).

Fix bugs with server accepting connections.

Merge Entity into Joinable.

Reworking ServerInstance?.

Extract more algorithm to InstanceUtils?.

Extract some common functionality from AbstractInstance?.

Functions were placed in InstanceUtils?.

Hide registry of Instance.

Use ValueParam? in Instance interface.

Minor change.

Extract Instance interface.

Old Instance is now AbstractInstance?.

File size: 3.4 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                                access.tryAutoAppend(child);
101                        }
102                }
103                log.debug("loaded " + object);
104
105                return object;
106        }
107
108        public Object load(InputStream stream) {
109                try {
110                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
111                        DocumentBuilder db = factory.newDocumentBuilder();
112
113                        Document document = db.parse(stream);
114                        document.getDocumentElement().normalize();
115                        Element element = document.getDocumentElement();
116                        assert element != null;
117
118                        return processElement(element);
119
120                } catch (Exception e) {
121                        throw new FramsticksException().msg("failed to load").cause(e);
122                }
123        }
124
125        public <T> T load(Class<T> type, InputStream stream) {
126                registry.registerAndBuild(type);
127
128                Object object = load(stream);
129                if (type.isAssignableFrom(object.getClass())) {
130                        return type.cast(object);
131                }
132                throw new FramsticksException().msg("invalid type has been loaded");
133        }
134}
135
Note: See TracBrowser for help on using the repository browser.