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 java.util.Iterator;
010: import java.util.List;
011: import java.util.Set;
012:
013: import com.sun.portal.desktop.context.DPContext;
014:
015: import com.sun.portal.desktop.dp.DPContainerChannel;
016: import com.sun.portal.desktop.dp.DPChannel;
017: import com.sun.portal.desktop.dp.DPAvailable;
018: import com.sun.portal.desktop.dp.DPSelected;
019: import com.sun.portal.desktop.dp.DPTypes;
020: import com.sun.portal.desktop.dp.DPRoot;
021: import com.sun.portal.desktop.dp.DPError;
022:
023: public class XMLDPContainerChannel extends XMLDPChannel implements
024: DPContainerChannel, DPTypes, XMLDPTags {
025:
026: private Element selectedElement = null;
027: private Element availableElement = null;
028:
029: XMLDPContainerChannel(DPContext dpc, DPRoot r, Document d,
030: String name, String providerName) {
031: this (dpc, r, createElement(dpc, d, name, providerName,
032: XMLDPProperties.createElement(dpc, d)));
033: }
034:
035: XMLDPContainerChannel(DPContext dpc, DPRoot r, Element e) {
036: super (dpc, r, e);
037: }
038:
039: public short getType() {
040: return CONTAINER_DP;
041: }
042:
043: public String getTag() {
044: return CONTAINER_TAG;
045: }
046:
047: public DPChannel copy(DPRoot dpr, boolean deep) {
048: return copy(dpr, deep, null);
049: }
050:
051: public DPChannel copy(DPRoot dpr, boolean deep, String newName) {
052: XMLDPRoot xmldpr = (XMLDPRoot) dpr;
053:
054: Document ownerDocument = xmldpr.getElement().getOwnerDocument();
055: Element copyElement = (Element) ownerDocument.importNode(
056: getElement(), deep);
057:
058: if (!deep) {
059: //
060: // we're not copying deep, so copy empty versions of the
061: // required sub elements
062: //
063: copyElement.appendChild(ownerDocument
064: .importNode(getChildElement(getElement(),
065: PROPERTIES_TAG, null), false));
066: copyElement.appendChild(ownerDocument.importNode(
067: getChildElement(getElement(), AVAILABLE_TAG, null),
068: false));
069: copyElement.appendChild(ownerDocument.importNode(
070: getChildElement(getElement(), SELECTED_TAG, null),
071: false));
072: copyElement.appendChild(ownerDocument.importNode(
073: getChildElement(getElement(), CHANNELS_TAG, null),
074: false));
075: }
076:
077: if (newName != null) {
078: setName(copyElement, newName);
079: }
080:
081: return XMLDPFactory.getInstance().getChannel(getContext(), dpr,
082: copyElement);
083: }
084:
085: Element getAvailableElement() {
086: if (availableElement == null) {
087: availableElement = getChildElement(getElement(),
088: AVAILABLE_TAG, null);
089: if (availableElement == null) {
090: throw new DPError(
091: "XMLDPContainerChannel.getAvailableElement(): no "
092: + AVAILABLE_TAG + " child found");
093: }
094: }
095:
096: return availableElement;
097: }
098:
099: public DPAvailable getAvailableFromThis() {
100: Element a = getAvailableElement();
101: DPAvailable dpAvailable = (DPAvailable) getObject(a);
102: if (dpAvailable == null) {
103: dpAvailable = XMLDPFactory.getInstance().getAvailable(
104: getContext(), getRoot(), a);
105: putObject(dpAvailable);
106: }
107:
108: return dpAvailable;
109: }
110:
111: protected DPAvailable getAvailableFromThis(Element a) {
112: DPAvailable dpAvailable = null;
113:
114: if (a == null) {
115: throw new DPError(
116: "XMLDPContainerChannel.getAvailableFromThis(): no "
117: + AVAILABLE_TAG + " child found");
118: }
119:
120: dpAvailable = XMLDPFactory.getInstance().getAvailable(
121: getContext(), getRoot(), a);
122: return dpAvailable;
123: }
124:
125: public DPSelected getSelectedFromThis() {
126: Element s = getSelectedElement();
127: DPSelected dpSelected = (DPSelected) getObject(s);
128: if (dpSelected == null) {
129: dpSelected = XMLDPFactory.getInstance().getSelected(
130: getContext(), getRoot(), s);
131: putObject(dpSelected);
132: }
133:
134: return dpSelected;
135: }
136:
137: Element getSelectedElement() {
138: if (selectedElement == null) {
139: selectedElement = getChildElement(getElement(),
140: SELECTED_TAG, null);
141: if (selectedElement == null) {
142: throw new DPError(
143: "XMLDPContainerChannel.getSelectedElement(): no "
144: + SELECTED_TAG + " child found");
145: }
146: }
147:
148: return selectedElement;
149: }
150:
151: public DPAvailable getAvailable() {
152: DPAvailable dpAvailable = getAvailableFromThis();
153:
154: if (dpAvailable != null && dpAvailable.isMerged()) {
155: // nothing
156: } else {
157: for (int i = 0; i < getMergers().size(); i++) {
158: DPContainerChannel dpcc = (DPContainerChannel) getMergers()
159: .get(i);
160: DPAvailable dpa = ((XMLDPContainerChannel) dpcc)
161: .getAvailableFromThis();
162:
163: dpAvailable.addMerger(dpa);
164: }
165: }
166:
167: return dpAvailable;
168: }
169:
170: public void setAvailable(List avail) {
171: getAvailable().setCollectionValue(avail);
172: }
173:
174: public DPSelected getSelected() {
175: DPSelected dpSelected = getSelectedFromThis();
176:
177: if (dpSelected != null && dpSelected.isMerged()) {
178: // nothing
179: } else {
180: for (int i = 0; i < getMergers().size(); i++) {
181: DPContainerChannel dpcc = (DPContainerChannel) getMergers()
182: .get(i);
183: DPSelected dps = ((XMLDPContainerChannel) dpcc)
184: .getSelectedFromThis();
185:
186: dpSelected.addMerger(dps);
187: }
188: }
189:
190: return dpSelected;
191: }
192:
193: public void setSelected(List set) {
194: getSelected().setCollectionValue(set);
195: }
196:
197: DPChannel createDummy(DPRoot dpr) {
198: DPContainerChannel dpc = (DPContainerChannel) super
199: .createDummy(dpr);
200:
201: dpc.setDefaults();
202:
203: return dpc;
204: }
205:
206: protected Element getMergedElement() {
207: XMLDPProperties xp = (XMLDPProperties) getProperties();
208: Element e = createElement(getContext(), getDocument(),
209: getName(), getProviderName(), xp.getMergedElement());
210: //
211: // add available
212: //
213: Element ae = getChildElement(e, AVAILABLE_TAG);
214: XMLDPAvailable xa = (XMLDPAvailable) getAvailable();
215: e.replaceChild(xa.getMergedElement(), ae);
216:
217: //
218: // add selected
219: //
220: Element se = getChildElement(e, SELECTED_TAG);
221: XMLDPSelected xs = (XMLDPSelected) getSelected();
222: e.replaceChild(xs.getMergedElement(), se);
223:
224: //
225: // add channels
226: //
227: Element cse = getChildElement(e, CHANNELS_TAG);
228: Set names = getChannelNames();
229: for (Iterator i = names.iterator(); i.hasNext();) {
230: String name = (String) i.next();
231: XMLDPChannel xc = (XMLDPChannel) getChannel(name);
232: Element ce = xc.getMergedElement();
233: cse.appendChild(ce);
234: }
235:
236: return e;
237: }
238:
239: static Element createElement(DPContext dpc, Document d,
240: String name, String providerName, Element propertiesElement) {
241: Element e = createElement(dpc, d, CONTAINER_TAG, name);
242: e.setAttribute("provider", providerName);
243:
244: //
245: // set empty properties
246: //
247: e.appendChild(propertiesElement);
248:
249: //
250: // add available
251: //
252: Element ae = XMLDPAvailable.createElement(dpc, d);
253: e.appendChild(ae);
254:
255: //
256: // add selected
257: //
258: Element se = XMLDPSelected.createElement(dpc, d);
259: e.appendChild(se);
260:
261: //
262: // add "channels" bag
263: //
264: Element channelsElement = d.createElement(CHANNELS_TAG);
265: e.appendChild(channelsElement);
266:
267: setDefaultsElement(e);
268:
269: return e;
270: }
271:
272: public void setMergeDefaults() {
273: setMergeDefaultsElement(getElement());
274: }
275:
276: public void setDefaults() {
277: setDefaultsElement(getElement());
278: }
279:
280: static void setMergeDefaultsElement(Element e) {
281: setMergeTypeElement(e, staticGetDefaultMergeType());
282: e.setAttribute(LOCK_KEY, FALSE_ATTR);
283:
284: Element pe = getChildElement(e, PROPERTIES_TAG, null);
285: XMLDPProperties.setMergeDefaultsElement(pe);
286: Element ae = getChildElement(e, AVAILABLE_TAG, null);
287: XMLDPAvailable.setMergeDefaultsElement(ae);
288: Element se = getChildElement(e, SELECTED_TAG, null);
289: XMLDPSelected.setMergeDefaultsElement(se);
290: }
291:
292: static void setDefaultsElement(Element e) {
293: setMergeDefaultsElement(e);
294: e.setAttribute(ADVANCED_KEY, FALSE_ATTR);
295:
296: Element pe = getChildElement(e, PROPERTIES_TAG, null);
297: XMLDPProperties.setDefaultsElement(pe);
298: Element ae = getChildElement(e, AVAILABLE_TAG, null);
299: XMLDPAvailable.setDefaultsElement(ae);
300: Element se = getChildElement(e, SELECTED_TAG, null);
301: XMLDPSelected.setDefaultsElement(se);
302: }
303:
304: public void toXML(StringBuffer b, int indent) {
305: if (isDummy()) {
306: return;
307: }
308:
309: indentBuffer(b, indent);
310: appendStartTag(b);
311: b.append(" " + NAME_KEY + "=\"").append(getChannelName())
312: .append("\"");
313:
314: appendProviderAttr(b);
315: appendMergeAttr(b);
316: appendLockAttr(b);
317: appendAdvancedAttr(b);
318: b.append(">\n");
319:
320: getPropertiesFromThis().toXML(b, indent + 1);
321: getAvailableFromThis().toXML(b, indent + 1);
322: getSelectedFromThis().toXML(b, indent + 1);
323:
324: // Add Channels
325: indentBuffer(b, indent + 1);
326:
327: appendChannels(b, indent);
328:
329: indentBuffer(b, indent);
330: appendEndTag(b);
331: }
332: }
|