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

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

HIGHLIGHTS:

  • add <include/> to configuration
  • add side notes to tree
    • used to store arbitrary information alongside the tree structure
  • migrate to log4j2
    • supports lazy string evaluation of passed arguments
  • improve GUI tree
    • it stays in synchronization with actual state (even in high load test scenario)
  • improve panel management in GUI
  • make loading objects in GUI more lazy
  • offload parsing to connection receiver thread
    • info parsing
    • first step of objects parsing
  • fix connection parsing bug (eof in long values)
  • support zero-arguments procedure in table view

CHANGELOG:
Implement procedure calls from table view.

Refactorization around procedures in tables.

Add table editor for buttons.

Render buttons in the the list view.

Further improve Columns.

Add Column class for TableModel?.

Accept also non-arguments ProcedureParams? in tableView.

Increase maximal TextAreaControl? size.

Add tooltip to ProcedureControl?.

Fix bug of interpreting eofs in long values by connection reader.

Further rework connection parsing.

Simplify client connection processing.

Test ListChange? modification.

Test ListChange? events with java server.

Add TestChild?.

Fix bug with fast deregistering when connecting to running server.

Another minor refactorization in TreeOperations?.

Fix bug in SimpleAbstractAccess? loading routine.

Another minor improvement.

Minor change.

Make reading of List objects two-phase.

Another minor change.

Dispatch parsing into receiver thread.

Another step.

Enclose passing value in ObjectParam? case in closure.

Minor step.

Minor change on way to offload parsing.

Temporarily comment out single ValueParam? get.

It will be generalized to multi ValueParam?.

Process info in receiver thread.

Add DispatchingExceptionHandler?.

Make waits in browser test longer.

Use FETCHED_MARK.

It is honored in GUI, where it used to decide whether to get values

after user action.

It is set in standard algorithm for processing fetched values.

Add remove operation to side notes.

Make loading more lazy.

Improve loading policy.

On node choose load itself, on node expansion, load children.

Minor improvement.

Fix bug with panel interleaving.

Minor improvements.

Improve panel management.

More cleaning around panels.

Reorganize panels.

Further improve tree.

Fix bug in TreeModel?.

Remove children from TreeNode?.

Implement TreeNode? hashCode and equals.

Make TreeNode? delegate equals and hashcode to internal reference.

Move listeners from TreeNode? to side notes.

Store path.textual as a side note.

Side note params instead of accesses for objects.

More refactorizations.

In TreeNode? bindAccess based on side notes.

Minor step.

Hide createAccess.

Rename AccessInterface? to Access.

Minor changes.

Several improvements in high load scenarios.

Change semantics of ArrayListAccess?.set(index, null);

It now removes the element, making list shorter
(it was set to null before).

Add path remove handler.

Handle exceptions in Connection.

Update .gitignore

Configure logging to file.

Move registration to TreeModel?.

Further refactorization.

Minor refactorization.

Minor improvements.

Use specialized event also for Modify action of ListChange?.

Use remove events.

Use the insertion events for tree.

Further improve tree refreshing.

Further improve reacting on events in GUI.

Fix problem with not adding objects on addition list change.

Migrate to log4j lazy String construction interface.

Migrate imports to log4j2.

Drop dependency on adapter to version 1.2.

Switch log4j implementation to log4j2.

Add dirty mark to the NodeAtFrame?.

Make selecting in AccessInterfaces? type safe.

Ignore containers size settings in Model and Genotype.

Use tree side notes to remember local changes and panels.

Add sideNotes to tree.

They will be used to store various accompanying information
right in the tree.

Use ReferenceIdentityMap? from apache in TreeNode?.

It suits the need perfectly (weak semantics on both key and value).

Make ArrayListParam? do not react size changes.

Guard in TableModel? before not yet loaded objects.

Add <include/> clause and AutoInjector?.

Extract common columns configuration to separate xml,
that can be included by other configurations.

File size: 4.7 KB
Line 
1package com.framsticks.parsers;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.InputStream;
7import java.util.LinkedList;
8import java.util.List;
9
10import javax.xml.parsers.DocumentBuilder;
11import javax.xml.parsers.DocumentBuilderFactory;
12
13import org.apache.logging.log4j.Logger;
14import org.apache.logging.log4j.LogManager;
15import org.w3c.dom.Document;
16import org.w3c.dom.Element;
17import org.w3c.dom.NamedNodeMap;
18import org.w3c.dom.Node;
19import org.w3c.dom.NodeList;
20
21import com.framsticks.params.Access;
22import com.framsticks.params.Registry;
23import com.framsticks.util.AutoBuilder;
24import com.framsticks.util.FramsticksException;
25import com.framsticks.util.lang.Strings;
26
27public class XmlLoader {
28        private static final Logger log = LogManager.getLogger(XmlLoader.class);
29
30        protected Registry registry = new Registry();
31
32        /**
33         *
34         */
35        public XmlLoader() {
36                registry.registerAndBuild(AutoInjector.class);
37        }
38
39        /**
40         * @return the registry
41         */
42        public Registry getRegistry() {
43                return registry;
44        }
45
46        boolean useLowerCase = false;
47
48        /**
49         * @param useLowerCase the useLowerCase to set
50         */
51        public void setUseLowerCase(boolean useLowerCase) {
52                this.useLowerCase = useLowerCase;
53        }
54
55        public String mangleName(String name) {
56                return useLowerCase ? name.toLowerCase() : name;
57        }
58
59        public String mangleAttribute(String name) {
60                return useLowerCase ? name.toLowerCase() : Strings.uncapitalize(name);
61        }
62
63        public Object processElement(Element element, Class<?> enclosingClass) {
64                final String name = mangleName(element.getNodeName());
65                if (name.equals("import")) {
66                        String className = element.getAttribute("class");
67                        try {
68                                registry.registerAndBuild(Class.forName(className));
69                                return null;
70                        } catch (ClassNotFoundException e) {
71                                throw new FramsticksException().msg("failed to import class").arg("name", name).cause(e);
72                        }
73                }
74                if (name.equals("include")) {
75                        String fileName = element.getAttribute("file");
76                        if (Strings.notEmpty(fileName)) {
77                                try {
78                                        return load(new FileInputStream(new File(fileName)), enclosingClass);
79                                } catch (FileNotFoundException e) {
80                                        throw new FramsticksException().msg("failed to include file").arg("file", fileName).cause(e);
81                                }
82                        }
83                        String resourceName = element.getAttribute("resource");
84                        if (Strings.notEmpty(resourceName)) {
85                                Class<?> javaClass = enclosingClass;
86                                String className = element.getAttribute("class");
87                                if (Strings.notEmpty(className)) {
88                                        try {
89                                                javaClass = Class.forName(className);
90                                        } catch (ClassNotFoundException e) {
91                                                throw new FramsticksException().msg("failed to find class for resource loading").arg("class name", className).cause(e);
92                                        }
93                                }
94
95                                return load(javaClass.getResourceAsStream(resourceName), enclosingClass);
96                        }
97                        throw new FramsticksException().msg("invalid <include/> node");
98                }
99
100                Access access = registry.createAccess(name);
101
102                Object object = access.createAccessee();
103                assert object != null;
104                access.select(object);
105
106                NamedNodeMap attributes = element.getAttributes();
107                for (int i = 0; i < attributes.getLength(); ++i) {
108                        Node attributeNode = attributes.item(i);
109                        access.set(mangleAttribute(attributeNode.getNodeName()), attributeNode.getNodeValue());
110                }
111
112                NodeList children = element.getChildNodes();
113                log.debug("found {} children in {}", children.getLength(), object);
114                for (int i = 0; i < children.getLength(); ++i) {
115                        Node childNode = children.item(i);
116                        if (!(childNode instanceof Element)) {
117                                continue;
118                        }
119                        Object childObject = processElement((Element) childNode, object.getClass());
120                        if (childObject == null) {
121                                continue;
122                        }
123
124                        List<Object> childrenObjects = new LinkedList<>();
125
126                        if (childObject instanceof AutoBuilder) {
127                                childrenObjects.addAll(((AutoBuilder) childObject).autoFinish());
128                        } else {
129                                childrenObjects.add(childObject);
130                        }
131
132                        for (Object child : childrenObjects) {
133                                access.tryAutoAppend(child);
134                        }
135                }
136                log.debug("loaded {}", object);
137
138                return object;
139        }
140
141        protected Object load(InputStream stream, Class<?> enclosingClass) {
142                try {
143                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
144                        DocumentBuilder db = factory.newDocumentBuilder();
145
146                        Document document = db.parse(stream);
147                        document.getDocumentElement().normalize();
148                        Element element = document.getDocumentElement();
149                        assert element != null;
150
151                        return processElement(element, enclosingClass);
152
153                } catch (Exception e) {
154                        throw new FramsticksException().msg("failed to load").cause(e);
155                }
156        }
157
158        public <T> T load(Class<T> type, InputStream stream) {
159                registry.registerAndBuild(type);
160
161                Object object = load(stream, type);
162                if (type.isAssignableFrom(object.getClass())) {
163                        return type.cast(object);
164                }
165                throw new FramsticksException().msg("invalid type has been loaded");
166        }
167}
168
Note: See TracBrowser for help on using the repository browser.