KickJava   Java API By Example, From Geeks To Geeks.

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


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.ModuleInfo;
28 import org.openide.modules.SpecificationVersion;
29 import org.openide.util.NbBundle;
30 import org.xml.sax.*;
31 import org.xml.sax.helpers.DefaultHandler JavaDoc;
32
33 import java.io.*;
34 import java.util.*;
35 import java.util.logging.Logger JavaDoc;
36
37 /**
38  * Handle loading/saving of Group configuration data.
39  *
40  * @author Marek Slama
41  */

42
43 class GroupParser {
44     
45     public static final String JavaDoc INSTANCE_DTD_ID_2_0
46         = "-//NetBeans//DTD Group Properties 2.0//EN"; // NOI18N
47

48     private static final boolean DEBUG = Debug.isLoggable(GroupParser.class);
49     
50     /** Module parent folder */
51     private FileObject moduleParentFolder;
52     
53     /** Local parent folder */
54     private FileObject localParentFolder;
55     
56     private InternalConfig internalConfig;
57     
58     private Map<String JavaDoc, TCGroupParser> tcGroupParserMap = new HashMap<String JavaDoc, TCGroupParser>(19);
59     
60     /** Unique group name from file name */
61     private String JavaDoc groupName;
62     
63     /** true if wsgrp file is present in module folder */
64     private boolean inModuleFolder;
65     /** true if wsgrp file is present in local folder */
66     private boolean inLocalFolder;
67     
68     public GroupParser(String JavaDoc name) {
69         this.groupName = name;
70     }
71     
72     /** Load group configuration including all tcgrp's. */
73     GroupConfig load () throws IOException {
74         //if (DEBUG) Debug.log(GroupParser.class, "");
75
//if (DEBUG) Debug.log(GroupParser.class, "++ GroupParser.load ENTER" + " group:" + name);
76
GroupConfig sc = new GroupConfig();
77         readProperties(sc);
78         readTCGroups(sc);
79         //if (DEBUG) Debug.log(GroupParser.class, "++ GroupParser.load LEAVE" + " group:" + name);
80
//if (DEBUG) Debug.log(GroupParser.class, "");
81
return sc;
82     }
83     
84     /** Save group configuration including all tcgrp's. */
85     void save (GroupConfig sc) throws IOException {
86         //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.save ENTER" + " group:" + name);
87
writeProperties(sc);
88         writeTCGroups(sc);
89         //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.save LEAVE" + " group:" + name);
90
}
91     
92     private void readProperties (GroupConfig sc) throws IOException {
93         if (DEBUG) Debug.log(GroupParser.class, "readProperties ENTER" + " group:" + getName());
94         PropertyHandler propertyHandler = new PropertyHandler();
95         InternalConfig internalCfg = getInternalConfig();
96         internalCfg.clear();
97         propertyHandler.readData(sc, internalCfg);
98         
99         /*if (DEBUG) Debug.log(GroupParser.class, " specVersion: " + internalCfg.specVersion);
100         if (DEBUG) Debug.log(GroupParser.class, " moduleCodeNameBase: " + internalCfg.moduleCodeNameBase);
101         if (DEBUG) Debug.log(GroupParser.class, " moduleCodeNameRelease: " + internalCfg.moduleCodeNameRelease);
102         if (DEBUG) Debug.log(GroupParser.class, "moduleSpecificationVersion: " + internalCfg.moduleSpecificationVersion);*/

103         if (DEBUG) Debug.log(GroupParser.class, "readProperties LEAVE" + " group:" + getName());
104     }
105     
106     private void readTCGroups (GroupConfig sc) throws IOException {
107         if (DEBUG) Debug.log(GroupParser.class, "readTCGroups ENTER" + " group:" + getName());
108         
109         for (Iterator it = tcGroupParserMap.keySet().iterator(); it.hasNext(); ) {
110             TCGroupParser tcGroupParser = (TCGroupParser) tcGroupParserMap.get(it.next());
111             tcGroupParser.setInModuleFolder(false);
112             tcGroupParser.setInLocalFolder(false);
113         }
114         
115         /*if (DEBUG) Debug.log(GroupParser.class, "moduleParentFolder: " + moduleParentFolder);
116         if (DEBUG) Debug.log(GroupParser.class, " localParentFolder: " + localParentFolder);
117         if (DEBUG) Debug.log(GroupParser.class, " moduleGroupFolder: " + moduleGroupFolder);
118         if (DEBUG) Debug.log(GroupParser.class, " localGroupFolder: " + localGroupFolder);*/

119         
120         if (isInModuleFolder()) {
121             FileObject moduleGroupFolder = moduleParentFolder.getFileObject(groupName);
122             if (moduleGroupFolder != null) {
123                 FileObject [] files = moduleGroupFolder.getChildren();
124                 for (int i = 0; i < files.length; i++) {
125                     //if (DEBUG) Debug.log(GroupParser.class, "-- MODULE fo[" + i + "]: " + files[i]);
126
if (!files[i].isFolder() && PersistenceManager.TCGROUP_EXT.equals(files[i].getExt())) {
127                         //wstcgrp file
128
TCGroupParser tcGroupParser = (TCGroupParser) tcGroupParserMap.get(files[i].getName());
129                         if (tcGroupParser == null) {
130                             tcGroupParser = new TCGroupParser(files[i].getName());
131                             tcGroupParserMap.put(files[i].getName(), tcGroupParser);
132                         }
133                         tcGroupParser.setInModuleFolder(true);
134                         tcGroupParser.setModuleParentFolder(moduleGroupFolder);
135                     }
136                 }
137             }
138         }
139
140         if (isInLocalFolder()) {
141             FileObject localGroupFolder = localParentFolder.getFileObject(groupName);
142             if (localGroupFolder != null) {
143                 FileObject [] files = localGroupFolder.getChildren();
144                 for (int i = 0; i < files.length; i++) {
145                     //if (DEBUG) Debug.log(GroupParser.class, "-- LOCAL fo[" + i + "]: " + files[i]);
146
if (!files[i].isFolder() && PersistenceManager.TCGROUP_EXT.equals(files[i].getExt())) {
147                         //wstcgrp file
148
TCGroupParser tcGroupParser;
149                         if (tcGroupParserMap.containsKey(files[i].getName())) {
150                             tcGroupParser = (TCGroupParser) tcGroupParserMap.get(files[i].getName());
151                         } else {
152                             tcGroupParser = new TCGroupParser(files[i].getName());
153                             tcGroupParserMap.put(files[i].getName(), tcGroupParser);
154                         }
155                         tcGroupParser.setInLocalFolder(true);
156                         tcGroupParser.setLocalParentFolder(localGroupFolder);
157                     }
158                 }
159             }
160         }
161         
162         /*for (Iterator it = tcGroupParserMap.keySet().iterator(); it.hasNext(); ) {
163             TCGroupParser tcGroupParser = (TCGroupParser) tcGroupParserMap.get(it.next());
164             if (DEBUG) Debug.log(GroupParser.class, "tcGroupParser: " + tcGroupParser.getName()
165             + " isInModuleFolder:" + tcGroupParser.isInModuleFolder()
166             + " isInLocalFolder:" + tcGroupParser.isInLocalFolder());
167         }*/

168         
169         //Check if corresponding module is present and enabled.
170
//We must load configuration data first because module info is stored in XML.
171
List<TCGroupConfig> tcGroupCfgList = new ArrayList<TCGroupConfig>(tcGroupParserMap.size());
172         List<TCGroupParser> toRemove = new ArrayList<TCGroupParser>(tcGroupParserMap.size());
173         for (Iterator it = tcGroupParserMap.keySet().iterator(); it.hasNext(); ) {
174             TCGroupParser tcGroupParser = (TCGroupParser) tcGroupParserMap.get(it.next());
175             TCGroupConfig tcGroupCfg;
176             try {
177                 tcGroupCfg = tcGroupParser.load();
178             } catch (IOException exc) {
179                 //If reading of one tcGroup fails we want to log message
180
//and continue.
181
// see #45497 - if something fails to load, remove it from local config..
182
toRemove.add(tcGroupParser);
183                 deleteLocalTCGroup(tcGroupParser.getName());
184                 Logger.getLogger(GroupParser.class.getName()).log(Level.WARNING, null, exc);
185                 continue;
186             }
187             boolean tcGroupAccepted = acceptTCGroup(tcGroupParser, tcGroupCfg);
188             if (tcGroupAccepted) {
189                 tcGroupCfgList.add(tcGroupCfg);
190             } else {
191                 toRemove.add(tcGroupParser);
192                 deleteLocalTCGroup(tcGroupParser.getName());
193             }
194         }
195         for (int i = 0; i < toRemove.size(); i++) {
196             TCGroupParser tcGroupParser = (TCGroupParser) toRemove.get(i);
197             tcGroupParserMap.remove(tcGroupParser.getName());
198         }
199         
200         sc.tcGroupConfigs = (TCGroupConfig [])
201             // safer array initialization, making sure the size of array matches size of list
202
// see #45497
203
tcGroupCfgList.toArray(new TCGroupConfig[tcGroupCfgList.size()]);
204         
205         PersistenceManager pm = PersistenceManager.getDefault();
206         for (int i = 0; i < sc.tcGroupConfigs.length; i++) {
207             pm.addUsedTCId(sc.tcGroupConfigs[i].tc_id);
208         }
209         
210         if (DEBUG) Debug.log(GroupParser.class, "readTCGroups LEAVE" + " group:" + getName());
211     }
212     
213     /** Checks if module for given tcGroup exists.
214      * @return true if tcGroup is valid - its module exists
215      */

216     private boolean acceptTCGroup (TCGroupParser tcGroupParser, TCGroupConfig config) {
217         InternalConfig cfg = tcGroupParser.getInternalConfig();
218         //Check module info
219
if (cfg.moduleCodeNameBase != null) {
220             ModuleInfo curModuleInfo = PersistenceManager.findModule
221                                         (cfg.moduleCodeNameBase, cfg.moduleCodeNameRelease,
222                                          cfg.moduleSpecificationVersion);
223             if (curModuleInfo == null) {
224                 PersistenceManager.LOG.fine("Cannot find module \'" +
225                           cfg.moduleCodeNameBase + " " + cfg.moduleCodeNameRelease + " " +
226                           cfg.moduleSpecificationVersion + "\' for tcgrp with name \'" + config.tc_id + "\'"); // NOI18N
227
}
228             if ((curModuleInfo != null) && curModuleInfo.isEnabled()) {
229                 //Module is present and is enabled
230
return true;
231             } else {
232                 //Module is NOT present (it could be deleted offline)
233
//or is NOT enabled
234
return false;
235             }
236         } else {
237             //No module info
238
return true;
239         }
240     }
241     
242     private void writeProperties (GroupConfig sc) throws IOException {
243         if (DEBUG) Debug.log(GroupParser.class, "writeProperties ENTER" + " group:" + getName());
244         PropertyHandler propertyHandler = new PropertyHandler();
245         InternalConfig internalCfg = getInternalConfig();
246         propertyHandler.writeData(sc, internalCfg);
247         if (DEBUG) Debug.log(GroupParser.class, "writeProperties LEAVE" + " group:" + getName());
248     }
249     
250     private void writeTCGroups (GroupConfig sc) throws IOException {
251         if (DEBUG) Debug.log(GroupParser.class, "writeTCGroups ENTER" + " group:" + getName());
252         //Step 1: Clean obsolete tcGroup parsers
253
Map<String JavaDoc, TCGroupConfig> tcGroupConfigMap = new HashMap<String JavaDoc, TCGroupConfig>(19);
254         for (int i = 0; i < sc.tcGroupConfigs.length; i++) {
255             tcGroupConfigMap.put(sc.tcGroupConfigs[i].tc_id, sc.tcGroupConfigs[i]);
256         }
257         List<String JavaDoc> toDelete = new ArrayList<String JavaDoc>(10);
258         for (String JavaDoc s: tcGroupParserMap.keySet()) {
259             TCGroupParser tcGroupParser = tcGroupParserMap.get(s);
260             if (!tcGroupConfigMap.containsKey(tcGroupParser.getName())) {
261                 toDelete.add(tcGroupParser.getName());
262             }
263         }
264         for (int i = 0; i < toDelete.size(); i++) {
265             /*if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.writeTCGroups"
266             + " ** REMOVE FROM MAP tcGroupParser: " + toDelete.get(i));*/

267             tcGroupParserMap.remove(toDelete.get(i));
268             /*if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.writeTCGroups"
269             + " ** DELETE tcGroupParser: " + toDelete.get(i));*/

270             deleteLocalTCGroup(toDelete.get(i));
271         }
272         
273         //Step 2: Create missing tcGoup parsers
274
for (int i = 0; i < sc.tcGroupConfigs.length; i++) {
275             if (!tcGroupParserMap.containsKey(sc.tcGroupConfigs[i].tc_id)) {
276                 TCGroupParser tcGroupParser = new TCGroupParser(sc.tcGroupConfigs[i].tc_id);
277                 tcGroupParserMap.put(sc.tcGroupConfigs[i].tc_id, tcGroupParser);
278             }
279         }
280         
281         //Step 3: Save all groups
282
FileObject localFolder = localParentFolder.getFileObject(getName());
283         if ((localFolder == null) && (tcGroupParserMap.size() > 0)) {
284             //Create local group folder
285
//if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.writeTCGroups" + " CREATE LOCAL FOLDER");
286
localFolder = FileUtil.createFolder(localParentFolder, getName());
287         }
288         //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.writeTCGroups" + " localFolder:" + localFolder);
289

290         for (Iterator it = tcGroupParserMap.keySet().iterator(); it.hasNext(); ) {
291             TCGroupParser tcGroupParser = (TCGroupParser) tcGroupParserMap.get(it.next());
292             tcGroupParser.setLocalParentFolder(localFolder);
293             tcGroupParser.setInLocalFolder(true);
294             tcGroupParser.save((TCGroupConfig) tcGroupConfigMap.get(tcGroupParser.getName()));
295         }
296         
297         if (DEBUG) Debug.log(GroupParser.class, "writeTCGroups LEAVE" + " group:" + getName());
298     }
299     
300     private void deleteLocalTCGroup (String JavaDoc tcGroupName) {
301         if (DEBUG) Debug.log(GroupParser.class, "deleteLocalTCGroup" + " group:" + tcGroupName);
302         if (localParentFolder == null) {
303             return;
304         }
305         FileObject localGroupFolder = localParentFolder.getFileObject(groupName);
306         if (localGroupFolder == null) {
307             return;
308         }
309         FileObject tcGroupFO = localGroupFolder.getFileObject(tcGroupName, PersistenceManager.TCGROUP_EXT);
310         if (tcGroupFO != null) {
311             PersistenceManager.deleteOneFO(tcGroupFO);
312         }
313     }
314     
315     /** Removes TCGroupParser from GroupParser and cleans wstcgrp file from local folder.
316      * @param tcGroupName unique name of tcgroup
317      */

318     void removeTCGroup (String JavaDoc tcGroupName) {
319         //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.removeTCGroup" + " group:" + getName() + " tcGroup:" + tcGroupName);
320
tcGroupParserMap.remove(tcGroupName);
321         deleteLocalTCGroup(tcGroupName);
322     }
323     
324     /** Adds TCGroupParser to GroupParser.
325      * @param tcGroupName unique name of tcGroup
326      */

327     TCGroupConfig addTCGroup (String JavaDoc tcGroupName) {
328         //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.addTCGroup" + " group:" + getName() + " tcGroup:" + tcGroupName);
329
//Check consistency. TCGroupParser instance should not exist.
330
TCGroupParser tcGroupParser = (TCGroupParser) tcGroupParserMap.get(tcGroupName);
331         if (tcGroupParser != null) {
332             PersistenceManager.LOG.log(Level.WARNING,
333             "[WinSys.GroupParser.addTCGroup]" // NOI18N
334
+ " Warning: GroupParser " + getName() + ". TCGroupParser " // NOI18N
335
+ tcGroupName + " exists but it should not."); // NOI18N
336
tcGroupParserMap.remove(tcGroupName);
337         }
338         tcGroupParser = new TCGroupParser(tcGroupName);
339         FileObject moduleFolder = moduleParentFolder.getFileObject(groupName);
340         tcGroupParser.setModuleParentFolder(moduleFolder);
341         tcGroupParser.setInModuleFolder(true);
342         tcGroupParserMap.put(tcGroupName, tcGroupParser);
343         TCGroupConfig tcGroupConfig = null;
344         try {
345             tcGroupConfig = tcGroupParser.load();
346         } catch (IOException exc) {
347             PersistenceManager.LOG.log(Level.WARNING,
348             "[WinSys.GroupParser.addTCGroup]" // NOI18N
349
+ " Warning: GroupParser " + getName() + ". Cannot load tcGroup " + tcGroupName, exc); // NOI18N
350
}
351         return tcGroupConfig;
352     }
353     
354     /** Getter for internal configuration data.
355      * @return instance of internal configuration data
356      */

357     InternalConfig getInternalConfig () {
358         if (internalConfig == null) {
359             internalConfig = new InternalConfig();
360         }
361         return internalConfig;
362     }
363     
364     void setModuleParentFolder (FileObject moduleParentFolder) {
365         this.moduleParentFolder = moduleParentFolder;
366     }
367     
368     void setLocalParentFolder (FileObject localParentFolder) {
369         this.localParentFolder = localParentFolder;
370     }
371     
372     String JavaDoc getName () {
373         return groupName;
374     }
375     
376     boolean isInModuleFolder () {
377         return inModuleFolder;
378     }
379     
380     void setInModuleFolder (boolean inModuleFolder) {
381         this.inModuleFolder = inModuleFolder;
382     }
383     
384     boolean isInLocalFolder () {
385         return inLocalFolder;
386     }
387     
388     void setInLocalFolder (boolean inLocalFolder) {
389         this.inLocalFolder = inLocalFolder;
390     }
391     
392     private final class PropertyHandler extends DefaultHandler JavaDoc {
393         
394         /** Group configuration data */
395         private GroupConfig groupConfig = null;
396         
397         /** Internal configuration data */
398         private InternalConfig internalConfig = null;
399         
400         /** Lock to prevent mixing readData and writeData */
401         private final Object JavaDoc RW_LOCK = new Object JavaDoc();
402         
403         public PropertyHandler () {
404         }
405         
406         private FileObject getConfigFOInput () {
407             FileObject groupConfigFO;
408             if (isInLocalFolder()) {
409                 //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.getConfigFOInput" + " looking for LOCAL");
410
groupConfigFO = localParentFolder.getFileObject
411                 (GroupParser.this.getName(), PersistenceManager.GROUP_EXT);
412             } else if (isInModuleFolder()) {
413                 //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.getConfigFOInput" + " looking for MODULE");
414
groupConfigFO = moduleParentFolder.getFileObject
415                 (GroupParser.this.getName(), PersistenceManager.GROUP_EXT);
416             } else {
417                 //XXX should not happen
418
groupConfigFO = null;
419             }
420             //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.getConfigFOInput" + " groupConfigFO:" + groupConfigFO);
421
return groupConfigFO;
422         }
423
424         private FileObject getConfigFOOutput () throws IOException {
425             FileObject groupConfigFO;
426             groupConfigFO = localParentFolder.getFileObject
427             (GroupParser.this.getName(), PersistenceManager.GROUP_EXT);
428             if (groupConfigFO != null) {
429                 //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.getConfigFOOutput" + " groupConfigFO LOCAL:" + groupConfigFO);
430
return groupConfigFO;
431             } else {
432                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
433                 buffer.append(GroupParser.this.getName());
434                 buffer.append('.');
435                 buffer.append(PersistenceManager.GROUP_EXT);
436                 //XXX should be improved localParentFolder can be null
437
groupConfigFO = FileUtil.createData(localParentFolder, buffer.toString());
438                 //if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.getConfigFOOutput" + " LOCAL not found CREATE");
439

440                 return groupConfigFO;
441             }
442         }
443         
444         /**
445          Reads group configuration data from XML file.
446          Data are returned in output params.
447          */

448         void readData (GroupConfig groupCfg, InternalConfig internalCfg)
449         throws IOException {
450             groupConfig = groupCfg;
451             internalConfig = internalCfg;
452             
453             FileObject cfgFOInput = getConfigFOInput();
454             if (cfgFOInput == null) {
455                 throw new FileNotFoundException("[WinSys] Missing Group configuration file:" // NOI18N
456
+ GroupParser.this.getName());
457             }
458             InputStream is = null;
459             try {
460                 synchronized (RW_LOCK) {
461                     //DUMP BEGIN
462
/*InputStream is = cfgFOInput.getInputStream();
463                     byte [] arr = new byte [is.available()];
464                     is.read(arr);
465                     if (DEBUG) Debug.log(GroupParser.class, "DUMP Group: " + GroupParser.this.getName());
466                     String s = new String(arr);
467                     if (DEBUG) Debug.log(GroupParser.class, s);*/

468                     //DUMP END
469
is = cfgFOInput.getInputStream();
470                     PersistenceManager.getDefault().getXMLParser(this).parse(new InputSource(is));
471                 }
472             } catch (SAXException exc) {
473                 // Turn into annotated IOException
474
String JavaDoc msg = NbBundle.getMessage(GroupParser.class,
475                                                  "EXC_GroupParse", cfgFOInput);
476
477                 throw (IOException) new IOException(msg).initCause(exc);
478             } finally {
479                 try {
480                     if (is != null) {
481                         is.close();
482                     }
483                 } catch (IOException exc) {
484                     Logger.getLogger(GroupParser.class.getName()).log(Level.WARNING, null, exc);
485                 }
486             }
487             
488             groupCfg = groupConfig;
489             internalCfg = internalConfig;
490             
491             groupConfig = null;
492             internalConfig = null;
493         }
494         
495         public void startElement (String JavaDoc nameSpace, String JavaDoc name, String JavaDoc qname, Attributes attrs) throws SAXException {
496             if ("group".equals(qname)) { // NOI18N
497
handleGroup(attrs);
498             } else if (internalConfig.specVersion.compareTo(new SpecificationVersion("2.0")) == 0) { // NOI18N
499
//Parse version 2.0
500
if ("module".equals(qname)) { // NOI18N
501
handleModule(attrs);
502                 } else if ("name".equals(qname)) { // NOI18N
503
handleName(attrs);
504                 } else if ("state".equals(qname)) { // NOI18N
505
handleState(attrs);
506                 }
507             } else {
508                 if (DEBUG) Debug.log(GroupParser.class, "-- GroupParser.startElement PARSING OLD");
509                 //Parse version < 2.0
510
}
511         }
512         
513         public void error(SAXParseException ex) throws SAXException {
514             throw ex;
515         }
516         
517         /** Reads element "group" */
518         private void handleGroup (Attributes attrs) {
519             String JavaDoc version = attrs.getValue("version"); // NOI18N
520
if (version != null) {
521                 internalConfig.specVersion = new SpecificationVersion(version);
522             } else {
523                 PersistenceManager.LOG.log(Level.WARNING,
524                 "[WinSys.GroupParser.handleGroup]" // NOI18N
525
+ " Warning: Missing attribute \"version\" of element \"group\"."); // NOI18N
526
internalConfig.specVersion = new SpecificationVersion("2.0"); // NOI18N
527
}
528         }
529         
530         /** Reads element "module" and updates mode config content */
531         private void handleModule (Attributes attrs) {
532             String JavaDoc moduleCodeName = attrs.getValue("name"); // NOI18N
533
//Parse code name
534
internalConfig.moduleCodeNameBase = null;
535             internalConfig.moduleCodeNameRelease = null;
536             internalConfig.moduleSpecificationVersion = null;
537             if (moduleCodeName != null) {
538                 int i = moduleCodeName.indexOf('/');
539                 if (i != -1) {
540                     internalConfig.moduleCodeNameBase = moduleCodeName.substring(0, i);
541                     internalConfig.moduleCodeNameRelease = moduleCodeName.substring(i + 1);
542                     checkReleaseCode(internalConfig);
543                 } else {
544                     internalConfig.moduleCodeNameBase = moduleCodeName;
545                 }
546                 internalConfig.moduleSpecificationVersion = attrs.getValue("spec"); // NOI18N
547
}
548         }
549
550         /** Checks validity of <code>moduleCodeNameRelease</code> field.
551          * Helper method. */

552         private void checkReleaseCode (InternalConfig internalConfig) {
553             // #24844. Repair the wrongly saved "null" string
554
// as release number.
555
if("null".equals(internalConfig.moduleCodeNameRelease)) { // NOI18N
556
Logger.getLogger(GroupParser.class.getName()).log(Level.WARNING, null,
557                                   new IllegalStateException JavaDoc("Module release code was saved as null string" +
558                                                             " for module " +
559                                                             internalConfig.moduleCodeNameBase +
560                                                             "! Repairing."));
561                 internalConfig.moduleCodeNameRelease = null;
562             }
563         }
564         
565         /** Reads element "name" */
566         private void handleName (Attributes attrs) throws SAXException {
567             String JavaDoc name = attrs.getValue("unique"); // NOI18N
568
if (name != null) {
569                 groupConfig.name = name;
570                 if (!name.equals(GroupParser.this.getName())) {
571                     PersistenceManager.LOG.log(Level.WARNING,
572                     "[WinSys.GroupParser.handleName]" // NOI18N
573
+ " Error: Value of attribute \"unique\" of element \"name\"" // NOI18N
574
+ " and configuration file name must be the same."); // NOI18N
575
throw new SAXException("Invalid attribute value"); // NOI18N
576
}
577             } else {
578                 PersistenceManager.LOG.log(Level.WARNING,
579                 "[WinSys.GroupParser.handleName]" // NOI18N
580
+ " Error: Missing required attribute \"unique\" of element \"name\"."); // NOI18N
581
throw new SAXException("Missing required attribute"); // NOI18N
582
}
583         }
584         
585         /** Reads element "state" */
586         private void handleState (Attributes attrs) throws SAXException {
587             String JavaDoc opened = attrs.getValue("opened"); // NOI18N
588
if (opened != null) {
589                 if ("true".equals(opened)) { // NOI18N
590
groupConfig.opened = true;
591                 } else if ("false".equals(opened)) { // NOI18N
592
groupConfig.opened = false;
593                 } else {
594                     PersistenceManager.LOG.log(Level.WARNING,
595                     "[WinSys.GroupParser.handleState]" // NOI18N
596
+ " Warning: Invalid value of attribute \"opened\" of element \"state\"."); // NOI18N
597
groupConfig.opened = false;
598                 }
599             } else {
600                  PersistenceManager.LOG.log(Level.WARNING,
601                 "[WinSys.GroupParser.handleState]" // NOI18N
602
+ " Error: Missing required attribute \"opened\" of element \"state\"."); // NOI18N
603
groupConfig.opened = false;
604             }
605         }
606         
607         /** Writes data from asociated group to the xml representation */
608         void writeData (GroupConfig sc, InternalConfig ic) throws IOException {
609             final StringBuffer JavaDoc buff = fillBuffer(sc, ic);
610             synchronized (RW_LOCK) {
611                 FileObject cfgFOOutput = getConfigFOOutput();
612                 FileLock lock = null;
613                 OutputStream os = null;
614                 OutputStreamWriter osw = null;
615                 try {
616                     lock = cfgFOOutput.lock();
617                     os = cfgFOOutput.getOutputStream(lock);
618                     osw = new OutputStreamWriter(os, "UTF-8"); // NOI18N
619
osw.write(buff.toString());
620                     //if (DEBUG) Debug.log(GroupParser.class, "-- DUMP Group: " + GroupParser.this.getName());
621
//if (DEBUG) Debug.log(GroupParser.class, buff.toString());
622
} finally {
623                     try {
624                         if (osw != null) {
625                             osw.close();
626                         }
627                     } catch (IOException exc) {
628                         Logger.getLogger(GroupParser.class.getName()).log(Level.WARNING, null, exc);
629                     }
630                     if (lock != null) {
631                         lock.releaseLock();
632                     }
633                 }
634             }
635         }
636         
637         /** Returns xml content in StringBuffer
638          */

639         private StringBuffer JavaDoc fillBuffer (GroupConfig gc, InternalConfig ic) throws IOException {
640             StringBuffer JavaDoc buff = new StringBuffer JavaDoc(800);
641             // header
642
buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"); // NOI18N
643
/*buff.append("<!DOCTYPE group PUBLIC\n"); // NOI18N
644             buff.append(" \"-//NetBeans//DTD Group Properties 2.0//EN\"\n"); // NOI18N
645             buff.append(" \"http://www.netbeans.org/dtds/group-properties2_0.dtd\">\n\n"); // NOI18N*/

646             buff.append("<group version=\"2.0\">\n"); // NOI18N
647

648             appendModule(ic, buff);
649             appendName(gc, buff);
650             appendState(gc, buff);
651             
652             buff.append("</group>\n"); // NOI18N
653
return buff;
654         }
655         
656         private void appendModule (InternalConfig ic, StringBuffer JavaDoc buff) {
657             if (ic == null) {
658                 return;
659             }
660             if (ic.moduleCodeNameBase != null) {
661                 buff.append(" <module"); // NOI18N
662
buff.append(" name=\""); // NOI18N
663
buff.append(ic.moduleCodeNameBase);
664                 if (ic.moduleCodeNameRelease != null) {
665                     buff.append("/" + ic.moduleCodeNameRelease); // NOI18N
666
}
667                 if (ic.moduleSpecificationVersion != null) {
668                     buff.append("\" spec=\""); // NOI18N
669
buff.append(ic.moduleSpecificationVersion);
670                 }
671                 buff.append("\" />\n"); // NOI18N
672
}
673         }
674
675         private void appendName (GroupConfig gc, StringBuffer JavaDoc buff) {
676             buff.append(" <name"); // NOI18N
677
buff.append(" unique=\""); // NOI18N
678
buff.append(gc.name);
679             buff.append("\""); // NOI18N
680
buff.append(" />\n"); // NOI18N
681
}
682         
683         private void appendState (GroupConfig gc, StringBuffer JavaDoc buff) {
684             buff.append(" <state"); // NOI18N
685
buff.append(" opened=\""); // NOI18N
686
if (gc.opened) {
687                 buff.append("true"); // NOI18N
688
} else {
689                 buff.append("false"); // NOI18N
690
}
691             buff.append("\""); // NOI18N
692
buff.append(" />\n"); // NOI18N
693
}
694         
695         /** Implementation of entity resolver. Points to the local DTD
696          * for our public ID */

697         public InputSource resolveEntity (String JavaDoc publicId, String JavaDoc systemId)
698         throws SAXException {
699             if (INSTANCE_DTD_ID_2_0.equals(publicId)) {
700                 InputStream is = new ByteArrayInputStream(new byte[0]);
701                 //getClass().getResourceAsStream(INSTANCE_DTD_LOCAL);
702
// if (is == null) {
703
// throw new IllegalStateException ("Entity cannot be resolved."); // NOI18N
704
// }
705
return new InputSource(is);
706             }
707             return null; // i.e. follow advice of systemID
708
}
709     }
710     
711 }
712
713
Popular Tags