001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.tax;
042:
043: import org.netbeans.tax.event.TreeEventManager;
044:
045: import org.netbeans.tax.spec.DTD;
046:
047: /**
048: *
049: * @author Libor Kramolis
050: * @version 0.1
051: */
052: public class TreeDTD extends AbstractTreeDTD implements
053: TreeDocumentRoot, TreeDTDRoot {
054:
055: /** */
056: public static final String PROP_VERSION = "version"; // NOI18N
057: /** */
058: public static final String PROP_ENCODING = "encoding"; // NOI18N
059:
060: /** Own event manager. */
061: private TreeEventManager eventManager;
062:
063: /** -- can be null. */
064: private String version;
065:
066: /** -- can be null. */
067: private String encoding;
068:
069: //
070: // init
071: //
072:
073: /**
074: * Creates new TreeDTD.
075: * @throws InvalidArgumentException
076: */
077: public TreeDTD(String version, String encoding)
078: throws InvalidArgumentException {
079: super ();
080:
081: checkVersion(version);
082: checkEncoding(encoding);
083: checkHeader(version, encoding);
084:
085: this .version = version;
086: this .encoding = encoding;
087:
088: if (Util.THIS.isLoggable()) /* then */
089: Util.THIS.debug("TreeDTD::init: encoding = "
090: + this .encoding); // NOI18N
091:
092: this .eventManager = new TreeEventManager();
093: }
094:
095: /** Creates new TreeDTD.
096: * @throws InvalidArgumentException
097: */
098: public TreeDTD() throws InvalidArgumentException {
099: this (null, null);
100: }
101:
102: /** Creates new TreeDTD -- copy constructor. */
103: protected TreeDTD(TreeDTD dtd, boolean deep) {
104: super (dtd, deep);
105:
106: this .version = dtd.version;
107: this .encoding = dtd.encoding;
108:
109: if (Util.THIS.isLoggable()) /* then */
110: Util.THIS.debug("TreeDTD::init [copy]: encoding = "
111: + this .encoding); // NOI18N
112:
113: this .eventManager = new TreeEventManager(dtd.eventManager);
114: }
115:
116: //
117: // from TreeObject
118: //
119:
120: /**
121: */
122: public Object clone(boolean deep) {
123: return new TreeDTD(this , deep);
124: }
125:
126: /**
127: */
128: public boolean equals(Object object, boolean deep) {
129: if (!!!super .equals(object, deep))
130: return false;
131:
132: TreeDTD peer = (TreeDTD) object;
133: if (!!!Util.equals(this .getVersion(), peer.getVersion()))
134: return false;
135: if (!!!Util.equals(this .getEncoding(), peer.getEncoding()))
136: return false;
137:
138: return true;
139: }
140:
141: /*
142: * Merges version and encoding properties.
143: */
144: public void merge(TreeObject treeObject)
145: throws CannotMergeException {
146: super .merge(treeObject);
147:
148: TreeDTD peer = (TreeDTD) treeObject;
149:
150: try {
151: setVersionImpl(peer.getVersion());
152: setEncodingImpl(peer.getEncoding());
153: } catch (Exception exc) {
154: throw new CannotMergeException(treeObject, exc);
155: }
156: }
157:
158: //
159: // from TreeDocumentRoot
160: //
161:
162: /**
163: */
164: public String getVersion() {
165: return version;
166: }
167:
168: /**
169: */
170: private final void setVersionImpl(String newVersion) {
171: String oldVersion = this .version;
172:
173: this .version = newVersion;
174:
175: firePropertyChange(PROP_VERSION, oldVersion, newVersion);
176: }
177:
178: /**
179: * @throws ReadOnlyException
180: * @throws InvalidArgumentException
181: */
182: public final void setVersion(String newVersion)
183: throws ReadOnlyException, InvalidArgumentException {
184: //
185: // check new value
186: //
187: if (Util.equals(this .version, newVersion))
188: return;
189: checkReadOnly();
190: checkVersion(newVersion);
191: checkHeader(newVersion, this .encoding);
192:
193: //
194: // set new value
195: //
196: setVersionImpl(newVersion);
197: }
198:
199: /**
200: */
201: protected final void checkVersion(String version)
202: throws InvalidArgumentException {
203: TreeUtilities.checkDTDVersion(version);
204: }
205:
206: /**
207: */
208: public String getEncoding() {
209: return encoding;
210: }
211:
212: /**
213: */
214: private final void setEncodingImpl(String newEncoding) {
215: String oldEncoding = this .encoding;
216:
217: this .encoding = newEncoding;
218:
219: if (Util.THIS.isLoggable()) /* then */
220: Util.THIS.debug("TreeDTD::setEncodingImpl: encoding = "
221: + this .encoding); // NOI18N
222:
223: firePropertyChange(PROP_ENCODING, oldEncoding, newEncoding);
224: }
225:
226: /**
227: * @throws ReadOnlyException
228: * @throws InvalidArgumentException
229: */
230: public final void setEncoding(String newEncoding)
231: throws ReadOnlyException, InvalidArgumentException {
232: //
233: // check new value
234: //
235: if (Util.equals(this .encoding, newEncoding))
236: return;
237: checkReadOnly();
238: checkEncoding(newEncoding);
239: checkHeader(this .version, newEncoding);
240:
241: //
242: // set new value
243: //
244: setEncodingImpl(newEncoding);
245: }
246:
247: /**
248: */
249: protected final void checkEncoding(String encoding)
250: throws InvalidArgumentException {
251: TreeUtilities.checkDTDEncoding(encoding);
252: }
253:
254: /**
255: * @throws ReadOnlyException
256: * @throws InvalidArgumentException
257: */
258: public final void setHeader(String newVersion, String newEncoding)
259: throws ReadOnlyException, InvalidArgumentException {
260: //
261: // check new value
262: //
263: boolean setVersion = !!!Util.equals(this .version, newVersion);
264: boolean setEncoding = !!!Util
265: .equals(this .encoding, newEncoding);
266: if (!!!setVersion && !!!setEncoding) {
267: return;
268: }
269: checkReadOnly();
270: if (setVersion) {
271: checkVersion(newVersion);
272: }
273: if (setEncoding) {
274: checkEncoding(newEncoding);
275: }
276: checkHeader(newVersion, newEncoding);
277:
278: //
279: // set new value
280: //
281: if (setVersion) {
282: setVersionImpl(newVersion);
283: }
284: if (setEncoding) {
285: setEncodingImpl(newEncoding);
286: }
287: }
288:
289: /**
290: */
291: protected final void checkHeader(String version, String encoding)
292: throws InvalidArgumentException {
293: if ((version != null) && (encoding == null)) {
294: throw new InvalidArgumentException(Util.THIS
295: .getString("EXC_invalid_dtd_header"),
296: new NullPointerException());
297: }
298: }
299:
300: //
301: // event model
302: //
303:
304: /**
305: */
306: public TreeEventManager getRootEventManager() {
307: return eventManager;
308: }
309:
310: //
311: // TreeObjectList.ContentManager
312: //
313:
314: /**
315: */
316: protected TreeObjectList.ContentManager createChildListContentManager() {
317: return new ChildListContentManager();
318: }
319:
320: /**
321: *
322: */
323: protected class ChildListContentManager extends
324: AbstractTreeDTD.ChildListContentManager {
325:
326: /**
327: */
328: public TreeNode getOwnerNode() {
329: return TreeDTD.this ;
330: }
331:
332: /**
333: */
334: public void checkAssignableObject(Object obj) {
335: super .checkAssignableObject(obj);
336: checkAssignableClass(DTD.Child.class, obj);
337: }
338:
339: } // end: class ChildListContentManager
340:
341: }
|