Source Code Cross Referenced for DocumentSave.java in  » IDE-Netbeans » vmd.analyzer » org » netbeans » modules » vmd » io » 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 » vmd.analyzer » org.netbeans.modules.vmd.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:        package org.netbeans.modules.vmd.io;
042:
043:        import org.netbeans.modules.vmd.api.model.*;
044:        import org.netbeans.modules.vmd.api.io.DataSerializer;
045:        import org.netbeans.modules.vmd.api.io.DataObjectContext;
046:        import org.netbeans.modules.vmd.api.io.ProjectTypeInfo;
047:        import org.netbeans.modules.vmd.api.io.providers.IOSupport;
048:        import org.openide.xml.XMLUtil;
049:        import org.openide.ErrorManager;
050:        import org.openide.util.Lookup;
051:        import org.openide.filesystems.FileObject;
052:        import org.openide.filesystems.FileLock;
053:        import org.openide.filesystems.FileSystem;
054:        import org.w3c.dom.Document;
055:        import org.w3c.dom.Node;
056:        import org.w3c.dom.NamedNodeMap;
057:        import org.w3c.dom.Attr;
058:
059:        import java.util.HashSet;
060:        import java.util.Collection;
061:        import java.io.IOException;
062:        import java.io.OutputStream;
063:
064:        /**
065:         * @author David Kaspar
066:         */
067:        // TODO - should not we save all components including ones outside of the tree
068:        // TODO - use versions
069:        public class DocumentSave {
070:
071:            static final Lookup.Result<DataSerializer> customDataSerializers = Lookup
072:                    .getDefault().lookupResult(DataSerializer.class);
073:
074:            static final String XML_ROOT_NODE = "VisualDesign"; // NOI18N
075:            static final String PROP_PROJECT_TYPE = "projectType"; // NOI18N
076:            static final String DOCUMENT_NODE = "Document"; // NOI18N
077:            static final String VERSION_ATTR = "version"; // NOI18N
078:            static final String COMPONENT_NODE = "Component"; // NOI18N
079:            static final String PROPERTY_NODE = "Property"; // NOI18N
080:            static final String COMPONENTID_ATTR = "componentID"; // NOI18N
081:            static final String TYPEID_ATTR = "typeID"; // NOI18N
082:            static final String NAME_ATTR = "name"; // NOI18N
083:            static final String VALUE_ATTR = "value"; // NOI18N
084:
085:            static final String VERSION_VALUE_1 = "1"; // NOI18N
086:
087:            static Object sync = new Object();
088:
089:            public static void save(DataObjectContext context,
090:                    DesignDocument savingDocument) {
091:                Document xml = XMLUtil.createDocument(XML_ROOT_NODE, null,
092:                        null, null);// TODO - NS, DTD
093:                Node xmlRootNode = xml.getFirstChild();
094:                setAttribute(xml, xmlRootNode, VERSION_ATTR, VERSION_VALUE_1);
095:                String projectType = savingDocument.getDocumentInterface()
096:                        .getProjectType();
097:                setAttribute(xml, xmlRootNode, PROP_PROJECT_TYPE, projectType);
098:
099:                Node node = xml.createElement(DOCUMENT_NODE);
100:                setAttribute(xml, node, VERSION_ATTR, ProjectTypeInfo
101:                        .getProjectTypeInfoFor(projectType)
102:                        .getDocumentVersion());
103:
104:                checkDocumentValidity(savingDocument);
105:                saveComponent(xml, node, savingDocument.getRootComponent());
106:                xmlRootNode.appendChild(node);
107:
108:                for (DataSerializer serializer : customDataSerializers
109:                        .allInstances()) {
110:                    Node data = serializer.serializeData(context,
111:                            savingDocument, xml);
112:                    if (data != null)
113:                        xml.getFirstChild().appendChild(data);
114:                }
115:
116:                try {
117:                    writeDocument(IOSupport.getDesignFile(context), xml);
118:                } catch (IOException e) {
119:                    throw Debug.error(e);
120:                }
121:            }
122:
123:            private static void checkDocumentValidity(
124:                    DesignDocument savingDocument) {
125:                HashSet<DesignComponent> componentsInTree = new HashSet<DesignComponent>();
126:                HashSet<DesignComponent> referencedComponents = new HashSet<DesignComponent>();
127:
128:                collectComponentsInTree(componentsInTree, savingDocument
129:                        .getRootComponent());
130:                for (DesignComponent component : componentsInTree)
131:                    collectUsedReferences(referencedComponents, component);
132:
133:                for (DesignComponent component : referencedComponents)
134:                    if (!componentsInTree.contains(component))
135:                        Debug.warning("Saving",
136:                                "Referenced component is not in the tree",
137:                                component); // NOI18N
138:
139:            }
140:
141:            private static void saveComponent(Document xml, Node parentNode,
142:                    DesignComponent component) {
143:                ComponentDescriptor descriptor = component
144:                        .getComponentDescriptor();
145:
146:                Node node = xml.createElement(COMPONENT_NODE);
147:                parentNode.appendChild(node);
148:
149:                setAttribute(xml, node, COMPONENTID_ATTR, Long
150:                        .toString(component.getComponentID()));
151:                setAttribute(xml, node, TYPEID_ATTR, component.getType()
152:                        .getEncoded());
153:
154:                Collection<PropertyDescriptor> propertyDescriptors = descriptor
155:                        .getPropertyDescriptors();
156:                for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
157:                    if (!propertyDescriptor.isUseForSerialization())
158:                        continue;
159:                    String propertyName = propertyDescriptor.getName();
160:                    if (component.isDefaultValue(propertyName))
161:                        continue;
162:                    PropertyValue propertyValue = component
163:                            .readProperty(propertyName);
164:                    String serialized = propertyValue.serialize();
165:
166:                    Node propertyNode = xml.createElement(PROPERTY_NODE);
167:                    node.appendChild(propertyNode);
168:                    setAttribute(xml, propertyNode, NAME_ATTR,
169:                            propertyDescriptor.getName());
170:                    setAttribute(xml, propertyNode, TYPEID_ATTR,
171:                            propertyDescriptor.getType().getEncoded());
172:                    setAttribute(xml, propertyNode, VALUE_ATTR, serialized);
173:                }
174:
175:                for (DesignComponent child : component.getComponents())
176:                    saveComponent(xml, node, child);
177:            }
178:
179:            private static void collectComponentsInTree(
180:                    HashSet<DesignComponent> componentsInTree,
181:                    DesignComponent component) {
182:                componentsInTree.add(component);
183:                for (DesignComponent child : component.getComponents())
184:                    collectComponentsInTree(componentsInTree, child);
185:            }
186:
187:            private static void collectUsedReferences(
188:                    HashSet<DesignComponent> referencedComponents,
189:                    DesignComponent component) {
190:                ComponentDescriptor descriptor = component
191:                        .getComponentDescriptor();
192:                Collection<PropertyDescriptor> propertyDescriptors = descriptor
193:                        .getPropertyDescriptors();
194:                for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
195:                    if (!propertyDescriptor.isUseForSerialization())
196:                        continue;
197:                    PropertyValue propertyValue = component
198:                            .readProperty(propertyDescriptor.getName());
199:                    Debug.collectAllComponentReferences(propertyValue,
200:                            referencedComponents);
201:                }
202:            }
203:
204:            private static void writeDocument(final FileObject file,
205:                    final Document doc) throws IOException {
206:                synchronized (sync) {
207:                    file.getFileSystem().runAtomicAction(
208:                            new FileSystem.AtomicAction() {
209:                                public void run() throws IOException {
210:                                    OutputStream os = null;
211:                                    FileLock lock = null;
212:                                    try {
213:                                        lock = file.lock();
214:                                        os = file.getOutputStream(lock);
215:                                        XMLUtil.write(doc, os, "UTF-8"); // NOI18N
216:                                    } finally {
217:                                        if (os != null)
218:                                            try {
219:                                                os.close();
220:                                            } catch (IOException e) {
221:                                                ErrorManager.getDefault()
222:                                                        .notify(e);
223:                                            }
224:                                        if (lock != null)
225:                                            lock.releaseLock();
226:                                    }
227:                                }
228:                            });
229:                }
230:            }
231:
232:            private static void setAttribute(Document xml, Node node,
233:                    String name, String value) {
234:                NamedNodeMap map = node.getAttributes();
235:                Attr attribute = xml.createAttribute(name);
236:                attribute.setValue(value);
237:                map.setNamedItem(attribute);
238:            }
239:
240:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.