Source Code Cross Referenced for XmlEditorLoader.java in  » Report » jmagallanes-1.0 » com » calipso » xmleditor » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » jmagallanes 1.0 » com.calipso.xmleditor 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.calipso.xmleditor;
002:
003:        import org.w3c.dom.*;
004:        import javax.xml.parsers.DocumentBuilderFactory;
005:        import javax.xml.parsers.DocumentBuilder;
006:        import javax.swing.tree.DefaultTreeModel;
007:        import java.io.File;
008:        import java.util.*;
009:
010:        /**
011:         *
012:         * User: soliveri
013:         * Date: 07-oct-2003
014:         * Time: 15:55:17
015:         *
016:         */
017:
018:        public class XmlEditorLoader {
019:
020:            private DefaultTreeModel model;
021:            private XmlEditorTreeDefinitionNode definition;
022:            private Node domRoot;
023:
024:            public XmlEditorLoader(DefaultTreeModel model,
025:                    XmlEditorTreeDefinitionNode definition) {
026:                this .model = model;
027:                this .definition = definition;
028:            }
029:
030:            public XmlEditorLoader() {
031:            }
032:
033:            /**
034:             * Carga un archivo. Puede o no tener una definicion Xsd cargada (segun parametro).
035:             * @param file
036:             * @param hasXsd
037:             * @return
038:             * @throws XmlEditorException
039:             */
040:            public DefaultTreeModel loadFrom(File file, boolean hasXsd)
041:                    throws XmlEditorException {
042:                Document document = getNewDocumentFrom(file);
043:                Node docRoot = document.getDocumentElement();
044:                this .domRoot = docRoot;
045:                if (hasXsd) {
046:                    fillModelFrom(docRoot, (XmlEditorTreeModelNode) model
047:                            .getRoot(), definition);
048:                    return model;
049:                } else {
050:                    XmlEditorTreeModelNode root = new XmlEditorTreeModelNode(
051:                            docRoot.getNodeName());
052:                    fillModelFrom(root, docRoot);
053:                    return new DefaultTreeModel(root);
054:                }
055:            }
056:
057:            /**
058:             * Obtiene los atributos para un nodo.
059:             * @param node
060:             * @return
061:             */
062:            private Vector getAttrsFrom(Node node) {
063:                Vector vector = new Vector();
064:                NamedNodeMap attrs = node.getAttributes();
065:                for (int i = 0; i < attrs.getLength(); i++) {
066:                    Node attr = attrs.item(i);
067:                    vector.add(attr.getNodeName());
068:                }
069:                return vector;
070:            }
071:
072:            /**
073:             * Llena el modelo en base al documento cargado. Luego toma los hijos del nodo y los recorre recursivamente llamando a sus hijos.
074:             * @param root
075:             * @param docRoot
076:             */
077:            private void fillModelFrom(XmlEditorTreeModelNode root, Node docRoot) {
078:                Vector attrsNames = getAttrsFrom(docRoot);
079:                XmlEditorTreeModelNode newModelNode = new XmlEditorTreeModelNode(
080:                        getIdValueFrom(docRoot)/*docRoot.getAttributes().item(attrsNames.indexOf("Id")).toString()*/);//getIdValueFrom(docRoot));
081:                newModelNode.addAttributesFrom(attrsNames, getAttrs(docRoot,
082:                        attrsNames));
083:                root.add(newModelNode);
084:                fillModelFrom(docRoot.getChildNodes(), root);
085:            }
086:
087:            private void fillModelFrom(NodeList children,
088:                    XmlEditorTreeModelNode modelNode) {
089:                XmlEditorTreeModelNode newParentModelNode = null;
090:                boolean elementCreated = false;
091:                for (int i = 0; i < children.getLength(); i++) {
092:                    Node node = children.item(i);
093:                    if (node.getNodeType() == Node.ELEMENT_NODE) {
094:                        if (node.getAttributes().getLength() > 0) {
095:                            if (!elementCreated) {
096:                                newParentModelNode = new XmlEditorTreeModelNode(
097:                                        node.getNodeName());
098:                                elementCreated = true;
099:                                modelNode.add(newParentModelNode);
100:                            }
101:                            XmlEditorTreeModelNode newModelNode = new XmlEditorTreeModelNode(
102:                                    getIdValueFrom(node));
103:                            Vector attrsNames = getAttrsFrom(node);
104:                            newModelNode.addAttributesFrom(attrsNames,
105:                                    getAttrs(node, attrsNames));
106:                            newParentModelNode.add(newModelNode);
107:                        } else {
108:                            XmlEditorTreeModelNode newModelNode = new XmlEditorTreeModelNode(
109:                                    node.getNodeName());
110:                            modelNode.add(newModelNode);
111:                            fillModelFrom(node.getChildNodes(), newModelNode);
112:                        }
113:                    }
114:                }
115:            }
116:
117:            private void fillModelFrom(Node domRoot,
118:                    XmlEditorTreeModelNode modelNode,
119:                    XmlEditorTreeDefinitionNode definition) {
120:                //XmlEditorTreeModelNode newModelNode = new XmlEditorTreeModelNode(definition.getValue());
121:                //Vector attrsNames = getAttrsFrom(definition);
122:                //newModelNode.addAttributesFrom(attrsNames, getAttrs(domRoot,attrsNames));
123:                //modelNode.addAttributesFrom(attrsNames, getAttrs(domRoot,attrsNames));
124:                //modelNode.add(newModelNode);
125:                XmlEditorTreeModelNode newModelNode = getModelNodeFrom(
126:                        modelNode.children(), definition.getValue(), false);
127:                Vector attrsNames = getAttrsFrom(definition);
128:                newModelNode.addAttributesFrom(attrsNames, getAttrs(domRoot,
129:                        attrsNames));
130:                fillFrom(domRoot.getChildNodes(), definition);
131:            }
132:
133:            /**
134:             * Obtiene los atributos para un nodo de definicion.
135:             * @param definition
136:             * @return
137:             */
138:            private Vector getAttrsFrom(XmlEditorTreeDefinitionNode definition) {
139:                Vector vector = new Vector();
140:                Enumeration enumeration = definition.getItemsKeys().elements();
141:                while (enumeration.hasMoreElements()) {
142:                    vector.add(enumeration.nextElement().toString());
143:                }
144:                return vector;
145:            }
146:
147:            private void fillFrom(NodeList childNodes,
148:                    XmlEditorTreeDefinitionNode rootDefinition) {
149:                for (int i = 0; i < childNodes.getLength(); i++) {
150:                    Node node = childNodes.item(i);
151:                    if (node.getNodeType() == Node.ELEMENT_NODE) {
152:                        if (node.getAttributes() != null
153:                                && node.getAttributes().getLength() > 0) {
154:                            XmlEditorTreeModelNode parent = getModelNodeFrom(node
155:                                    .getNodeName());
156:                            XmlEditorTreeModelNode child = new XmlEditorTreeModelNode(
157:                                    getIdValueFrom(node));
158:                            Vector attrNames = getAttrsFrom(getNodeDefinitionFrom(
159:                                    rootDefinition, node.getNodeName()));
160:                            child.addAttributesFrom(attrNames, getAttrs(node,
161:                                    attrNames));
162:                            parent.add(child);
163:                        } else {
164:                            fillFrom(node.getChildNodes(), rootDefinition);
165:                        }
166:                    }
167:                }
168:            }
169:
170:            /**
171:             * Obtiene un nodo definicion en base a la definicion "root" y un nombre. Si no es esa, busca recursivamente entre los hijos.
172:             * @param rootDefinition
173:             * @param nodeName
174:             * @return
175:             */
176:            private XmlEditorTreeDefinitionNode getNodeDefinitionFrom(
177:                    XmlEditorTreeDefinitionNode rootDefinition, String nodeName) {
178:                if (rootDefinition.getValue().equals(nodeName)) {
179:                    return rootDefinition;
180:                } else {
181:                    return getNodeDefinitionFrom(rootDefinition.getSubnodes(),
182:                            nodeName, false);
183:                }
184:            }
185:
186:            private XmlEditorTreeDefinitionNode getNodeDefinitionFrom(
187:                    Map rootDefinition, String nodeName, boolean found) {
188:                XmlEditorTreeDefinitionNode returnVal = null;
189:                Iterator iterator = rootDefinition.keySet().iterator();
190:                while (iterator.hasNext() && !found) {
191:                    XmlEditorTreeDefinitionNode definition = (XmlEditorTreeDefinitionNode) rootDefinition
192:                            .get(iterator.next().toString());
193:                    if (definition.getValue().equals(nodeName)) {
194:                        return definition;
195:                    } else {
196:                        returnVal = getNodeDefinitionFrom(definition
197:                                .getSubnodes(), nodeName, found);
198:                        if (returnVal != null) {
199:                            found = true;
200:                        }
201:                    }
202:                }
203:                return returnVal;
204:            }
205:
206:            private String getIdValueFrom(Node node) {
207:                Vector names = getAttrsFrom(node);
208:                int pos = names.indexOf("Id");
209:                if (pos >= 0) {
210:                    return node.getAttributes().item(pos).getNodeValue()
211:                            .toString();
212:                } else if (names.indexOf("Name") >= 0) {
213:                    return node.getAttributes().item(names.indexOf("Name"))
214:                            .getNodeValue().toString();
215:                } else {
216:                    Node attrNode = node.getAttributes().item(0);
217:                    return attrNode.getNodeValue();
218:                }
219:            }
220:
221:            /*  private Vector getAttrsNames(Node node) {
222:             node.getAttributes()
223:             }*/
224:
225:            /**
226:             * Busca el nodo con el nombre dado. Si no es el root busca recursivamente en sus hijos.
227:             * @param nodeName
228:             * @return
229:             */
230:            private XmlEditorTreeModelNode getModelNodeFrom(String nodeName) {
231:                if (((XmlEditorTreeModelNode) model.getRoot()).getUserObject()
232:                        .equals(nodeName)) {
233:                    return (XmlEditorTreeModelNode) model.getRoot();
234:                } else {
235:                    return getModelNodeFrom(((XmlEditorTreeModelNode) model
236:                            .getRoot()).children(), nodeName, false);
237:                }
238:            }
239:
240:            private XmlEditorTreeModelNode getModelNodeFrom(
241:                    Enumeration children, String nodeName, boolean found) {
242:                XmlEditorTreeModelNode returnVal = null;
243:                while (children.hasMoreElements() && !found) {
244:                    XmlEditorTreeModelNode current = (XmlEditorTreeModelNode) children
245:                            .nextElement();
246:                    if (current.getUserObject().equals(nodeName)) {
247:                        return current;
248:                    } else {
249:                        returnVal = getModelNodeFrom(current.children(),
250:                                nodeName, found);
251:                        if (returnVal != null) {
252:                            found = true;
253:                        }
254:                    }
255:                }
256:                return returnVal;
257:            }
258:
259:            private Vector getAttrs(Node node, Vector attrNames) {
260:                Vector vector = new Vector();
261:                NamedNodeMap attrs = node.getAttributes();
262:                Enumeration enumeration = attrNames.elements();
263:                while (enumeration.hasMoreElements()) {
264:                    Node attr = attrs.getNamedItem(enumeration.nextElement()
265:                            .toString());
266:                    if (attr == null) {
267:                        vector.add("");
268:                    } else {
269:                        vector.add(attr.getNodeValue());
270:                    }
271:                }
272:                return vector;
273:            }
274:
275:            /**
276:             * Obtiene el documento en base al archivo XML
277:             * @param file
278:             * @return
279:             * @throws XmlEditorException
280:             */
281:            private Document getNewDocumentFrom(File file)
282:                    throws XmlEditorException {
283:                Document document = null;
284:                try {
285:                    DocumentBuilderFactory factory = DocumentBuilderFactory
286:                            .newInstance();
287:                    DocumentBuilder builder = factory.newDocumentBuilder();
288:                    document = builder.parse(file);
289:                } catch (Exception e) {
290:                    throw new XmlEditorException(e);
291:                }
292:                return document;
293:            }
294:
295:            public Node getDomRoot() {
296:                return domRoot;
297:            }
298:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.