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.Element;
008: import org.w3c.dom.Document;
009: import org.w3c.dom.Text;
010:
011: import com.sun.portal.desktop.context.DPContext;
012: import com.sun.portal.desktop.encode.Encoder;
013: import com.sun.portal.desktop.dp.DPAtomic;
014: import com.sun.portal.desktop.dp.DPProperty;
015: import com.sun.portal.desktop.dp.DPRoot;
016:
017: abstract class XMLDPAtomic extends XMLDPProperty implements DPAtomic,
018: XMLDPAttrs {
019: XMLDPAtomic(DPContext dpc, DPRoot r, Element e) {
020: super (dpc, r, e);
021: }
022:
023: public Object getValue() {
024: //
025: // here, the case where this is remove, or were the merger is locked
026: // and is remove should be handled by the object that created us.
027: // such an object should not give an instance of a DP object
028: // if the merger is remove.
029: //
030: // namely, our merge == replace, and the merger's merge (if there's
031: // a merger) is also replace.
032:
033: //
034: // if there's no mergers, all we can do is get from this
035: //
036: if (getMergers().size() == 0) {
037: return getValueFromThis();
038: }
039:
040: // Iterate through the dp docs at different node and check
041: // for lock and remove condition
042: for (int i = 0; i < getMergers().size(); i++) {
043: DPProperty dpc = (DPProperty) getMergers().get(i);
044: // If the property is locked at a higher level, handle that
045: if (dpc.isLocked()) {
046: // Property is to be removed, return null
047: if (dpc.isRemove()) {
048: return null;
049: }
050: // Locked at a higher level, hence don't consider other dp
051: // docs leave the method with what you have.
052: return ((XMLDPAtomic) dpc).getValueFromThis();
053: }
054: }
055:
056: //if it is a dummy, return the last merger
057: if (isDummy()) {
058: return ((XMLDPAtomic) getLastMerger()).getValueFromThis();
059: }
060:
061: //
062: // else, merge type was replace, so return the user value
063: //
064:
065: return getValueFromThis();
066: }
067:
068: /**
069: * Make a copy of the DP property object.
070: *
071: */
072: public DPProperty copy(DPRoot dpr, boolean deep) {
073: //
074: // we must copy atomic properties deeply, to get their body content
075: // value if it exists.
076: //
077: return super .copy(dpr, true);
078: }
079:
080: protected Object getValueFromThis() {
081:
082: if (isValueBodyContent()) {
083: return getValueFromThisBodyContent();
084: } else {
085: return getValueFromThisAttribute();
086: }
087:
088: }
089:
090: protected boolean isValueBodyContent() {
091: boolean bodyContent = false;
092:
093: if (getTextNode(getElement()) != null) {
094: bodyContent = true;
095: }
096:
097: return bodyContent;
098: }
099:
100: protected Object getValueFromThisBodyContent() {
101: return getTextNode(getElement()).getData();
102: }
103:
104: protected Object getValueFromThisAttribute() {
105: return getElement().getAttribute("value");
106: }
107:
108: public Object setValue(Object val) {
109: Object old = getValue();
110:
111: Text textNode = getTextNode(getElement());
112:
113: if (textNode == null) {
114: getElement().setAttribute("value", val.toString());
115: } else {
116: textNode.setData(val.toString());
117: }
118:
119: setDummy(false);
120:
121: return old;
122: }
123:
124: protected Element getMergedElement() {
125: Element e = createElement(getContext(), getDocument(),
126: getTag(), getName(), (String) getValue());
127: return e;
128: }
129:
130: static Element createElement(DPContext dpc, Document d,
131: String tagName, String val) {
132: Element e = createElement(dpc, d, tagName, null, val);
133: return e;
134: }
135:
136: static Element createElement(DPContext dpc, Document d,
137: String tagName, String name, String val) {
138: Element e = XMLDPObject.createElement(dpc, d, tagName, name);
139:
140: //
141: // TBD(jtb): must change this to allow the value to be set
142: // in the body content, not just as an attr
143: //
144: e.setAttribute(VALUE_KEY, val);
145: setDefaultsElement(e);
146:
147: return e;
148: }
149:
150: public int getDefaultMergeType() {
151: return staticGetDefaultMergeType();
152: }
153:
154: static int staticGetDefaultMergeType() {
155: return MERGE_REPLACE;
156: }
157:
158: public void setMergeDefaults() {
159: setMergeDefaultsElement(getElement());
160: }
161:
162: public void setDefaults() {
163: setDefaultsElement(getElement());
164: }
165:
166: static void setMergeDefaultsElement(Element e) {
167: setMergeTypeElement(e, staticGetDefaultMergeType());
168: e.setAttribute(LOCK_KEY, FALSE_ATTR);
169: }
170:
171: static void setDefaultsElement(Element e) {
172: setMergeDefaultsElement(e);
173: e.setAttribute(PROPAGATE_KEY, TRUE_ATTR);
174: e.setAttribute(ADVANCED_KEY, FALSE_ATTR);
175: }
176:
177: public void appendTagValue(StringBuffer b) {
178:
179: appendStartTag(b);
180:
181: if (isNamed()) {
182: b.append(" " + NAME_KEY + "=\"").append(
183: Encoder.XML_ENCODER.encode(getName())).append("\"");
184: }
185: b.append(" " + VALUE_KEY + "=\"")
186: .append(
187: Encoder.XML_ENCODER
188: .encode((String) getValueFromThis()))
189: .append("\"");
190: }
191:
192: public void toXML(StringBuffer b, int indent) {
193: if (isDummy()) {
194: return;
195: }
196: boolean bodyContent = isValueBodyContent();
197: indentBuffer(b, indent);
198:
199: if (bodyContent) {
200: appendStartTag(b);
201: } else {
202: appendTagValue(b);
203: }
204: appendMergeAttr(b);
205: appendLockAttr(b);
206: appendAdvancedAttr(b);
207: appendPropagateAttr(b);
208: if (bodyContent) {
209: b.append(">").append(
210: Encoder.XML_ENCODER
211: .encode((String) getValueFromThis()));
212: appendEndTag(b);
213: } else {
214: b.append("/>\n");
215: }
216:
217: }
218: }
|