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

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

HIGHLIGHTS:

  • add proper exception passing between communication sides:

if exception occur during handling client request, it is
automatically passed as comment to error response.

it may be used to snoop communication between peers

  • fix algorithm choosing text controls in GUI
  • allow GUI testing in virtual frame buffer (xvfb)

FEST had some problem with xvfb but workaround was found

supports tab-completion based on requests history

CHANGELOG:
Further improve handling of exceptions in GUI.

Add StatusBar? implementing ExceptionResultHandler?.

Make completion processing asynchronous.

Minor changes.

Improve completion in console.

Improve history in InteractiveConsole?.

First working version of DirectConsole?.

Minor changes.

Make Connection.address non final.

It is more suitable to use in configuration.

Improvement of consoles.

Improve PopupMenu? and closing of FrameJoinable?.

Fix BrowserTest?.

Found bug with FEST running under xvfb.

JButtonFixture.click() is not working under xvfb.
GuiTest? has wrapper which uses JButton.doClick() directly.

Store CompositeParam? param in TreeNode?.

Simplify ClientSideManagedConnection? connecting.

There is now connectedFunctor needed, ApplicationRequests? can be
send right after creation. They are buffered until the version
and features are negotiated.

Narow down interface of ClientSideManagedConnection?.

Allow that connection specialization send only
ApplicationRequests?.

Improve policy of text control choosing.

Change name of Genotype in BrowserTest?.

Make BrowserTest? change name of Genotype.

Minor change.

First working draft of TrackConsole?.

Simplify Consoles.

More improvements with gui joinables.

Unify initialization on gui joinables.

More rework of Frame based entities.

Refactorize structure of JFrames based entities.

Extract GuiTest? from BrowserBaseTest?.

Reorganize Console classes structure.

Add Collection view to JoinableCollection?.

Configure timeout in testing.

Minor changes.

Rework connections hierarchy.

Add Mode to the get operation.

Make get and set in Tree take PrimitiveParam?.

Unify naming of operations.

Make RunAt? use the given ExceptionHandler?.

It wraps the virtual runAt() method call with
try-catch passing exception to handler.

Force RunAt? to include ExceptionHandler?.

Improve ClientAtServer?.

Minor change.

Another sweep with FindBugs?.

Rename Instance to Tree.

Minor changes.

Minor changes.

Further clarify semantics of Futures.

Add FutureHandler?.

FutureHandler? is refinement of Future, that proxifies
exception handling to ExceptionResultHandler? given
at construction time.

Remove StateFunctor? (use Future<Void> instead).

Make Connection use Future<Void>.

Unparametrize *ResponseFuture?.

Remove StateCallback? not needed anymore.

Distinguish between sides of ResponseFuture?.

Base ResponseCallback? on Future (now ResponseFuture?).

Make asynchronous store taking Future for flags.

Implement storeValue in ObjectInstance?.

File size: 3.3 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                Object object = access.createAccessee();
67                assert object != null;
68                access.select(object);
69
70                NamedNodeMap attributes = element.getAttributes();
71                for (int i = 0; i < attributes.getLength(); ++i) {
72                        Node attributeNode = attributes.item(i);
73                        access.set(attributeNode.getNodeName().toLowerCase(), attributeNode.getNodeValue());
74                }
75
76                NodeList children = element.getChildNodes();
77                log.debug("found " + children.getLength() + " children in " + object);
78                for (int i = 0; i < children.getLength(); ++i) {
79                        Node childNode = children.item(i);
80                        if (!(childNode instanceof Element)) {
81                                continue;
82                        }
83                        Object childObject = processElement((Element) childNode);
84                        if (childObject == null) {
85                                continue;
86                        }
87
88                        List<Object> childrenObjects = new LinkedList<>();
89
90                        if (childObject instanceof AutoBuilder) {
91                                childrenObjects.addAll(((AutoBuilder) childObject).autoFinish());
92                        } else {
93                                childrenObjects.add(childObject);
94                        }
95
96                        for (Object child : childrenObjects) {
97                                access.tryAutoAppend(child);
98                        }
99                }
100                log.debug("loaded " + object);
101
102                return object;
103        }
104
105        public Object load(InputStream stream) {
106                try {
107                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
108                        DocumentBuilder db = factory.newDocumentBuilder();
109
110                        Document document = db.parse(stream);
111                        document.getDocumentElement().normalize();
112                        Element element = document.getDocumentElement();
113                        assert element != null;
114
115                        return processElement(element);
116
117                } catch (Exception e) {
118                        throw new FramsticksException().msg("failed to load").cause(e);
119                }
120        }
121
122        public <T> T load(Class<T> type, InputStream stream) {
123                registry.registerAndBuild(type);
124
125                Object object = load(stream);
126                if (type.isAssignableFrom(object.getClass())) {
127                        return type.cast(object);
128                }
129                throw new FramsticksException().msg("invalid type has been loaded");
130        }
131}
132
Note: See TracBrowser for help on using the repository browser.