001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.dp.xml;
006:
007: import org.w3c.dom.*;
008:
009: import com.sun.portal.desktop.context.DPContext;
010:
011: import com.sun.portal.desktop.dp.DPProvider;
012: import com.sun.portal.desktop.dp.DPPropertyHolder;
013: import com.sun.portal.desktop.dp.DPTypes;
014: import com.sun.portal.desktop.dp.DPRoot;
015: import com.sun.portal.desktop.dp.DPError;
016:
017: public class XMLDPProvider extends XMLDPPropertyHolder implements
018: DPProvider, DPTypes, XMLDPTags {
019:
020: static long ccount = 0;
021:
022: XMLDPProvider(DPContext dpc, DPRoot r, Element e) {
023: super (dpc, r, e);
024:
025: //
026: // following line will throw an error if the version
027: // has an invalid value assigned
028: //
029: getProviderVersion();
030: }
031:
032: XMLDPProvider(DPContext dpc, DPRoot r, Document d, String name,
033: String className, int providerVersion) {
034: this (dpc, r, createElement(dpc, d, name, className,
035: providerVersion, XMLDPProperties.createElement(dpc, d)));
036: }
037:
038: public void checkType() {
039: if (!getElement().getTagName().equals(getTag())) {
040: throw new DPError(
041: "XMLDPProvider.checkType(): wrong type tagName="
042: + getElement().getTagName());
043: }
044: }
045:
046: public DPPropertyHolder getParentPropertyHolder() {
047: return getRoot();
048: }
049:
050: public String getTag() {
051: return PROVIDER_TAG;
052: }
053:
054: public short getType() {
055: return PROVIDER_DP;
056: }
057:
058: public String getClassName() {
059: // See whether the provider is locked, if so return
060: // the provider class from the locked node.
061: for (int i = 0; i < getMergers().size(); i++) {
062: DPProvider dpp = (DPProvider) getMergers().get(i);
063: if (dpp.isLocked()) {
064: if (dpp.isRemove()) {
065: // This should not happen, but to be complete
066: return null;
067: }
068: // Return the locked one
069: return dpp.getClassName();
070: }
071: }
072:
073: //if it is a dummy, return the last merger
074: if (isDummy()) {
075: return ((DPProvider) getLastMerger()).getClassName();
076: }
077:
078: return getClassNameFromThis();
079: }
080:
081: String getClassNameFromThis() {
082: return getElement().getAttribute("class");
083: }
084:
085: public void setClassName(String className) {
086: getElement().setAttribute("class", className);
087: }
088:
089: public int getProviderVersion() {
090: // See whether the provider is locked, if so return
091: // the provider version from the locked node.
092: for (int i = 0; i < getMergers().size(); i++) {
093: DPProvider dpp = (DPProvider) getMergers().get(i);
094: if (dpp.isLocked()) {
095: if (dpp.isRemove()) {
096: // This should not happen, but to be complete
097: return -1;
098: }
099: // Return the locked one
100: return dpp.getProviderVersion();
101: }
102: }
103:
104: //if it is a dummy, return the last merger
105: if (isDummy()) {
106: return ((DPProvider) getLastMerger()).getProviderVersion();
107: }
108:
109: return getProviderVersionFromThis();
110: }
111:
112: public int getProviderVersionFromThis() {
113: int version = -1;
114: String versionStr = getElement().getAttribute(
115: PROVIDERVERSION_KEY);
116: if (versionStr == null || versionStr.length() == 0) {
117: version = DEFAULT_PROVIDERVERSION_ATTR;
118: } else {
119: try {
120: version = Integer.parseInt(versionStr);
121: } catch (NumberFormatException nfe) {
122: throw new DPError(
123: "XMLDPProvider.getProviderVersionFromThis(): not a valid version number = "
124: + versionStr);
125: }
126: }
127: return version;
128: }
129:
130: public void setProviderVersion(int version) {
131: getElement().setAttribute(PROVIDERVERSION_KEY,
132: Integer.toString(version));
133: }
134:
135: public DPProvider copy(DPRoot dpr, boolean deep) {
136: XMLDPRoot xmldpr = (XMLDPRoot) dpr;
137:
138: Document ownerDocument = xmldpr.getElement().getOwnerDocument();
139: Element copyElement = (Element) ownerDocument.importNode(
140: getElement(), deep);
141:
142: if (!deep) {
143: //
144: // we're not copying deep, so copy empty versions of the
145: // required sub elements
146: //
147: copyElement.appendChild(ownerDocument
148: .importNode(getChildElement(getElement(),
149: PROPERTIES_TAG, null), false));
150: }
151:
152: return XMLDPFactory.getInstance().getProvider(getContext(),
153: dpr, copyElement);
154: }
155:
156: public DPProvider createDummy(DPRoot dpr) {
157: DPProvider dpp = copy(dpr, false);
158:
159: dpp.setDummy(true);
160: dpp.setDefaults();
161:
162: return dpp;
163: }
164:
165: protected Element getMergedElement() {
166: XMLDPProperties xp = (XMLDPProperties) getProperties();
167: Element e = createElement(getContext(), getDocument(),
168: getName(), getClassName(), getProviderVersion(), xp
169: .getMergedElement());
170: return e;
171: }
172:
173: static Element createElement(DPContext dpc, Document d,
174: String name, String className, int providerVersion,
175: Element propertiesElement) {
176: Element e = createElement(dpc, d, PROVIDER_TAG, name);
177: e.setAttribute("class", className);
178: e.setAttribute(PROVIDERVERSION_KEY, Integer
179: .toString(providerVersion));
180: e.appendChild(propertiesElement);
181:
182: return e;
183: }
184:
185: public void appendProviderVersionAttr(StringBuffer b) {
186: if (getProviderVersionFromThis() != DEFAULT_PROVIDERVERSION_ATTR) {
187: b.append(" " + VERSION_KEY + "=\"").append(
188: getProviderVersionFromThis()).append("\"");
189: }
190: }
191:
192: public void toXML(StringBuffer b, int indent) {
193: if (isDummy()) {
194: return;
195: }
196:
197: indentBuffer(b, indent);
198:
199: appendStartTag(b);
200: b.append(" " + NAME_KEY + "=\"").append(getName()).append("\"")
201: .append(" " + CLASS_KEY + "=\"").append(
202: getClassNameFromThis()).append("\"");
203:
204: appendMergeAttr(b);
205: appendLockAttr(b);
206: appendAdvancedAttr(b);
207: appendProviderVersionAttr(b);
208:
209: b.append(">\n");
210:
211: getPropertiesFromThis().toXML(b, indent + 1);
212:
213: indentBuffer(b, indent);
214: appendEndTag(b);
215: }
216:
217: //-----------------------------------------------------
218: public boolean isContainer() {
219: // See whether the provider is locked, if so return
220: // the provider type from the locked node.
221: for (int i = 0; i < getMergers().size(); i++) {
222: DPProvider dpp = (DPProvider) getMergers().get(i);
223: if (dpp.isLocked()) {
224: if (dpp.isRemove()) {
225: // This should not happen, but to be complete
226: return false;
227: }
228: // Return the locked one
229: return dpp.isContainer();
230: }
231: }
232:
233: //if it is a dummy, return the last merger
234: if (isDummy()) {
235: return ((DPProvider) getLastMerger()).isContainer();
236: }
237:
238: return isThisContainer();
239: }
240:
241: //------------------------------------------------------
242:
243: private boolean isThisContainer() {
244: String val = getElement().getAttribute(CONTAINER_KEY);
245: boolean boolVal = false;
246: if (val != null) {
247: boolVal = new Boolean(val).booleanValue();
248: }
249: return boolVal;
250:
251: }
252:
253: }
|