KickJava   Java API By Example, From Geeks To Geeks.

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


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 Mode configuration data.
37  *
38  * @author Marek Slama
39  */

40
41 class TCRefParser {
42     
43     public static final String JavaDoc INSTANCE_DTD_ID_1_0
44     = "-//NetBeans//DTD Top Component in Mode Properties 1.0//EN"; // NOI18N
45
public static final String JavaDoc INSTANCE_DTD_ID_2_0
46     = "-//NetBeans//DTD Top Component in Mode Properties 2.0//EN"; // NOI18N
47
public static final String JavaDoc INSTANCE_DTD_ID_2_1
48     = "-//NetBeans//DTD Top Component in Mode Properties 2.1//EN"; // NOI18N
49
public static final String JavaDoc INSTANCE_DTD_ID_2_2
50     = "-//NetBeans//DTD Top Component in Mode Properties 2.2//EN"; // NOI18N
51

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

102     InternalConfig getInternalConfig () {
103         if (internalConfig == null) {
104             internalConfig = new InternalConfig();
105         }
106         return internalConfig;
107     }
108     
109     /** Setter for internal configuration data. Used only to pass module info
110      * from import.
111      * @param internalCfg instance of internal configuration data
112      */

113     void setInternalConfig (InternalConfig internalCfg) {
114         internalConfig = internalCfg;
115     }
116     
117     boolean isInModuleFolder () {
118         return inModuleFolder;
119     }
120     
121     void setInModuleFolder (boolean inModuleFolder) {
122         this.inModuleFolder = inModuleFolder;
123     }
124     
125     boolean isInLocalFolder () {
126         return inLocalFolder;
127     }
128     
129     void setInLocalFolder (boolean inLocalFolder) {
130         this.inLocalFolder = inLocalFolder;
131     }
132     
133     void setModuleParentFolder (FileObject moduleParentFolder) {
134         this.moduleParentFolder = moduleParentFolder;
135     }
136     
137     void setLocalParentFolder (FileObject localParentFolder) {
138         this.localParentFolder = localParentFolder;
139     }
140     
141     void log (String JavaDoc s) {
142         Debug.log(TCRefParser.class, s);
143     }
144     
145     
146     private final class PropertyHandler extends DefaultHandler JavaDoc {
147         
148         /** tcRef manager configuration data */
149         private TCRefConfig tcRefConfig = null;
150         
151         /** internal configuration data */
152         private InternalConfig internalConfig = null;
153         
154         /** Lock to prevent mixing readData and writeData */
155         private final Object JavaDoc RW_LOCK = new Object JavaDoc();
156         
157         public PropertyHandler () {
158         }
159         
160         private FileObject getConfigFOInput () {
161             FileObject tcRefConfigFO;
162             if (isInLocalFolder()) {
163                 //log("getConfigFOInput" + " looking for LOCAL");
164
tcRefConfigFO = localParentFolder.getFileObject
165                 (TCRefParser.this.getName(), PersistenceManager.TCREF_EXT);
166             } else if (isInModuleFolder()) {
167                 //log("getConfigFOInput" + " looking for MODULE");
168
tcRefConfigFO = moduleParentFolder.getFileObject
169                 (TCRefParser.this.getName(), PersistenceManager.TCREF_EXT);
170             } else {
171                 //XXX should not happen
172
tcRefConfigFO = null;
173             }
174             //log("getConfigFOInput" + " tcRefConfigFO:" + tcRefConfigFO);
175
return tcRefConfigFO;
176         }
177
178         private FileObject getConfigFOOutput () throws IOException {
179             FileObject tcRefConfigFO;
180             tcRefConfigFO = localParentFolder.getFileObject
181             (TCRefParser.this.getName(), PersistenceManager.TCREF_EXT);
182             if (tcRefConfigFO != null) {
183                 //log("-- TCRefParser.getConfigFOOutput" + " tcRefConfigFO LOCAL:" + tcRefConfigFO);
184
return tcRefConfigFO;
185             } else {
186                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
187                 buffer.append(TCRefParser.this.getName());
188                 buffer.append('.');
189                 buffer.append(PersistenceManager.TCREF_EXT);
190                 //XXX should be improved localParentFolder can be null
191
tcRefConfigFO = FileUtil.createData(localParentFolder, buffer.toString());
192                 //log("-- TCRefParser.getConfigFOOutput" + " LOCAL not found CREATE");
193
return tcRefConfigFO;
194             }
195         }
196         /**
197          Reads tcRef configuration data from XML file.
198          Data are returned in output params.
199          */

200         void readData (TCRefConfig tcRefCfg, InternalConfig internalCfg)
201         throws IOException {
202             tcRefConfig = tcRefCfg;
203             internalConfig = internalCfg;
204             
205             FileObject cfgFOInput = getConfigFOInput();
206             if (cfgFOInput == null) {
207                 throw new FileNotFoundException("[WinSys] Missing TCRef configuration file:" // NOI18N
208
+ TCRefParser.this.getName());
209             }
210             InputStream is = null;
211             try {
212                 synchronized (RW_LOCK) {
213                     //DUMP BEGIN
214
/*InputStream is = cfgFOInput.getInputStream();
215                     byte [] arr = new byte [is.available()];
216                     is.read(arr);
217                     log("DUMP TCRef: " + TCRefParser.this.getName());
218                     String s = new String(arr);
219                     log(s);*/

220                     //DUMP END
221
is = cfgFOInput.getInputStream();
222                     PersistenceManager.getDefault().getXMLParser(this).parse(new InputSource(is));
223                 }
224             } catch (SAXException exc) {
225                 // Turn into annotated IOException
226
String JavaDoc msg = NbBundle.getMessage(TCRefParser.class,
227                                                  "EXC_TCRefParse", cfgFOInput);
228
229                 throw (IOException) new IOException(msg).initCause(exc);
230             } finally {
231                 try {
232                     if (is != null) {
233                         is.close();
234                     }
235                 } catch (IOException exc) {
236                     Logger.getLogger(TCRefParser.class.getName()).log(Level.WARNING, null, exc);
237                 }
238             }
239                         
240             tcRefCfg = tcRefConfig;
241             internalCfg = internalConfig;
242             
243             tcRefConfig = null;
244             internalConfig = null;
245         }
246         
247         public void startElement (String JavaDoc nameSpace, String JavaDoc name, String JavaDoc qname, Attributes attrs)
248         throws SAXException {
249             if ("tc-ref".equals(qname)) { // NOI18N
250
handleTCRef(attrs);
251             } else if (internalConfig.specVersion.compareTo(new SpecificationVersion("2.0")) >= 0) { // NOI18N
252
//Parse version 2.0
253
if ("module".equals(qname)) { // NOI18N
254
handleModule(attrs);
255                 } else if ("tc-id".equals(qname)) { // NOI18N
256
handleTcId(attrs);
257                 } else if ("state".equals(qname)) { // NOI18N
258
handleState(attrs);
259                 } else if ("previousMode".equals(qname)) { // NOI18N
260
handlePreviousMode(attrs);
261                 } else if ("docking-status".equals(qname)) { // NOI18N
262
handleDockingStatus(attrs);
263                 } else if ("slide-in-status".equals(qname)) { // NOI18N
264
handleSlideInStatus(attrs);
265                 }
266             } else {
267                 log("-- TCRefParser.startElement PARSING OLD");
268                 //Parse version < 2.0
269
}
270         }
271
272         public void error(SAXParseException ex) throws SAXException {
273             throw ex;
274         }
275
276         /** Reads element "tc-ref" */
277         private void handleTCRef (Attributes attrs) {
278             String JavaDoc version = attrs.getValue("version"); // NOI18N
279
if (version != null) {
280                 internalConfig.specVersion = new SpecificationVersion(version);
281             } else {
282                 PersistenceManager.LOG.log(Level.WARNING,
283                 "[WinSys.TCRefParser.handleTCRef]" // NOI18N
284
+ " Warning: Missing attribute \"version\" of element \"tc-ref\"."); // NOI18N
285
internalConfig.specVersion = new SpecificationVersion("2.0"); // NOI18N
286
}
287             //Before version 2.0 tc_id was attribute of tc-ref element
288
//so we must read it directly here.
289
if (internalConfig.specVersion.compareTo(new SpecificationVersion("2.0")) < 0) { // NOI18N
290
String JavaDoc tc_id = attrs.getValue("id"); // NOI18N
291
if (tc_id != null) {
292                     //XXX handle old format
293
} else {
294                     PersistenceManager.LOG.log(Level.WARNING,
295                     "[WinSys.TCRefParser.handleTCRef]" // NOI18N
296
+ " Warning: Missing attribute \"id\" of element \"tc-ref\"."); // NOI18N
297
}
298             }
299         }
300         
301         /** Reads element "module" and updates mode config content */
302         private void handleModule (Attributes attrs) {
303             String JavaDoc moduleCodeName = attrs.getValue("name"); // NOI18N
304
//Parse code name
305
internalConfig.moduleCodeNameBase = null;
306             internalConfig.moduleCodeNameRelease = null;
307             internalConfig.moduleSpecificationVersion = null;
308             if (moduleCodeName != null) {
309                 int i = moduleCodeName.indexOf('/');
310                 if (i != -1) {
311                     internalConfig.moduleCodeNameBase = moduleCodeName.substring(0, i);
312                     internalConfig.moduleCodeNameRelease = moduleCodeName.substring(i + 1);
313                     checkReleaseCode(internalConfig);
314                 } else {
315                     internalConfig.moduleCodeNameBase = moduleCodeName;
316                 }
317                 internalConfig.moduleSpecificationVersion = attrs.getValue("spec"); // NOI18N
318
}
319         }
320
321         /** Checks validity of <code>moduleCodeNameRelease</code> field.
322          * Helper method. */

323         private void checkReleaseCode (InternalConfig internalConfig) {
324             // #24844. Repair the wrongly saved "null" string
325
// as release number.
326
if("null".equals(internalConfig.moduleCodeNameRelease)) { // NOI18N
327
Logger.getLogger(TCRefParser.class.getName()).log(Level.WARNING, null,
328                                   new IllegalStateException JavaDoc("Module release code was saved as null string" +
329                                                             " for module " +
330                                                             internalConfig.moduleCodeNameBase +
331                                                             "! Repairing."));
332                 internalConfig.moduleCodeNameRelease = null;
333             }
334         }
335         
336         /** Reads element "tc-id" */
337         private void handleTcId (Attributes attrs) throws SAXException {
338             String JavaDoc tc_id = attrs.getValue("id"); // NOI18N
339
if (tc_id != null) {
340                 tcRefConfig.tc_id = tc_id;
341                 if (!tc_id.equals(TCRefParser.this.getName())) {
342                     PersistenceManager.LOG.log(Level.WARNING,
343                     "[WinSys.TCRefParser.handleTcId]" // NOI18N
344
+ " Error: Value of attribute \"id\" of element \"tc-id\"" // NOI18N
345
+ " and configuration file name must be the same."); // NOI18N
346
throw new SAXException("Invalid attribute value"); // NOI18N
347
}
348             } else {
349                 PersistenceManager.LOG.log(Level.WARNING,
350                 "[WinSys.TCRefParser.handleTcId]" // NOI18N
351
+ " Error: Missing required attribute \"id\" of element \"tc-id\"."); // NOI18N
352
throw new SAXException("Missing required attribute"); // NOI18N
353
}
354         }
355         
356         private void handleState (Attributes attrs) throws SAXException {
357             String JavaDoc opened = attrs.getValue("opened"); // NOI18N;
358
if (opened != null) {
359                 if ("true".equals(opened)) { // NOI18N
360
tcRefConfig.opened = true;
361                 } else if ("false".equals(opened)) { // NOI18N
362
tcRefConfig.opened = false;
363                 } else {
364                     PersistenceManager.LOG.log(Level.WARNING,
365                     "[WinSys.TCRefParser.handleState]" // NOI18N
366
+ " Warning: Invalid value of attribute \"opened\"" // NOI18N
367
+ " of element \"state\"."); // NOI18N
368
tcRefConfig.opened = false;
369                 }
370             } else {
371                 PersistenceManager.LOG.log(Level.WARNING,
372                 "[WinSys.TCRefParser.handleState]" // NOI18N
373
+ " Warning: Missing required attribute \"opened\"" // NOI18N
374
+ " of element \"state\"."); // NOI18N
375
tcRefConfig.opened = false;
376             }
377         }
378
379         private void handlePreviousMode (Attributes attrs) throws SAXException {
380             String JavaDoc name = attrs.getValue("name"); // NOI18N;
381
if (name != null) {
382                 tcRefConfig.previousMode = name;
383             } else {
384                 PersistenceManager.LOG.log(Level.WARNING,
385                 "[WinSys.TCRefParser.handlePreviousMode]" // NOI18N
386
+ " Warning: Missing required attribute \"name\"" // NOI18N
387
+ " of element \"previousMode\"."); // NOI18N
388
tcRefConfig.previousMode = null;
389             }
390             
391             String JavaDoc index = attrs.getValue("index"); // NOI18N;
392
if (index != null) {
393                 try {
394                     tcRefConfig.previousIndex = Integer.parseInt( index );
395                 } catch( NumberFormatException JavaDoc nfE ) {
396                     PersistenceManager.LOG.log(Level.WARNING,
397                     "[WinSys.TCRefParser.handlePreviousMode]" // NOI18N
398
+ " Warning: Invalid value of attribute \"index\"" // NOI18N
399
+ " of element \"previousMode\"."); // NOI18N
400
tcRefConfig.previousIndex = -1;
401                 }
402             }
403         }
404         
405         private void handleDockingStatus (Attributes attrs) throws SAXException {
406             String JavaDoc status = attrs.getValue("maximized-mode"); // NOI18N;
407
if (status != null) {
408                 if ("docked".equals(status)) { // NOI18N
409
tcRefConfig.dockedInMaximizedMode = true;
410                 } else if ("slided".equals(status)) { // NOI18N
411
tcRefConfig.dockedInMaximizedMode = false;
412                 } else {
413                     PersistenceManager.LOG.log(Level.WARNING,
414                     "[WinSys.TCRefParser.handleDockingStatus]" // NOI18N
415
+ " Warning: Invalid value of attribute \"maximized-mode\"" // NOI18N
416
+ " of element \"docking-status\"."); // NOI18N
417
tcRefConfig.dockedInMaximizedMode = false;
418                 }
419             }
420             status = attrs.getValue("default-mode"); // NOI18N;
421
if (status != null) {
422                 if ("docked".equals(status)) { // NOI18N
423
tcRefConfig.dockedInDefaultMode = true;
424                 } else if ("slided".equals(status)) { // NOI18N
425
tcRefConfig.dockedInDefaultMode = false;
426                 } else {
427                     PersistenceManager.LOG.log(Level.WARNING,
428                     "[WinSys.TCRefParser.handleDockingStatus]" // NOI18N
429
+ " Warning: Invalid value of attribute \"default-mode\"" // NOI18N
430
+ " of element \"docking-status\"."); // NOI18N
431
tcRefConfig.dockedInDefaultMode = true;
432                 }
433             }
434         }
435         
436         private void handleSlideInStatus (Attributes attrs) throws SAXException {
437             String JavaDoc status = attrs.getValue("maximized"); // NOI18N;
438
if (status != null) {
439                 if ("true".equals(status)) { // NOI18N
440
tcRefConfig.slidedInMaximized = true;
441                 } else if ("false".equals(status)) { // NOI18N
442
tcRefConfig.slidedInMaximized = false;
443                 } else {
444                     PersistenceManager.LOG.log(Level.WARNING,
445                     "[WinSys.TCRefParser.handleSlideInStatus]" // NOI18N
446
+ " Warning: Invalid value of attribute \"maximized\"" // NOI18N
447
+ " of element \"slide-in-status\"."); // NOI18N
448
tcRefConfig.slidedInMaximized = false;
449                 }
450             }
451         }
452         
453         /** Writes data from asociated tcRef to the xml representation */
454         void writeData (TCRefConfig tcRefCfg, InternalConfig ic) throws IOException {
455             final StringBuffer JavaDoc buff = fillBuffer(tcRefCfg, ic);
456             synchronized (RW_LOCK) {
457                 FileObject cfgFOOutput = getConfigFOOutput();
458                 FileLock lock = null;
459                 OutputStream os = null;
460                 OutputStreamWriter osw = null;
461                 try {
462                     lock = cfgFOOutput.lock();
463                     os = cfgFOOutput.getOutputStream(lock);
464                     osw = new OutputStreamWriter(os, "UTF-8"); // NOI18N
465
osw.write(buff.toString());
466                     //log("DUMP TCRef: " + TCRefParser.this.getName());
467
//log(buff.toString());
468
} finally {
469                     try {
470                         if (osw != null) {
471                             osw.close();
472                         }
473                     } catch (IOException exc) {
474                         Logger.getLogger(TCRefParser.class.getName()).log(Level.WARNING, null, exc);
475                     }
476                     if (lock != null) {
477                         lock.releaseLock();
478                     }
479                 }
480             }
481         }
482         
483         /** Returns xml content in StringBuffer
484          */

485         private StringBuffer JavaDoc fillBuffer (TCRefConfig tcRefCfg, InternalConfig ic) throws IOException {
486             StringBuffer JavaDoc buff = new StringBuffer JavaDoc(800);
487             String JavaDoc curValue = null;
488             // header
489
buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"); // NOI18N
490
/*buff.append("<!DOCTYPE tc-ref PUBLIC\n"); // NOI18N
491             buff.append(" \"-//NetBeans//DTD Top Component in Mode Properties 2.2//EN\"\n"); // NOI18N
492             buff.append(" \"http://www.netbeans.org/dtds/tc-ref2_2.dtd\">\n\n"); // NOI18N*/

493             buff.append("<tc-ref version=\"2.2\">\n"); // NOI18N
494

495             appendModule(ic, buff);
496             appendTcId(tcRefCfg, buff);
497             appendState(tcRefCfg, buff);
498             if (tcRefCfg.previousMode != null) {
499                 appendPreviousMode(tcRefCfg, buff);
500             }
501             appendDockingStatus( tcRefCfg, buff );
502             appendSlideInStatus( tcRefCfg, buff );
503             
504             buff.append("</tc-ref>\n"); // NOI18N
505
return buff;
506         }
507         
508         private void appendModule (InternalConfig ic, StringBuffer JavaDoc buff) {
509             if (ic == null) {
510                 return;
511             }
512             if (ic.moduleCodeNameBase != null) {
513                 buff.append(" <module"); // NOI18N
514
buff.append(" name=\""); // NOI18N
515
buff.append(ic.moduleCodeNameBase);
516                 if (ic.moduleCodeNameRelease != null) {
517                     buff.append("/" + ic.moduleCodeNameRelease); // NOI18N
518
}
519                 if (ic.moduleSpecificationVersion != null) {
520                     buff.append("\" spec=\""); // NOI18N
521
buff.append(ic.moduleSpecificationVersion);
522                 }
523                 buff.append("\" />\n"); // NOI18N
524
}
525         }
526
527         private void appendTcId (TCRefConfig tcRefCfg, StringBuffer JavaDoc buff) {
528             buff.append(" <tc-id"); // NOI18N
529
buff.append(" id=\""); // NOI18N
530

531             buff.append(PersistenceManager.escapeTcId4XmlContent(tcRefCfg.tc_id));
532             buff.append("\""); // NOI18N
533
buff.append(" />\n"); // NOI18N
534
}
535         
536         private void appendState (TCRefConfig tcRefCfg, StringBuffer JavaDoc buff) {
537             buff.append(" <state"); // NOI18N
538
buff.append(" opened=\""); // NOI18N
539
if (tcRefCfg.opened) {
540                 buff.append("true"); // NOI18N
541
} else {
542                 buff.append("false"); // NOI18N
543
}
544             buff.append("\""); // NOI18N
545
buff.append(" />\n"); // NOI18N
546
}
547         
548         private void appendDockingStatus (TCRefConfig tcRefCfg, StringBuffer JavaDoc buff) {
549             if( tcRefCfg.dockedInMaximizedMode || !tcRefCfg.dockedInDefaultMode ) {
550                 buff.append(" <docking-status"); // NOI18N
551
if( tcRefCfg.dockedInMaximizedMode )
552                     buff.append(" maximized-mode=\"docked\""); // NOI18N
553
if( !tcRefCfg.dockedInDefaultMode )
554                     buff.append(" default-mode=\"slided\""); // NOI18N
555
buff.append(" />\n"); // NOI18N
556
}
557         }
558         
559         private void appendSlideInStatus (TCRefConfig tcRefCfg, StringBuffer JavaDoc buff) {
560             if( tcRefCfg.slidedInMaximized ) {
561                 buff.append(" <slide-in-status maximized=\"true\" />\n"); // NOI18N
562
}
563         }
564         
565         private void appendPreviousMode(TCRefConfig tcRefCfg, StringBuffer JavaDoc buff) {
566             buff.append(" <previousMode name=\""); // NOI18N
567
buff.append(tcRefCfg.previousMode).append("\" ");
568             if( tcRefCfg.previousIndex >= 0 )
569                 buff.append( " index=\"" ).append( tcRefCfg.previousIndex ).append( "\" " );
570             buff.append(" />\n"); // NOI18N
571
}
572
573     }
574     
575 }
576
Popular Tags