KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > settings > storage > XMLStorage


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.settings.storage;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import org.openide.filesystems.FileLock;
28
29 import org.openide.filesystems.FileObject;
30 import org.openide.xml.EntityCatalog;
31 import org.openide.xml.XMLUtil;
32 import org.w3c.dom.Document JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.SAXParseException JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37 import org.xml.sax.helpers.DefaultHandler JavaDoc;
38
39
40 public final class XMLStorage {
41
42     private static final Logger JavaDoc LOG = Logger.getLogger(XMLStorage.class.getName());
43     
44     private XMLStorage() {
45         
46     }
47     
48     public static void save(FileObject fo, Document JavaDoc doc) {
49         assert fo != null : "FileObject can't be null"; //NOI18N
50
assert doc != null : "Document can't be null"; //NOI18N
51

52         try {
53             FileLock lock = fo.lock();
54             try {
55                 OutputStream JavaDoc os = fo.getOutputStream(lock);
56                 try {
57                     XMLUtil.write(doc, os, "UTF-8"); //NOI18N
58
} finally {
59                     os.close();
60                 }
61             } finally {
62                 lock.releaseLock();
63             }
64         } catch (IOException JavaDoc ex) {
65             LOG.log(Level.WARNING, "Can't save editor settings to " + fo.getPath(), ex); //NOI18N
66
}
67     }
68     
69     public static Object JavaDoc load(FileObject fo, Handler JavaDoc handler) {
70         assert fo != null : "Settings file must not be null"; //NOI18N
71

72         handler.setProcessedFile(fo);
73         try {
74             XMLReader JavaDoc reader = XMLUtil.createXMLReader(true);
75             reader.setEntityResolver(EntityCatalog.getDefault());
76             reader.setContentHandler(handler);
77             reader.setErrorHandler(handler);
78             
79             InputStream JavaDoc is = fo.getInputStream();
80             try {
81                 reader.parse(new InputSource JavaDoc(is));
82             } finally {
83                 is.close();
84             }
85         } catch (Exception JavaDoc ex) {
86             LOG.log(Level.WARNING, "Invalid or corrupted file: " + fo.getPath(), ex); //NOI18N
87
} finally {
88             handler.setProcessedFile(null);
89         }
90         
91         return handler.getResult();
92     }
93     
94     public static class Handler extends DefaultHandler JavaDoc {
95         private FileObject file;
96         private boolean isModuleFile;
97         
98         private Object JavaDoc result;
99         
100         public void setResult (Object JavaDoc result) {
101             this.result = result;
102         }
103         
104         public Object JavaDoc getResult () {
105             return result;
106         }
107
108         public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
109             log("warning", e); //NOI18N
110
}
111
112         public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
113             log("error", e); //NOI18N
114
}
115
116         public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
117             log("fatal error", e); //NOI18N
118
throw e;
119         }
120
121         private void log(String JavaDoc errorType, SAXParseException JavaDoc e) {
122             if (file == null) {
123                 LOG.log(Level.FINE, "XML parser " + errorType, e); //NOI18N
124
} else {
125                 Level JavaDoc level;
126                 if (isModuleFile()) { //NOI18N
127
level = Level.WARNING; // warnings for module layer supplied files
128
} else {
129                     level = Level.FINE; // user files, can be from previous versions
130
}
131                 
132                 LOG.log(level, "XML parser " + errorType + " in file " + file.getPath(), e); //NOI18N
133
}
134         }
135         
136         protected final FileObject getProcessedFile() {
137             return this.file;
138         }
139         
140         protected final boolean isModuleFile() {
141             return this.isModuleFile;
142         }
143         
144         private void setProcessedFile(FileObject f) {
145             this.file = f;
146             if (this.file != null) {
147                 FileObject parent = this.file.getParent();
148                 this.isModuleFile = parent != null && parent.getNameExt().contains("Default"); //NOI18N
149
} else {
150                 this.isModuleFile = false;
151             }
152         }
153     } // End of Handler class
154
}
155
Popular Tags