Source Code Cross Referenced for SceneSerializer.java in  » IDE-Netbeans » web.core » org » netbeans » modules » web » jsf » navigation » graph » 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 » IDE Netbeans » web.core » org.netbeans.modules.web.jsf.navigation.graph 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.netbeans.modules.web.jsf.navigation.graph;
002:
003:        import org.openide.util.Exceptions;
004:        import org.openide.xml.XMLUtil;
005:        import org.w3c.dom.*;
006:        import org.xml.sax.ErrorHandler;
007:        import org.xml.sax.InputSource;
008:        import org.xml.sax.SAXException;
009:        import org.xml.sax.SAXParseException;
010:
011:        import java.awt.*;
012:        import java.io.*;
013:        import java.util.HashMap;
014:        import java.util.Map;
015:        import java.util.logging.Logger; //import org.netbeans.modules.web.jsf.navigation.PageFlowToolbarUtilities;
016:        import org.openide.filesystems.FileObject;
017:        import org.netbeans.modules.web.jsf.navigation.graph.PageFlowSceneData.PageData;
018:
019:        /**
020:         * @author David Kaspar
021:         */
022:        public class SceneSerializer {
023:
024:            private static final String SCENE_ELEMENT = "Scene"; // NOI18N
025:            private static final String SCENE_LAST_USED_SCOPE_ATTR = "Scope"; // NOI18N
026:            private static final String SCENE_SCOPE_ATTR = "Scope"; // NOI18N
027:            private static final String SCENE_SCOPE_ELEMENT = "Scope"; // NOI18N
028:            private static final String VERSION_ATTR = "version"; // NOI18NC
029:
030:            //    private static final String SCENE_FACES_SCOPE = PageFlowToolbarUtilities.getScopeLabel(PageFlowToolbarUtilities.Scope.SCOPE_FACESCONFIG); //NOI18N
031:            //    private static final String SCENE_PROJECT_SCOPE = PageFlowToolbarUtilities.getScopeLabel(PageFlowToolbarUtilities.Scope.SCOPE_PROJECT);
032:
033:            //    private static final String SCENE_NODE_COUNTER_ATTR = "nodeIDcounter"; // NOI18N
034:            //    private static final String SCENE_EDGE_COUNTER_ATTR = "edgeIDcounter"; // NOI18N
035:
036:            private static final String NODE_ELEMENT = "Node"; // NOI18N
037:            private static final String NODE_ID_ATTR = "id"; // NOI18N
038:            private static final String NODE_X_ATTR = "x"; // NOI18N
039:            private static final String NODE_Y_ATTR = "y"; // NOI18N    
040:            private static final String NODE_ZOOM_ATTR = "zoom"; // NOI18N
041:
042:            //    private static final String EDGE_ELEMENT = "Edge"; // NOI18N
043:            //    private static final String EDGE_ID_ATTR = "id"; // NOI18N
044:            //    private static final String EDGE_SOURCE_ATTR = "source"; // NOI18N
045:            //    private static final String EDGE_TARGET_ATTR = "target"; // NOI18N
046:
047:            private static final String VERSION_VALUE_1 = "1"; // NOI18N
048:            private static final String VERSION_VALUE_2 = "2"; // NOI18N
049:
050:            private SceneSerializer() {
051:            }
052:
053:            // call in AWT to serialize scene
054:            //    public static void serialize(PageFlowScene scene, File file) {
055:            //        Document document = XMLUtil.createDocument(SCENE_ELEMENT, null, null, null);
056:            //
057:            //        Node sceneElement = document.getFirstChild();
058:            //        setAttribute(document, sceneElement, VERSION_ATTR, VERSION_VALUE_1);
059:            //
060:            //
061:            //
062:            //        //        setAttribute (document, sceneElement, SCENE_NODE_COUNTER_ATTR, Long.toString (scene.nodeIDcounter));
063:            //        //        setAttribute (document, sceneElement, SCENE_EDGE_COUNTER_ATTR, Long.toString (scene.edgeIDcounter));
064:            //
065:            //        for (Page page : scene.getNodes()) {
066:            //            Element nodeElement = document.createElement(NODE_ELEMENT);
067:            //            setAttribute(document, nodeElement, NODE_ID_ATTR, page.getDisplayName());
068:            //            Widget widget = scene.findWidget(page);
069:            //            Point location = widget.getPreferredLocation();
070:            //            if( location == null ) {
071:            //                location = widget.getLocation();
072:            //            }
073:            //            setAttribute(document, nodeElement, NODE_X_ATTR, Integer.toString(location.x));
074:            //            setAttribute(document, nodeElement, NODE_Y_ATTR, Integer.toString(location.y));
075:            //            sceneElement.appendChild(nodeElement);
076:            //        }
077:            //        //        for (String edge : scene.getEdges ()) {
078:            //        //            Element edgeElement = document.createElement (EDGE_ELEMENT);
079:            //        //            setAttribute (document, edgeElement, EDGE_ID_ATTR, edge);
080:            //        //            String sourceNode = scene.getEdgeSource (edge);
081:            //        //            if (sourceNode != null)
082:            //        //                setAttribute (document, edgeElement, EDGE_SOURCE_ATTR, sourceNode);
083:            //        //            String targetNode = scene.getEdgeTarget (edge);
084:            //        //            if (targetNode != null)
085:            //        //                setAttribute (document, edgeElement, EDGE_TARGET_ATTR, targetNode);
086:            //        //            sceneElement.appendChild (edgeElement);
087:            //        //        }
088:            //
089:            //        FileOutputStream fos = null;
090:            //        try {
091:            //            fos = new FileOutputStream(file);
092:            //            XMLUtil.write(document, fos, "UTF-8"); // NOI18N
093:            //        } catch (Exception e) {
094:            //            Exceptions.printStackTrace(e);
095:            //        } finally {
096:            //            try {
097:            //                if (fos != null) {
098:            //                    fos.close();
099:            //                }
100:            //            } catch (Exception e) {
101:            //                Exceptions.printStackTrace(e);
102:            //            }
103:            //        }
104:            //    }
105:            //
106:
107:            public static void serialize(PageFlowSceneData sceneData,
108:                    FileObject file) {
109:                if (file == null || !file.isValid()) {
110:                    LOG
111:                            .warning("Can not serialize locations because file is null.");
112:                    return;
113:                }
114:                LOG.entering("SceneSerializer", "serialize");
115:                Document document = XMLUtil.createDocument(SCENE_ELEMENT, null,
116:                        null, null);
117:
118:                Node sceneElement = document.getFirstChild();
119:                setAttribute(document, sceneElement, VERSION_ATTR,
120:                        VERSION_VALUE_2);
121:                setAttribute(document, sceneElement,
122:                        SCENE_LAST_USED_SCOPE_ATTR, XmlScope.getInstance(
123:                                sceneData.getCurrentScopeStr()).toString());
124:                Node scopeFacesElement = createScopeElement(document,
125:                        sceneData, XmlScope.SCOPE_FACES);
126:                if (scopeFacesElement != null) {
127:                    sceneElement.appendChild(scopeFacesElement);
128:                }
129:                Node scopeProjectElement = createScopeElement(document,
130:                        sceneData, XmlScope.SCOPE_PROJECT);
131:                if (scopeProjectElement != null) {
132:                    sceneElement.appendChild(scopeProjectElement);
133:                }
134:                Node scopeAllElement = createScopeElement(document, sceneData,
135:                        XmlScope.SCOPE_ALL);
136:                if (scopeAllElement != null) {
137:                    sceneElement.appendChild(scopeAllElement);
138:                }
139:
140:                writeToFile(document, file);
141:                LOG.finest("Serializing to the follwoing file: "
142:                        + file.toString());
143:
144:                LOG.exiting("SceneSerializer", "serialize");
145:            }
146:
147:            /**
148:             * @param Should be either SCENE_PROJECT_SCOPR or SCENE_FACES_SCOPE
149:             **/
150:            private final static Node createScopeElement(Document document,
151:                    PageFlowSceneData sceneData, XmlScope scopeXml) {
152:                Node sceneScopeElement = null;
153:                Map<String, PageFlowSceneData.PageData> facesConfigScopeMap = sceneData
154:                        .getScopeData(scopeXml.getScope());
155:                if (facesConfigScopeMap != null) {
156:                    sceneScopeElement = document
157:                            .createElement(SCENE_SCOPE_ELEMENT);
158:                    setAttribute(document, sceneScopeElement, SCENE_SCOPE_ATTR,
159:                            scopeXml.toString());
160:
161:                    for (String key : facesConfigScopeMap.keySet()) {
162:                        PageFlowSceneData.PageData data = facesConfigScopeMap
163:                                .get(key);
164:                        if (data != null) {
165:                            Element nodeElement = document
166:                                    .createElement(NODE_ELEMENT);
167:                            setAttribute(document, nodeElement, NODE_ID_ATTR,
168:                                    key);
169:                            setAttribute(document, nodeElement, NODE_X_ATTR,
170:                                    Integer.toString(data.getPoint().x));
171:                            setAttribute(document, nodeElement, NODE_Y_ATTR,
172:                                    Integer.toString(data.getPoint().y));
173:                            setAttribute(document, nodeElement, NODE_ZOOM_ATTR,
174:                                    Boolean.toString(data.isMinimized()));
175:                            sceneScopeElement.appendChild(nodeElement);
176:                        }
177:                    }
178:                }
179:                return sceneScopeElement;
180:
181:            }
182:
183:            private final static void writeToFile(Document document,
184:                    FileObject file) {
185:                OutputStream fos = null;
186:                try {
187:                    fos = file.getOutputStream();
188:                    XMLUtil.write(document, fos, "UTF-8"); // NOI18N
189:                } catch (Exception e) {
190:                    Exceptions.printStackTrace(e);
191:                } finally {
192:                    try {
193:                        if (fos != null) {
194:                            fos.close();
195:                        }
196:                    } catch (Exception e) {
197:                        Exceptions.printStackTrace(e);
198:                    }
199:                }
200:            }
201:
202:            private final static Logger LOG = Logger
203:                    .getLogger("org.netbeans.modules.web.jsf.navigation");
204:
205:            // call in AWT to deserialize scene
206:            public static void deserializeV1(PageFlowSceneData sceneData,
207:                    FileObject file) {
208:                LOG
209:                        .entering("SceneSerializer",
210:                                "deserializeV1(PageFlowSceneData sceneData, File file)");
211:                Node sceneElement = getRootNode(file);
212:
213:                //        scene.nodeIDcounter = Long.parseLong (getAttributeValue (sceneElement, SCENE_NODE_COUNTER_ATTR));
214:                //        scene.edgeIDcounter = Long.parseLong (getAttributeValue (sceneElement, SCENE_EDGE_COUNTER_ATTR));
215:
216:                Map<String, PageData> sceneInfo = new HashMap<String, PageData>();
217:                for (Node element : getChildNode(sceneElement)) {
218:                    if (NODE_ELEMENT.equals(element.getNodeName())) {
219:                        String pageId = getAttributeValue(element, NODE_ID_ATTR);
220:                        int x = Integer.parseInt(getAttributeValue(element,
221:                                NODE_X_ATTR));
222:                        int y = Integer.parseInt(getAttributeValue(element,
223:                                NODE_Y_ATTR));
224:
225:                    }
226:                }
227:                sceneData.setScopeData(XmlScope.SCOPE_PROJECT.getScope(),
228:                        sceneInfo);
229:                LOG.exiting("SceneSerializer", "deserialize");
230:            }
231:
232:            public static void deserialize(PageFlowSceneData sceneData,
233:                    FileObject file) {
234:                LOG.entering("SceneSerializer",
235:                        "deserialize(PageFlowSceneData sceneData, File file)");
236:                Node sceneElement = getRootNode(file);
237:                if (VERSION_VALUE_1.equals(getAttributeValue(sceneElement,
238:                        VERSION_ATTR))) {
239:                    deserializeV1(sceneData, file);
240:                } else if (VERSION_VALUE_2.equals(getAttributeValue(
241:                        sceneElement, VERSION_ATTR))) {
242:
243:                    String lastUsedScopeXML = getAttributeValue(sceneElement,
244:                            SCENE_LAST_USED_SCOPE_ATTR);
245:                    XmlScope lastUsedScope = XmlScope
246:                            .getInstance(lastUsedScopeXML);
247:                    sceneData.setCurrentScope(lastUsedScope.getScope());
248:                    LOG.fine("Last Used Scope: " + lastUsedScope);
249:                    // TODO: Save the Last Used Scope
250:
251:                    NodeList scopeNodes = sceneElement.getChildNodes();
252:                    for (int i = 0; i < scopeNodes.getLength(); i++) {
253:                        Node scopeElement = scopeNodes.item(i);
254:                        if (scopeElement.getNodeName().equals(
255:                                SCENE_SCOPE_ELEMENT)) {
256:                            String scopeXMLStr = getAttributeValue(
257:                                    scopeElement, SCENE_SCOPE_ATTR);
258:                            NodeList pageNodes = scopeElement.getChildNodes();
259:                            Map<String, PageData> sceneInfo = new HashMap<String, PageData>();
260:                            for (int j = 0; j < pageNodes.getLength(); j++) {
261:                                Node pageNode = pageNodes.item(j);
262:                                if (pageNode.getNodeName().equals(NODE_ELEMENT)) {
263:                                    String pageDisplayName = getAttributeValue(
264:                                            pageNode, NODE_ID_ATTR);
265:                                    int x = Integer.parseInt(getAttributeValue(
266:                                            pageNode, NODE_X_ATTR));
267:                                    int y = Integer.parseInt(getAttributeValue(
268:                                            pageNode, NODE_Y_ATTR));
269:                                    boolean isMinimized = false;
270:                                    String zoom = getAttributeValue(pageNode,
271:                                            NODE_ZOOM_ATTR);
272:                                    if (zoom != null) {
273:                                        isMinimized = Boolean
274:                                                .parseBoolean(zoom);
275:                                    }
276:                                    PageData data = PageFlowSceneData
277:                                            .createPageData(new Point(x, y),
278:                                                    isMinimized);
279:                                    sceneInfo.put(pageDisplayName, data);
280:                                }
281:                            }
282:                            sceneData.setScopeData(XmlScope.getInstance(
283:                                    scopeXMLStr).getScope(), sceneInfo);
284:                        }
285:                    }
286:                }
287:
288:                LOG.exiting("SceneSerializer",
289:                        "deserialize(PageFlowSceneData sceneData, File file)");
290:            }
291:
292:            private static void setAttribute(Document xml, Node node,
293:                    String name, String value) {
294:                NamedNodeMap map = node.getAttributes();
295:                Attr attribute = xml.createAttribute(name);
296:                attribute.setValue(value);
297:                map.setNamedItem(attribute);
298:            }
299:
300:            private static Node getRootNode(FileObject file) {
301:                InputStream is = null;
302:                try {
303:                    is = file.getInputStream();
304:                    Document doc = XMLUtil.parse(new InputSource(is), false,
305:                            false, new ErrorHandler() {
306:                                public void error(SAXParseException e)
307:                                        throws SAXException {
308:                                    throw new SAXException(e);
309:                                }
310:
311:                                public void fatalError(SAXParseException e)
312:                                        throws SAXException {
313:                                    throw new SAXException(e);
314:                                }
315:
316:                                public void warning(SAXParseException e) {
317:                                    Exceptions.printStackTrace(e);
318:                                }
319:                            }, null);
320:                    return doc.getFirstChild();
321:                } catch (Exception e) {
322:                    Exceptions.printStackTrace(e);
323:                } finally {
324:                    try {
325:                        if (is != null) {
326:                            is.close();
327:                        }
328:                    } catch (IOException e) {
329:                        Exceptions.printStackTrace(e);
330:                    }
331:                }
332:                return null;
333:            }
334:
335:            private static String getAttributeValue(Node node, String attr) {
336:                try {
337:                    if (node != null) {
338:                        NamedNodeMap map = node.getAttributes();
339:                        if (map != null) {
340:                            Node mynode = map.getNamedItem(attr);
341:                            if (mynode != null) {
342:                                return mynode.getNodeValue();
343:                            }
344:                        }
345:                    }
346:                } catch (DOMException e) {
347:                    Exceptions.printStackTrace(e);
348:                }
349:                return null;
350:            }
351:
352:            private static Node[] getChildNode(Node node) {
353:                NodeList childNodes = node.getChildNodes();
354:                Node[] nodes = new Node[childNodes != null ? childNodes
355:                        .getLength() : 0];
356:                for (int i = 0; i < nodes.length; i++) {
357:                    nodes[i] = childNodes.item(i);
358:                }
359:                return nodes;
360:            }
361:
362:        }
w__w___w__._jav___a_2___s.__c__o__m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.