KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > persistence > TCGroupParser


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.core.windows.persistence;
21
22 import java.util.logging.Level JavaDoc;
23 import org.netbeans.core.windows.Debug;
24 import org.openide.filesystems.FileLock;
25 import org.openide.filesystems.FileObject;
26 import org.openide.filesystems.FileUtil;
27 import org.openide.modules.SpecificationVersion;
28 import org.openide.util.NbBundle;
29 import org.xml.sax.*;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31
32 import java.io.*;
33 import java.util.logging.Logger JavaDoc;
34
35 /**
36  * Handle loading/saving of TopComponent reference in Group configuration data.
37  *
38  * @author Marek Slama
39  */

40
41 class TCGroupParser {
42     
43     public static final String JavaDoc INSTANCE_DTD_ID_2_0
44     = "-//NetBeans//DTD Top Component in Group Properties 2.0//EN"; // NOI18N
45

46     private static final boolean DEBUG = Debug.isLoggable(TCGroupParser.class);
47     
48     /** Unique id from file name */
49     private String JavaDoc tc_id;
50     
51     /** Module parent folder */
52     private FileObject moduleParentFolder;
53     
54     /** Local parent folder */
55     private FileObject localParentFolder;
56     
57     private InternalConfig internalConfig;
58     
59     /** true if wstcgrp file is present in module folder */
60     private boolean inModuleFolder;
61     /** true if wstcgrp file is present in local folder */
62     private boolean inLocalFolder;
63     
64     public TCGroupParser(String JavaDoc tc_id) {
65         this.tc_id = tc_id;
66     }
67     
68     /** Load tcgroup configuration. */
69     TCGroupConfig load () throws IOException {
70         if (DEBUG) Debug.log(TCGroupParser.class, "load ENTER" + " tcGrp:" + tc_id);
71         TCGroupConfig tcGroupCfg = new TCGroupConfig();
72         PropertyHandler propertyHandler = new PropertyHandler();
73         InternalConfig internalCfg = getInternalConfig();
74         internalCfg.clear();
75         propertyHandler.readData(tcGroupCfg, internalCfg);
76         if (DEBUG) Debug.log(TCGroupParser.class, "load LEAVE" + " tcGrp:" + tc_id);
77         return tcGroupCfg;
78     }
79     
80     /** Save tcGroup configuration. */
81     void save (TCGroupConfig tcGroupCfg) throws IOException {
82         if (DEBUG) Debug.log(TCGroupParser.class, "save ENTER" + " tcGrp:" + tc_id);
83         
84         PropertyHandler propertyHandler = new PropertyHandler();
85         InternalConfig internalCfg = getInternalConfig();
86         propertyHandler.writeData(tcGroupCfg, internalCfg);
87         if (DEBUG) Debug.log(TCGroupParser.class, "save LEAVE" + " tcGrp:" + tc_id);
88     }
89     
90     String JavaDoc getName () {
91         return tc_id;
92     }
93     
94     /** Getter for internal configuration data.
95      * @return instance of internal configuration data
96      */

97     InternalConfig getInternalConfig () {
98         if (internalConfig == null) {
99             internalConfig = new InternalConfig();
100         }
101         return internalConfig;
102     }
103     
104     boolean isInModuleFolder () {
105         return inModuleFolder;
106     }
107     
108     void setInModuleFolder (boolean inModuleFolder) {
109         this.inModuleFolder = inModuleFolder;
110     }
111     
112     boolean isInLocalFolder () {
113         return inLocalFolder;
114     }
115     
116     void setInLocalFolder (boolean inLocalFolder) {
117         this.inLocalFolder = inLocalFolder;
118     }
119     
120     void setModuleParentFolder (FileObject moduleParentFolder) {
121         this.moduleParentFolder = moduleParentFolder;
122     }
123     
124     void setLocalParentFolder (FileObject localParentFolder) {
125         this.localParentFolder = localParentFolder;
126     }
127     
128     void log (String JavaDoc s) {
129         Debug.log(TCGroupParser.class, s);
130     }
131     
132     private final class PropertyHandler extends DefaultHandler JavaDoc {
133         
134         /** tcRef manager configuration data */
135         private TCGroupConfig tcGroupConfig = null;
136         
137         /** internal configuration data */
138         private InternalConfig internalConfig = null;
139         
140         /** Lock to prevent mixing readData and writeData */
141         private final Object JavaDoc RW_LOCK = new Object JavaDoc();
142         
143         public PropertyHandler () {
144         }
145         
146         private FileObject getConfigFOInput () {
147             FileObject tcGroupConfigFO;
148             if (isInLocalFolder()) {
149                 //log("-- TCGroupParser.getConfigFOInput" + " looking for LOCAL");
150
tcGroupConfigFO = localParentFolder.getFileObject
151                 (TCGroupParser.this.getName(), PersistenceManager.TCGROUP_EXT);
152             } else if (isInModuleFolder()) {
153                 //log("-- TCGroupParser.getConfigFOInput" + " looking for MODULE");
154
tcGroupConfigFO = moduleParentFolder.getFileObject
155                 (TCGroupParser.this.getName(), PersistenceManager.TCGROUP_EXT);
156             } else {
157                 //XXX should not happen
158
tcGroupConfigFO = null;
159             }
160             //log("-- TCGroupParser.getConfigFOInput" + " tcGroupConfigFO:" + tcGroupConfigFO);
161
return tcGroupConfigFO;
162         }
163
164         private FileObject getConfigFOOutput () throws IOException {
165             FileObject tcGroupConfigFO;
166             tcGroupConfigFO = localParentFolder.getFileObject
167             (TCGroupParser.this.getName(), PersistenceManager.TCGROUP_EXT);
168             if (tcGroupConfigFO != null) {
169                 //log("-- TCGroupParser.getConfigFOOutput" + " tcGroupConfigFO LOCAL:" + tcGroupConfigFO);
170
return tcGroupConfigFO;
171             } else {
172                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
173                 buffer.append(TCGroupParser.this.getName());
174                 buffer.append('.');
175                 buffer.append(PersistenceManager.TCGROUP_EXT);
176                 //XXX should be improved localParentFolder can be null
177
tcGroupConfigFO = FileUtil.createData(localParentFolder, buffer.toString());
178                 //log("-- TCGroupParser.getConfigFOOutput" + " LOCAL not found CREATE");
179

180                 return tcGroupConfigFO;
181             }
182         }
183         /**
184          Reads tcRef configuration data from XML file.
185          Data are returned in output params.
186          */

187         void readData (TCGroupConfig tcGroupCfg, InternalConfig internalCfg)
188         throws IOException {
189             tcGroupConfig = tcGroupCfg;
190             internalConfig = internalCfg;
191             
192             FileObject cfgFOInput = getConfigFOInput();
193             if (cfgFOInput == null) {
194                 throw new FileNotFoundException("[WinSys] Missing TCGroup configuration file:" // NOI18N
195
+ TCGroupParser.this.getName());
196             }
197             InputStream is = null;
198             try {
199                 synchronized (RW_LOCK) {
200                     //DUMP BEGIN
201
/*InputStream is = cfgFOInput.getInputStream();
202                     byte [] arr = new byte [is.available()];
203                     is.read(arr);
204                     log("DUMP TCGroup: " + TCGroupParser.this.getName());
205                     String s = new String(arr);
206                     log(s);*/

207                     //DUMP END
208
is = cfgFOInput.getInputStream();
209                     PersistenceManager.getDefault().getXMLParser(this).parse(new InputSource(is));
210                 }
211             } catch (SAXException exc) {
212                 // Turn into annotated IOException
213
String JavaDoc msg = NbBundle.getMessage(TCGroupParser.class,
214                                                  "EXC_TCGroupParse", cfgFOInput);
215
216                 throw (IOException) new IOException(msg).initCause(exc);
217             } finally {
218                 try {
219                     if (is != null) {
220                         is.close();
221                     }
222                 } catch (IOException exc) {
223                     Logger.getLogger(TCGroupParser.class.getName()).log(Level.WARNING, null, exc);
224                 }
225             }
226                         
227             tcGroupCfg = tcGroupConfig;
228             internalCfg = internalConfig;
229             
230             tcGroupConfig = null;
231             internalConfig = null;
232         }
233         
234         public void startElement (String JavaDoc nameSpace, String JavaDoc name, String JavaDoc qname, Attributes attrs)
235         throws SAXException {
236             if ("tc-group".equals(qname)) { // NOI18N
237
handleTCGroup(attrs);
238             } else if (internalConfig.specVersion.compareTo(new SpecificationVersion("2.0")) == 0) { // NOI18N
239
//Parse version 2.0
240
if ("module".equals(qname)) { // NOI18N
241
handleModule(attrs);
242                 } else if ("tc-id".equals(qname)) { // NOI18N
243
handleTcId(attrs);
244                 } else if ("open-close-behavior".equals(qname)) { // NOI18N
245
handleOpenCloseBehavior(attrs);
246                 }
247             } else {
248                 log("-- TCGroupParser.startElement PARSING OLD");
249                 //Parse version < 2.0
250
}
251         }
252
253         public void error(SAXParseException ex) throws SAXException {
254             throw ex;
255         }
256
257         /** Reads element "tc-group" */
258         private void handleTCGroup (Attributes attrs) {
259             String JavaDoc version = attrs.getValue("version"); // NOI18N
260
if (version != null) {
261                 internalConfig.specVersion = new SpecificationVersion(version);
262             } else {
263                 PersistenceManager.LOG.log(Level.WARNING,
264                 "[WinSys.TCGroupParser.handleTCGroup]" // NOI18N
265
+ " Warning: Missing attribute \"version\" of element \"tc-group\"."); // NOI18N
266
internalConfig.specVersion = new SpecificationVersion("2.0"); // NOI18N
267
}
268         }
269         
270         /** Reads element "module" and updates mode config content */
271         private void handleModule (Attributes attrs) {
272             String JavaDoc moduleCodeName = attrs.getValue("name"); // NOI18N
273
//Parse code name
274
internalConfig.moduleCodeNameBase = null;
275             internalConfig.moduleCodeNameRelease = null;
276             internalConfig.moduleSpecificationVersion = null;
277             if (moduleCodeName != null) {
278                 int i = moduleCodeName.indexOf('/');
279                 if (i != -1) {
280                     internalConfig.moduleCodeNameBase = moduleCodeName.substring(0, i);
281                     internalConfig.moduleCodeNameRelease = moduleCodeName.substring(i + 1);
282                     checkReleaseCode(internalConfig);
283                 } else {
284                     internalConfig.moduleCodeNameBase = moduleCodeName;
285                 }
286                 internalConfig.moduleSpecificationVersion = attrs.getValue("spec"); // NOI18N
287
}
288         }
289
290         /** Checks validity of <code>moduleCodeNameRelease</code> field.
291          * Helper method. */

292         private void checkReleaseCode (InternalConfig internalConfig) {
293             // #24844. Repair the wrongly saved "null" string
294
// as release number.
295
if("null".equals(internalConfig.moduleCodeNameRelease)) { // NOI18N
296
Logger.getLogger(TCGroupParser.class.getName()).log(Level.WARNING, null,
297                                   new IllegalStateException JavaDoc("Module release code was saved as null string" +
298                                                             " for module " +
299                                                             internalConfig.moduleCodeNameBase +
300                                                             "! Repairing."));
301                 internalConfig.moduleCodeNameRelease = null;
302             }
303         }
304         
305         /** Reads element "tc-id" */
306         private void handleTcId (Attributes attrs) throws SAXException {
307             String JavaDoc tc_id = attrs.getValue("id"); // NOI18N
308
if (tc_id != null) {
309                 tcGroupConfig.tc_id = tc_id;
310                 if (!tc_id.equals(TCGroupParser.this.getName())) {
311                     PersistenceManager.LOG.log(Level.WARNING,
312                     "[WinSys.TCGroupParser.handleTcId]" // NOI18N
313
+ " Error: Value of attribute \"id\" of element \"tc-id\"" // NOI18N
314
+ " and configuration file name must be the same."); // NOI18N
315
throw new SAXException("Invalid attribute value"); // NOI18N
316
}
317             } else {
318                 PersistenceManager.LOG.log(Level.WARNING,
319                 "[WinSys.TCGroupParser.handleTcId]" // NOI18N
320
+ " Error: Missing required attribute \"id\" of element \"tc-id\"."); // NOI18N
321
throw new SAXException("Missing required attribute"); // NOI18N
322
}
323         }
324         
325         /** Reads element "open-close-behavior" */
326         private void handleOpenCloseBehavior (Attributes attrs) throws SAXException {
327             String JavaDoc open = attrs.getValue("open"); // NOI18N;
328
if (open != null) {
329                 if ("true".equals(open)) { // NOI18N
330
tcGroupConfig.open = true;
331                 } else if ("false".equals(open)) { // NOI18N
332
tcGroupConfig.open = false;
333                 } else {
334                     PersistenceManager.LOG.log(Level.WARNING,
335                     "[WinSys.TCGroupParser.handleOpenCloseBehavior]" // NOI18N
336
+ " Warning: Invalid value of attribute \"open\"" // NOI18N
337
+ " of element \"open-close-behavior\"."); // NOI18N
338
tcGroupConfig.open = false;
339                 }
340             } else {
341                 PersistenceManager.LOG.log(Level.WARNING,
342                 "[WinSys.TCGroupParser.handleOpenCloseBehavior]" // NOI18N
343
+ " Warning: Missing required attribute \"open\"" // NOI18N
344
+ " of element \"open-close-behavior\"."); // NOI18N
345
tcGroupConfig.open = false;
346             }
347             
348             String JavaDoc close = attrs.getValue("close"); // NOI18N;
349
if (close != null) {
350                 if ("true".equals(close)) { // NOI18N
351
tcGroupConfig.close = true;
352                 } else if ("false".equals(close)) { // NOI18N
353
tcGroupConfig.close = false;
354                 } else {
355                     PersistenceManager.LOG.log(Level.WARNING,
356                     "[WinSys.TCGroupParser.handleOpenCloseBehavior]" // NOI18N
357
+ " Warning: Invalid value of attribute \"close\"" // NOI18N
358
+ " of element \"open-close-behavior\"."); // NOI18N
359
tcGroupConfig.close = false;
360                 }
361             } else {
362                 PersistenceManager.LOG.log(Level.WARNING,
363                 "[WinSys.TCGroupParser.handleOpenCloseBehavior]" // NOI18N
364
+ " Warning: Missing required attribute \"close\"" // NOI18N
365
+ " of element \"open-close-behavior\"."); // NOI18N
366
tcGroupConfig.close = false;
367             }
368             
369             String JavaDoc wasOpened = attrs.getValue("was-opened"); // NOI18N;
370
if (wasOpened != null) {
371                 if ("true".equals(wasOpened)) { // NOI18N
372
tcGroupConfig.wasOpened = true;
373                 } else if ("false".equals(wasOpened)) { // NOI18N
374
tcGroupConfig.wasOpened = false;
375                 } else {
376                     PersistenceManager.LOG.log(Level.WARNING,
377                     "[WinSys.TCGroupParser.handleOpenCloseBehavior]" // NOI18N
378
+ " Warning: Invalid value of attribute \"was-opened\"" // NOI18N
379
+ " of element \"open-close-behavior\"."); // NOI18N
380
tcGroupConfig.wasOpened = false;
381                 }
382             } else {
383                 tcGroupConfig.wasOpened = false;
384             }
385         }
386         
387         /** Writes data from asociated tcRef to the xml representation */
388         void writeData (TCGroupConfig tcGroupCfg, InternalConfig ic) throws IOException {
389             final StringBuffer JavaDoc buff = fillBuffer(tcGroupCfg, ic);
390             synchronized (RW_LOCK) {
391                 FileObject cfgFOOutput = getConfigFOOutput();
392                 FileLock lock = null;
393                 OutputStream os = null;
394                 OutputStreamWriter osw = null;
395                 try {
396                     lock = cfgFOOutput.lock();
397                     os = cfgFOOutput.getOutputStream(lock);
398                     osw = new OutputStreamWriter(os, "UTF-8"); // NOI18N
399
osw.write(buff.toString());
400                     //log("DUMP TCGroup: " + TCGroupParser.this.getName());
401
//log(buff.toString());
402
} finally {
403                     try {
404                         if (osw != null) {
405                             osw.close();
406                         }
407                     } catch (IOException exc) {
408                         Logger.getLogger(TCGroupParser.class.getName()).log(Level.WARNING, null, exc);
409                     }
410                     if (lock != null) {
411                         lock.releaseLock();
412                     }
413                 }
414             }
415         }
416         
417         /** Returns xml content in StringBuffer
418          */

419         private StringBuffer JavaDoc fillBuffer (TCGroupConfig tcGroupCfg, InternalConfig ic) throws IOException {
420             StringBuffer JavaDoc buff = new StringBuffer JavaDoc(800);
421             String JavaDoc curValue = null;
422             // header
423
buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"). // NOI18N
424
/*buff.append("<!DOCTYPE tc-group PUBLIC\n"); // NOI18N
425             buff.append(" \"-//NetBeans//DTD Top Component in Group Properties 2.0//EN\"\n"); // NOI18N
426             buff.append(" \"http://www.netbeans.org/dtds/tc-group2_0.dtd\">\n\n"); // NOI18N*/

427                 append("<tc-group version=\"2.0\">\n"); // NOI18N
428

429             appendModule(ic, buff);
430             appendTcId(tcGroupCfg, buff);
431             appendOpenCloseBehavior(tcGroupCfg, buff);
432             
433             buff.append("</tc-group>\n"); // NOI18N
434
return buff;
435         }
436         
437         private void appendModule (InternalConfig ic, StringBuffer JavaDoc buff) {
438             if (ic == null) {
439                 return;
440             }
441             if (ic.moduleCodeNameBase != null) {
442                 buff.append(" <module name=\""); // NOI18N
443
buff.append(ic.moduleCodeNameBase);
444                 if (ic.moduleCodeNameRelease != null) {
445                     buff.append("/").append(ic.moduleCodeNameRelease); // NOI18N
446
}
447                 if (ic.moduleSpecificationVersion != null) {
448                     buff.append("\" spec=\""); // NOI18N
449
buff.append(ic.moduleSpecificationVersion);
450                 }
451                 buff.append("\" />\n"); // NOI18N
452
}
453         }
454
455         private void appendTcId (TCGroupConfig tcGroupCfg, StringBuffer JavaDoc buff) {
456             buff.append(" <tc-id id=\"").append(
457                     PersistenceManager.escapeTcId4XmlContent(tcGroupCfg.tc_id)).
458                     append("\"/>\n"); // NOI18N
459
}
460         
461         private void appendOpenCloseBehavior (TCGroupConfig tcGroupCfg, StringBuffer JavaDoc buff) {
462             buff.append(" <open-close-behavior open=\"").append(tcGroupCfg.open). // NOI18N
463
append("\" close=\"").append(tcGroupCfg.close). // NOI18N
464
append("\" was-opened=\"").append(tcGroupCfg.wasOpened).append("\"/>\n"); // NOI18N
465
}
466         
467     }
468     
469 }
470
Popular Tags