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: import com.sun.portal.desktop.dp.DPError;
011: import com.sun.portal.desktop.dp.DPChannel;
012: import com.sun.portal.desktop.dp.DPTypes;
013: import com.sun.portal.desktop.dp.DPRoot;
014: import com.sun.portal.desktop.dp.DPNode;
015: import com.sun.portal.desktop.dp.DPProvider;
016:
017: public class XMLDPChannel extends XMLDPNode implements DPChannel,
018: DPTypes, XMLDPTags {
019:
020: static long ccount = 0;
021:
022: XMLDPChannel(DPContext dpc, DPRoot r, Element e) {
023: super (dpc, r, e);
024: }
025:
026: /**
027: * Construct a DP channel with empty properties.
028: */
029:
030: XMLDPChannel(DPContext dpc, DPRoot r, Document d, String name,
031: String providerName) {
032: this (dpc, r, createElement(dpc, d, name, providerName,
033: XMLDPProperties.createElement(dpc, d)));
034: }
035:
036: public void checkType() {
037: if ((!getElement().getTagName().equals(getTag()))
038: && (!getElement().getTagName().equals(CONTAINER_TAG))) {
039: throw new DPError(
040: "XMLDPChannel.checkType(): invalid type tagName="
041: + getElement().getTagName());
042: }
043: }
044:
045: public String getTag() {
046: return CHANNEL_TAG;
047: }
048:
049: public short getType() {
050: return CHANNEL_DP;
051: }
052:
053: //
054: // FIXME(susu): getName() gets called quite frequently and it makes
055: // sense to cache this value. however, we first need to figure out
056: // how this value should get refreshed. i.e. when the channel gets added
057: // to a node, the name should be converted to a fully-qualified name.
058: //
059: public String getName() {
060: StringBuffer name = new StringBuffer(super .getName());
061:
062: DPNode parentNode = getParentNode();
063: if (parentNode != null && parentNode.getType() != ROOT_DP) {
064: name.insert(0, CHANNEL_NAME_SEPARATOR);
065: name.insert(0, parentNode.getName());
066: }
067: return name.toString();
068: }
069:
070: public String getChannelName() {
071: return super .getName();
072: }
073:
074: public String getProviderName() {
075: String providerName = null;
076: boolean locked = false;
077:
078: // See whether the channel is locked, if so return
079: // the provider class from the locked node.
080: for (int i = 0; i < getMergers().size(); i++) {
081: DPChannel dpc = (DPChannel) getMergers().get(i);
082: String pn = dpc.getProviderName();
083: if (pn != null && pn.length() > 0) {
084: providerName = pn;
085: }
086:
087: if (dpc.isLocked()) {
088: locked = true;
089: if (dpc.isRemove()) {
090: providerName = null;
091: }
092: break;
093: }
094: }
095:
096: // if it is a not a dummy, and not locked, return set to
097: // the name from this
098: if (!locked && !isDummy()) {
099: String pn = getProviderNameFromThis();
100: if (pn != null && pn.length() > 0) {
101: providerName = pn;
102: }
103: }
104:
105: return providerName;
106: }
107:
108: protected String getProviderNameFromThis() {
109: return getElement().getAttribute("provider");
110: }
111:
112: public void setProviderName(String providerName) {
113: getElement().setAttribute("provider", providerName);
114: setDirty(true);
115: }
116:
117: //
118: // TBD(jtb): declared all well-known dp attribute keys via string
119: // constants ... here and throughout the rest of the DP code.
120: //
121:
122: public DPProvider getProvider() {
123: String name = getProviderName();
124: return getProvider(name);
125: }
126:
127: public DPProvider getProvider(String name) {
128: DPRoot root = getRoot();
129: return root.getProvider(name);
130: }
131:
132: public DPChannel getChannel(String name) {
133: //
134: // a channel cannot contain channels.
135: // only containers do.
136: //
137: if (getType() != CONTAINER_DP) {
138: return null;
139: } else {
140: return super .getChannel(name);
141: }
142: }
143:
144: public DPChannel copy(DPRoot dpr, boolean deep) {
145: return copy(dpr, deep, null);
146: }
147:
148: public DPChannel copy(DPRoot dpr, boolean deep, String newName) {
149: XMLDPRoot xmldpr = (XMLDPRoot) dpr;
150:
151: Document ownerDocument = xmldpr.getElement().getOwnerDocument();
152: Element copyElement = (Element) ownerDocument.importNode(
153: getElement(), deep);
154:
155: if (!deep) {
156: //
157: // we're not copying deep, so copy empty versions of the
158: // required sub elements
159: //
160: copyElement.appendChild(ownerDocument
161: .importNode(getChildElement(getElement(),
162: PROPERTIES_TAG, null), false));
163: }
164:
165: if (newName != null) {
166: setName(copyElement, newName);
167: }
168:
169: return XMLDPFactory.getInstance().getChannel(getContext(), dpr,
170: copyElement);
171: }
172:
173: DPChannel createDummy(DPRoot dpr) {
174: DPChannel dpc = copy(dpr, false);
175:
176: dpc.setDummy(true);
177: dpc.setDefaults();
178:
179: return dpc;
180: }
181:
182: protected Element getMergedElement() {
183: XMLDPProperties xp = (XMLDPProperties) getProperties();
184: Element e = createElement(getContext(), getDocument(), super
185: .getName(), getProviderName(), xp.getMergedElement());
186: return e;
187: }
188:
189: static Element createElement(DPContext dpc, Document d,
190: String name, String providerName, Element propertiesElement) {
191: Element e = createElement(dpc, d, CHANNEL_TAG, name);
192: e.setAttribute("provider", providerName);
193:
194: e.appendChild(propertiesElement);
195:
196: setDefaultsElement(e);
197:
198: return e;
199: }
200:
201: protected void appendProviderAttr(StringBuffer b) {
202: String toSet = null;
203: String fromMergers = null;
204: boolean locked = false;
205:
206: for (int i = 0; i < getMergers().size(); i++) {
207: DPChannel dpc = (DPChannel) getMergers().get(i);
208: fromMergers = dpc.getProviderName();
209:
210: if (dpc.isLocked()) {
211: locked = true;
212: if (dpc.isRemove()) {
213: fromMergers = null;
214: }
215: break;
216: }
217: }
218:
219: if (locked) {
220: toSet = fromMergers;
221: } else {
222: String fromThis = getProviderNameFromThis();
223:
224: if (fromMergers == null) {
225: toSet = fromThis;
226: } else if (fromThis != null && fromThis.length() > 0
227: && !fromThis.equals(fromMergers)) {
228: toSet = fromThis;
229: }
230: }
231:
232: if (toSet != null && toSet.length() > 0) {
233: b.append(" " + PROVIDER_KEY + "=\"");
234: b.append(toSet);
235: b.append("\"");
236: }
237: }
238:
239: public void toXML(StringBuffer b, int indent) {
240: if (isDummy()) {
241: return;
242: }
243: indentBuffer(b, indent);
244:
245: appendStartTag(b);
246:
247: b.append(" " + NAME_KEY + "=\"").append(getChannelName())
248: .append("\"");
249:
250: appendProviderAttr(b);
251: appendMergeAttr(b);
252: appendLockAttr(b);
253: appendAdvancedAttr(b);
254:
255: b.append(">\n");
256:
257: getPropertiesFromThis().toXML(b, indent + 1);
258:
259: indentBuffer(b, indent);
260: appendEndTag(b);
261: }
262: }
|