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

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

HIGHLIGTS:

  • complete events implementation
  • add CLI in Java Framsticks server
  • add automatic registration for events in GUI
  • improve objects fetching (object are never overwritten with new instances)
  • properly react for ListChange? events
  • add ListPanel? with table view
    • columns to be shown may be statically specified in configuration
    • currently modyfying data through tables is not available
  • improve maven configuration
    • configuration file may be specified without touching pom.xml

CHANGELOG:
Extract constants from Flags into ParamFlags? and SetStateFlags?.

Extract flags I/O to FlagsUtils? class.

Configured maven to exec given resource configuration.

For example:
mvn exec:exec -Dframsticks.config=/configs/managed-console.xml

Cleanup pom.xml

Rename ObjectTree? to LocalTree? (also make LocalTree? and RemoteTree? final).

Minor change.

Add maximum number of columns in ListPanelProvider?.

Improve ColumnsConfig? interpretation.

Automatically fill FramsClass?.name if trying to construct empty.

Improve identitifer case mangling in XmlLoader?.

Introduce configurable ColumnsConfig?.

Draft working version of ListPanel?.

Table is being shown (although empty).

More improvements to table building.

Move some functionality from Frame to TreeModel?.

Move tree classes in gui to separate package.

Remove old table related classes.

Add draft implementation of TableModel?.

Redirect ParamBuilder?.forAccess to AccessInterface?.

Optimize ParamBuilder?.forAccess()

Do not clear list when loading.

Do not load fetched values directly.

Implement different AccessInterface? copying policy.

Optimize fetching values routine.

Remove Mode enum (work out get semantics).

Some improvements to ListChange? handling.

Improve UniqueListAccess?.

Add reaction for ListChanges? in the TreeNode?.

EventListeners? are being added in the TreeNode?.

Listeners for ListParams? are now very naive (they download
whole list).

Automatially register on events in GUI.

Events are working in RemoteTree? and Server.

Move listeners to the ClientSideManagedConnection?.

Remove old classes responsible for event subscriptions.

Improve event reading.

Improve events handling at server side.

Add register attribute in FramsClassAnnotation?
to automatically also register other classes.

Registering events works.

Setup for remote listeners registration.

More improvements.

Minor changes.

Add rootTree to the ClientAtServer?.

Moving CLI to the ClientAtServer?.

Fix bug: use Void.TYPE instead of Void.class

More development around CLI.

  • Improve Path resolving.

Add synthetic root to ObjectTree?.

It is needed to allow sybling for the original root
that would containg CLI.

Some work with registering events in RemoteTree?.

Draft implementation of listener registering in RemoteTree?.

Support events registration in the ObjectTree?.

Add events support to ReflectionAccess?.

EventParam? is recognized by ParamCandidate?.

Prepare interface for Events across project.

Add EventListener? and API for listeners in Tree.

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;
21import com.framsticks.util.lang.Strings;
22
23public class XmlLoader {
24        private static final Logger log = Logger.getLogger(XmlLoader.class);
25
26        protected Registry registry = new Registry();
27
28        /**
29         *
30         */
31        public XmlLoader() {
32        }
33
34        /**
35         * @return the registry
36         */
37        public Registry getRegistry() {
38                return registry;
39        }
40
41        boolean useLowerCase = false;
42
43        /**
44         * @param useLowerCase the useLowerCase to set
45         */
46        public void setUseLowerCase(boolean useLowerCase) {
47                this.useLowerCase = useLowerCase;
48        }
49
50        public String mangleName(String name) {
51                return useLowerCase ? name.toLowerCase() : name;
52        }
53
54        public String mangleAttribute(String name) {
55                return useLowerCase ? name.toLowerCase() : Strings.uncapitalize(name);
56        }
57
58        public Object processElement(Element element) {
59                final String name = mangleName(element.getNodeName());
60                if (name.equals("import")) {
61                        String className = element.getAttribute("class");
62                        try {
63                                registry.registerAndBuild(Class.forName(className));
64                                return null;
65                        } catch (ClassNotFoundException e) {
66                                throw new FramsticksException().msg("failed to import class").arg("name", name).cause(e);
67                        }
68                }
69
70                AccessInterface access = registry.createAccess(name);
71
72                Object object = access.createAccessee();
73                assert object != null;
74                access.select(object);
75
76                NamedNodeMap attributes = element.getAttributes();
77                for (int i = 0; i < attributes.getLength(); ++i) {
78                        Node attributeNode = attributes.item(i);
79                        access.set(mangleAttribute(attributeNode.getNodeName()), attributeNode.getNodeValue());
80                }
81
82                NodeList children = element.getChildNodes();
83                log.debug("found " + children.getLength() + " children in " + object);
84                for (int i = 0; i < children.getLength(); ++i) {
85                        Node childNode = children.item(i);
86                        if (!(childNode instanceof Element)) {
87                                continue;
88                        }
89                        Object childObject = processElement((Element) childNode);
90                        if (childObject == null) {
91                                continue;
92                        }
93
94                        List<Object> childrenObjects = new LinkedList<>();
95
96                        if (childObject instanceof AutoBuilder) {
97                                childrenObjects.addAll(((AutoBuilder) childObject).autoFinish());
98                        } else {
99                                childrenObjects.add(childObject);
100                        }
101
102                        for (Object child : childrenObjects) {
103                                access.tryAutoAppend(child);
104                        }
105                }
106                log.debug("loaded " + object);
107
108                return object;
109        }
110
111        public Object load(InputStream stream) {
112                try {
113                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
114                        DocumentBuilder db = factory.newDocumentBuilder();
115
116                        Document document = db.parse(stream);
117                        document.getDocumentElement().normalize();
118                        Element element = document.getDocumentElement();
119                        assert element != null;
120
121                        return processElement(element);
122
123                } catch (Exception e) {
124                        throw new FramsticksException().msg("failed to load").cause(e);
125                }
126        }
127
128        public <T> T load(Class<T> type, InputStream stream) {
129                registry.registerAndBuild(type);
130
131                Object object = load(stream);
132                if (type.isAssignableFrom(object.getClass())) {
133                        return type.cast(object);
134                }
135                throw new FramsticksException().msg("invalid type has been loaded");
136        }
137}
138
Note: See TracBrowser for help on using the repository browser.