KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > taglib > TLDDataObject


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.web.taglib;
21
22 import java.io.*;
23 import org.xml.sax.InputSource JavaDoc;
24
25 import org.openide.loaders.*;
26 import org.openide.filesystems.FileObject;
27 import org.openide.nodes.Node;
28 import org.openide.util.NbBundle;
29
30 import org.netbeans.api.xml.cookies.ValidateXMLCookie;
31 import org.netbeans.api.xml.cookies.CheckXMLCookie;
32 import org.netbeans.spi.xml.cookies.*;
33
34 import org.netbeans.modules.web.taglib.model.Taglib;
35
36 /** Object that provides main functionality for TLDLoader(data loader).
37  * This class is final only for performance reasons,
38  * can be unfinaled if desired.
39  *
40  */

41 public final class TLDDataObject extends MultiDataObject implements org.openide.nodes.CookieSet.Factory {
42
43     private static final boolean debug = false;
44     /** Editor support for text data object. */
45     private transient TLDEditorSupport editorSupport;
46     /** generated Serialized Version UID */
47     private static final long serialVersionUID = -7581377241494497816L;
48     
49     public TLDDataObject ()
50         throws DataObjectExistsException, IOException {
51     super(null, null);
52     if (debug) System.out.println("====> TLDDataObject():constructor"); // NOI18N
53
}
54
55     public TLDDataObject (final FileObject obj, final MultiFileLoader loader)
56     throws DataObjectExistsException, IOException {
57     super (obj, loader);
58         
59         getCookieSet().add(TLDEditorSupport.class, this);
60         
61         // Creates Check XML and Validate XML context actions
62
InputSource JavaDoc in = DataObjectAdapters.inputSource(this);
63         CheckXMLCookie checkCookie = new CheckXMLSupport(in);
64         getCookieSet().add(checkCookie);
65         ValidateXMLCookie validateCookie = new ValidateXMLSupport(in);
66         getCookieSet().add(validateCookie);
67         
68     if (debug) System.out.println("====> TLDDataObject(FileObject, loader):constructor()"); // NOI18N
69

70     //
71
// Sometimes the FileObject is not valid. This most usually
72
// occurs when the tag library exists in a source controlled
73
// filesystem such as a CVS or Teamware filesystem, has been
74
// checked in and then deleted. The source control system
75
// reports the existence of the FileObject, but the filesystem
76
// does not. In this case we throw an IOException, and the
77
// data object does not get built.
78
//
79
/*
80     if (!isValid(obj)) {
81         
82         MessageFormat msgFormat =
83         new MessageFormat(resbundle.getString("TLDDataObject_FileDoesntExist")); // NOI18N
84         Object[] arg0 = new Object[] {getPrimaryFile().getName()};
85         // PENDING: somehow we seem to be doing nothing here.
86         //String msg = msgFormat.format(arg0);
87         //System.out.println(msg);
88         //throw new IOException(msg);
89     }
90         */

91     }
92     
93     /**
94      * Provides node that should represent this data object. When a node for
95      * representation in a parent is requested by a call to getNode (parent)
96      * it is the exact copy of this node
97      * with only parent changed. This implementation creates instance
98      * <CODE>DataNode</CODE>.
99      * <P>
100      * This method is called only once.
101      *
102      * @return the node representation for this data object
103      * @see DataNode
104      */

105     protected synchronized Node createNodeDelegate () {
106     return new TLDNode(this);
107     }
108     
109      // Accessibility from TXTEditorSupport:
110
org.openide.nodes.CookieSet getCookieSet0() {
111         return getCookieSet();
112     }
113     
114     public Taglib getTaglib() throws java.io.IOException JavaDoc {
115         java.io.InputStream JavaDoc is = getPrimaryFile().getInputStream();
116         try {
117             return Taglib.createGraph(is);
118         } catch (RuntimeException JavaDoc ex) {
119             throw new java.io.IOException JavaDoc(ex.getMessage());
120         }
121     }
122     
123     public void write(Taglib taglib) throws java.io.IOException JavaDoc {
124         java.io.File JavaDoc file = org.openide.filesystems.FileUtil.toFile(getPrimaryFile());
125         org.openide.filesystems.FileObject tldFO = getPrimaryFile();
126         try {
127             org.openide.filesystems.FileLock lock = tldFO.lock();
128             try {
129                 java.io.OutputStream JavaDoc os = tldFO.getOutputStream(lock);
130                 try {
131                     String JavaDoc version=taglib.getAttributeValue("version"); //NOI18N
132
if (version==null) { //JSP1.2 version
133
taglib.changeDocType("-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN", //NOI18N
134
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"); //NOI18N
135
taglib.setAttributeValue("xmlns",null); //NOI18N
136
}
137                     taglib.write(os);
138                 } finally {
139                     os.close();
140                 }
141             }
142             finally {
143                 lock.releaseLock();
144             }
145         } catch (org.openide.filesystems.FileAlreadyLockedException ex) {
146             // PENDING should write a message
147
}
148     }
149     
150     /** Implements <code>CookieSet.Factory</code> interface. */
151     public Node.Cookie createCookie(Class JavaDoc clazz) {
152         if(clazz.isAssignableFrom(TLDEditorSupport.class))
153             return getEditorSupport();
154         else
155             return null;
156     }
157     
158     /** Gets editor support for this data object. */
159     private TLDEditorSupport getEditorSupport() {
160         if(editorSupport == null) {
161             synchronized(this) {
162                 if(editorSupport == null)
163                     editorSupport = new TLDEditorSupport(this);
164             }
165         }
166         
167         return editorSupport;
168     }
169     
170 }
171
Popular Tags