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.modules.j2ee.sun.dd.impl.cmp;
042:
043: import java.beans.PropertyChangeEvent;
044: import java.beans.PropertyChangeListener;
045: import java.io.IOException;
046: import java.io.OutputStream;
047: import java.io.Writer;
048: import java.math.BigDecimal;
049: import java.util.ArrayList;
050: import java.util.List;
051: import org.netbeans.modules.j2ee.sun.dd.api.ASDDVersion;
052: import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
053: import org.netbeans.modules.j2ee.sun.dd.api.DDException;
054: import org.netbeans.modules.j2ee.sun.dd.api.RootInterface;
055: import org.netbeans.modules.j2ee.sun.dd.api.cmp.SunCmpMapping;
056: import org.netbeans.modules.j2ee.sun.dd.api.cmp.SunCmpMappings;
057: import org.netbeans.modules.j2ee.sun.dd.impl.DTDRegistry;
058: import org.netbeans.modules.j2ee.sun.dd.impl.DDTreeWalker;
059: import org.netbeans.modules.j2ee.sun.dd.impl.RootInterfaceImpl;
060: import org.netbeans.modules.j2ee.sun.dd.impl.common.DDProviderDataObject;
061: import org.netbeans.modules.schema2beans.BaseBean;
062: import org.netbeans.modules.schema2beans.GraphManager;
063: import org.netbeans.modules.schema2beans.Schema2BeansUtil;
064: import org.openide.filesystems.FileLock;
065: import org.openide.filesystems.FileObject;
066: import org.openide.loaders.DataObject;
067: import org.w3c.dom.Document;
068:
069: /**
070: *
071: * @author Peter Williams
072: */
073: public class SunCmpMappingsProxy implements SunCmpMappings,
074: RootInterfaceImpl {
075:
076: private SunCmpMappings cmpMappingsRoot;
077: private String version;
078: private int ddStatus;
079: private org.xml.sax.SAXParseException error;
080: private List<PropertyChangeListener> listeners;
081: private Schema2BeansUtil.ReindentationListener reindentationListener = new Schema2BeansUtil.ReindentationListener();
082:
083: public SunCmpMappingsProxy(SunCmpMappings cmpMappingsRoot,
084: String version) {
085: this .cmpMappingsRoot = cmpMappingsRoot;
086: this .version = version;
087: this .listeners = new ArrayList<PropertyChangeListener>();
088: addPropertyChangeListener(reindentationListener);
089: }
090:
091: public void setSunCmpMapping(int index, SunCmpMapping value) {
092: if (cmpMappingsRoot != null)
093: cmpMappingsRoot.setSunCmpMapping(index, value);
094: }
095:
096: public SunCmpMapping getSunCmpMapping(int index) {
097: return (cmpMappingsRoot != null) ? cmpMappingsRoot
098: .getSunCmpMapping(index) : null;
099: }
100:
101: public int sizeSunCmpMapping() {
102: return (cmpMappingsRoot != null) ? cmpMappingsRoot
103: .sizeSunCmpMapping() : -1;
104: }
105:
106: public void setSunCmpMapping(SunCmpMapping[] value) {
107: if (cmpMappingsRoot != null)
108: cmpMappingsRoot.setSunCmpMapping(value);
109: }
110:
111: public SunCmpMapping[] getSunCmpMapping() {
112: return (cmpMappingsRoot != null) ? cmpMappingsRoot
113: .getSunCmpMapping() : null;
114: }
115:
116: public int addSunCmpMapping(SunCmpMapping value) {
117: return (cmpMappingsRoot != null) ? cmpMappingsRoot
118: .addSunCmpMapping(value) : -1;
119: }
120:
121: public int removeSunCmpMapping(SunCmpMapping value) {
122: return (cmpMappingsRoot != null) ? cmpMappingsRoot
123: .removeSunCmpMapping(value) : -1;
124: }
125:
126: public SunCmpMapping newSunCmpMapping() {
127: return (cmpMappingsRoot != null) ? cmpMappingsRoot
128: .newSunCmpMapping() : null;
129: }
130:
131: public void setVersion(BigDecimal version) {
132: String newVersion = version.toString();
133: String currentVersion = null;
134: if (this .version.equals(newVersion))
135: return;
136: if (cmpMappingsRoot != null) {
137: Document document = null;
138: if (cmpMappingsRoot instanceof org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_0.SunCmpMappings) {
139: document = ((org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_0.SunCmpMappings) cmpMappingsRoot)
140: .graphManager().getXmlDocument();
141: currentVersion = SunCmpMappings.VERSION_1_0;
142: } else if (cmpMappingsRoot instanceof org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_1.SunCmpMappings) {
143: document = ((org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_1.SunCmpMappings) cmpMappingsRoot)
144: .graphManager().getXmlDocument();
145: currentVersion = SunCmpMappings.VERSION_1_1;
146: } else if (cmpMappingsRoot instanceof org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_2.SunCmpMappings) {
147: document = ((org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_2.SunCmpMappings) cmpMappingsRoot)
148: .graphManager().getXmlDocument();
149: currentVersion = SunCmpMappings.VERSION_1_2;
150: }
151:
152: //remove the doctype
153: document = removeDocType(document);
154:
155: if (newVersion.equals(SunCmpMappings.VERSION_1_2)) {
156: if (currentVersion.equals(SunCmpMappings.VERSION_1_1)
157: || currentVersion
158: .equals(SunCmpMappings.VERSION_1_0))
159: generate1_2Graph(document);
160: else
161: downgradeCmpMappingsGraph(document, newVersion,
162: currentVersion);
163: }
164: if (newVersion.equals(SunCmpMappings.VERSION_1_1)) {
165: if (currentVersion.equals(SunCmpMappings.VERSION_1_0))
166: generate1_1Graph(document);
167: else
168: downgradeCmpMappingsGraph(document, newVersion,
169: currentVersion);
170: }
171: if (newVersion.equals(SunCmpMappings.VERSION_1_0)) {
172: //This will always be a downgrade
173: downgradeCmpMappingsGraph(document, newVersion,
174: currentVersion);
175: }
176: }
177: }
178:
179: private void downgradeCmpMappingsGraph(Document document,
180: String downgradeVersion, String currentVersion) {
181: DDTreeWalker downgradeScanner = new DDTreeWalker(document,
182: downgradeVersion, currentVersion);
183: downgradeScanner.downgradeSunCmpMappingsDocument();
184: if (downgradeVersion.equals(SunCmpMappings.VERSION_1_2)) {
185: generate1_2Graph(document);
186: } else if (downgradeVersion.equals(SunCmpMappings.VERSION_1_1)) {
187: generate1_1Graph(document);
188: } else if (downgradeVersion.equals(SunCmpMappings.VERSION_1_0)) {
189: generate1_0Graph(document);
190: }
191: }
192:
193: private void generate1_2Graph(Document document) {
194: org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_2.SunCmpMappings cmpMappingsGraph = org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_2.SunCmpMappings
195: .createGraph(document);
196: cmpMappingsGraph.changeDocType(
197: DTDRegistry.SUN_CMP_MAPPING_810_DTD_PUBLIC_ID,
198: DTDRegistry.SUN_CMP_MAPPING_810_DTD_SYSTEM_ID);
199: this .cmpMappingsRoot = cmpMappingsGraph;
200: }
201:
202: private void generate1_1Graph(Document document) {
203: org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_1.SunCmpMappings cmpMappingsGraph = org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_1.SunCmpMappings
204: .createGraph(document);
205: cmpMappingsGraph.changeDocType(
206: DTDRegistry.SUN_CMP_MAPPING_800_DTD_PUBLIC_ID,
207: DTDRegistry.SUN_CMP_MAPPING_800_DTD_SYSTEM_ID);
208: this .cmpMappingsRoot = cmpMappingsGraph;
209: }
210:
211: private void generate1_0Graph(Document document) {
212: org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_0.SunCmpMappings cmpMappingsGraph = org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_0.SunCmpMappings
213: .createGraph(document);
214: cmpMappingsGraph.changeDocType(
215: DTDRegistry.SUN_CMP_MAPPING_700_DTD_PUBLIC_ID,
216: DTDRegistry.SUN_CMP_MAPPING_700_DTD_SYSTEM_ID);
217: this .cmpMappingsRoot = cmpMappingsGraph;
218: }
219:
220: public BigDecimal getVersion() {
221: return new java.math.BigDecimal(version);
222: }
223:
224: private Document getDocument() {
225: Document document = null;
226: if (cmpMappingsRoot instanceof org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_0.SunCmpMappings) {
227: document = ((org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_0.SunCmpMappings) cmpMappingsRoot)
228: .graphManager().getXmlDocument();
229: } else if (cmpMappingsRoot instanceof org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_1.SunCmpMappings) {
230: document = ((org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_1.SunCmpMappings) cmpMappingsRoot)
231: .graphManager().getXmlDocument();
232: } else if (cmpMappingsRoot instanceof org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_2.SunCmpMappings) {
233: document = ((org.netbeans.modules.j2ee.sun.dd.impl.cmp.model_1_2.SunCmpMappings) cmpMappingsRoot)
234: .graphManager().getXmlDocument();
235: }
236: return document;
237: }
238:
239: private Document removeDocType(Document document) {
240: if (document != null) {
241: org.w3c.dom.Element docElement = document
242: .getDocumentElement();
243: if (docElement != null) {
244: org.w3c.dom.DocumentType docType = document
245: .getDoctype();
246: if (docType != null) {
247: document.removeChild(docType); //NOI18N
248: }
249: }
250: }
251: return document;
252: }
253:
254: public void merge(CommonDDBean root, int mode) {
255: if (root instanceof SunCmpMappingsProxy) {
256: root = ((SunCmpMappingsProxy) root).getOriginal();
257: }
258: if (cmpMappingsRoot != root && root instanceof SunCmpMappings) {
259: SunCmpMappings newCmpMappingsRoot = (SunCmpMappings) root;
260: if (cmpMappingsRoot != null
261: && cmpMappingsRoot.getVersion().equals(
262: newCmpMappingsRoot.getVersion())) {
263: removePropertyChangeListener(reindentationListener);
264: cmpMappingsRoot.merge(newCmpMappingsRoot, mode);
265: addPropertyChangeListener(reindentationListener);
266: } else {
267: setOriginal((SunCmpMappings) newCmpMappingsRoot.clone());
268: }
269: }
270: }
271:
272: public CommonDDBean cloneVersion(String version) {
273: return cmpMappingsRoot == null ? null : cmpMappingsRoot
274: .cloneVersion(version);
275: }
276:
277: public void setOriginal(SunCmpMappings cmpMappingsRoot) {
278: if (this .cmpMappingsRoot != cmpMappingsRoot) {
279: for (int i = 0; i < listeners.size(); i++) {
280: PropertyChangeListener pcl = listeners.get(i);
281: if (this .cmpMappingsRoot != null) {
282: this .cmpMappingsRoot
283: .removePropertyChangeListener(pcl);
284: }
285: if (cmpMappingsRoot != null) {
286: cmpMappingsRoot.addPropertyChangeListener(pcl);
287: }
288: }
289: this .cmpMappingsRoot = cmpMappingsRoot;
290: if (cmpMappingsRoot != null) {
291: setProxyVersion(cmpMappingsRoot.getVersion().toString());
292: }
293: }
294: }
295:
296: public SunCmpMappings getOriginal() {
297: return cmpMappingsRoot;
298: }
299:
300: public void addPropertyChangeListener(PropertyChangeListener pcl) {
301: if (cmpMappingsRoot != null)
302: cmpMappingsRoot.addPropertyChangeListener(pcl);
303: listeners.add(pcl);
304: }
305:
306: public void removePropertyChangeListener(PropertyChangeListener pcl) {
307: if (cmpMappingsRoot != null)
308: cmpMappingsRoot.removePropertyChangeListener(pcl);
309: listeners.remove(pcl);
310: }
311:
312: public Object getValue(String propertyName) {
313: return cmpMappingsRoot == null ? null : cmpMappingsRoot
314: .getValue(propertyName);
315: }
316:
317: public Object[] getValues(String name) {
318: return cmpMappingsRoot == null ? null : cmpMappingsRoot
319: .getValues(name);
320: }
321:
322: public Object getValue(String name, int index) {
323: return cmpMappingsRoot == null ? null : cmpMappingsRoot
324: .getValue(name, index);
325: }
326:
327: public void setValue(String name, Object value) {
328: if (cmpMappingsRoot != null)
329: cmpMappingsRoot.setValue(name, value);
330: }
331:
332: public void setValue(String name, Object[] value) {
333: if (cmpMappingsRoot != null)
334: cmpMappingsRoot.setValue(name, value);
335: }
336:
337: public void setValue(String name, int index, Object value) {
338: if (cmpMappingsRoot != null)
339: cmpMappingsRoot.setValue(name, index, value);
340: }
341:
342: public String getAttributeValue(String name) {
343: return cmpMappingsRoot == null ? null : cmpMappingsRoot
344: .getAttributeValue(name);
345: }
346:
347: public String getAttributeValue(String propName, String name) {
348: return cmpMappingsRoot == null ? null : cmpMappingsRoot
349: .getAttributeValue(propName, name);
350: }
351:
352: public String getAttributeValue(String propName, int index,
353: String name) {
354: return cmpMappingsRoot == null ? null : cmpMappingsRoot
355: .getAttributeValue(propName, index, name);
356: }
357:
358: public void setAttributeValue(String name, String value) {
359: if (cmpMappingsRoot != null)
360: cmpMappingsRoot.setAttributeValue(name, value);
361: }
362:
363: public void setAttributeValue(String propName, int index,
364: String name, String value) {
365: if (cmpMappingsRoot != null)
366: cmpMappingsRoot.setAttributeValue(propName, index, name,
367: value);
368: }
369:
370: public void setAttributeValue(String propName, String name,
371: String value) {
372: if (cmpMappingsRoot != null)
373: cmpMappingsRoot.setAttributeValue(propName, name, value);
374: }
375:
376: public String[] findPropertyValue(String propName, Object value) {
377: return cmpMappingsRoot == null ? null : cmpMappingsRoot
378: .findPropertyValue(propName, value);
379: }
380:
381: public int addValue(String name, Object value) {
382: return cmpMappingsRoot == null ? -1 : cmpMappingsRoot.addValue(
383: name, value);
384: }
385:
386: public int removeValue(String name, Object value) {
387: return cmpMappingsRoot == null ? -1 : cmpMappingsRoot
388: .removeValue(name, value);
389: }
390:
391: public void removeValue(String name, int index) {
392: if (cmpMappingsRoot != null)
393: cmpMappingsRoot.removeValue(name, index);
394: }
395:
396: public void write(OutputStream os) throws IOException {
397: if (cmpMappingsRoot != null) {
398: cmpMappingsRoot.write(os);
399: }
400: }
401:
402: public void write(Writer w) throws IOException, DDException {
403: if (cmpMappingsRoot != null)
404: cmpMappingsRoot.write(w);
405: }
406:
407: public void write(FileObject fo) throws IOException {
408: if (cmpMappingsRoot != null) {
409: DataObject dataObject = DataObject.find(fo);
410: if (dataObject instanceof DDProviderDataObject) {
411: ((DDProviderDataObject) dataObject)
412: .writeModel(cmpMappingsRoot);
413: } else {
414: FileLock lock = fo.lock();
415: try {
416: OutputStream os = fo.getOutputStream(lock);
417: try {
418: write(os);
419: } finally {
420: os.close();
421: }
422: } finally {
423: lock.releaseLock();
424: }
425: }
426: }
427: }
428:
429: public String dumpBeanNode() {
430: if (cmpMappingsRoot != null)
431: return cmpMappingsRoot.dumpBeanNode();
432: else
433: return null;
434: }
435:
436: public CommonDDBean getPropertyParent(String name) {
437: return cmpMappingsRoot.getPropertyParent(name);
438: }
439:
440: public Object clone() {
441: SunCmpMappingsProxy proxy = null;
442: if (cmpMappingsRoot == null)
443: proxy = new SunCmpMappingsProxy(null, version);
444: else {
445: SunCmpMappings clonedSunCmpMappings = (SunCmpMappings) cmpMappingsRoot
446: .clone();
447: proxy = new SunCmpMappingsProxy(clonedSunCmpMappings,
448: version);
449: }
450: proxy.setError(error);
451: return proxy;
452: }
453:
454: public org.xml.sax.SAXParseException getError() {
455: return error;
456: }
457:
458: public void setError(org.xml.sax.SAXParseException error) {
459: this .error = error;
460: }
461:
462: public void setProxyVersion(java.lang.String value) {
463: if ((version == null && value != null)
464: || (version != null && !version.equals(value))) {
465: PropertyChangeEvent evt = new PropertyChangeEvent(this ,
466: PROPERTY_VERSION, version, value);
467: version = value;
468: for (int i = 0; i < listeners.size(); i++) {
469: listeners.get(i).propertyChange(evt);
470: }
471: }
472: }
473:
474: public int size(String name) {
475: return cmpMappingsRoot == null ? -1 : cmpMappingsRoot
476: .size(name);
477: }
478:
479: public int getStatus() {
480: return ddStatus;
481: }
482:
483: public void setStatus(int value) {
484: if (ddStatus != value) {
485: PropertyChangeEvent evt = new PropertyChangeEvent(this ,
486: PROPERTY_STATUS, Integer.valueOf(ddStatus), Integer
487: .valueOf(value));
488: ddStatus = value;
489: for (int i = 0; i < listeners.size(); i++) {
490: listeners.get(i).propertyChange(evt);
491: }
492: }
493: }
494:
495: public RootInterface getRootInterface() {
496: return this ;
497: }
498:
499: public boolean hasOriginal() {
500: return getOriginal() != null;
501: }
502:
503: public ASDDVersion getASDDVersion() {
504: return ASDDVersion
505: .getASDDVersionFromCmpMappingsVersion(getVersion());
506: }
507:
508: public boolean isTrivial(String nameProperty) {
509: // Root nodes are non-trivial by definition.
510: return false;
511: }
512:
513: public boolean isEventSource(RootInterface rootDD) {
514: return cmpMappingsRoot != null && cmpMappingsRoot == rootDD;
515: }
516:
517: public GraphManager graphManager() {
518: return cmpMappingsRoot instanceof BaseBean ? ((BaseBean) cmpMappingsRoot)
519: .graphManager()
520: : null;
521: }
522:
523: }
|