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.Node;
009: import org.w3c.dom.Document;
010: import java.util.Map;
011:
012: import com.sun.portal.desktop.context.DPContext;
013:
014: import com.sun.portal.desktop.dp.DPProperty;
015: import com.sun.portal.desktop.dp.DPRoot;
016: import com.sun.portal.desktop.dp.DPProperties;
017: import com.sun.portal.desktop.dp.DPError;
018:
019: abstract class XMLDPProperty extends XMLDPObject implements DPProperty,
020: XMLDPTags, Map.Entry {
021:
022: static long ccount = 0;
023:
024: private Element propertiesElement = null;
025:
026: XMLDPProperty(DPContext dpc, DPRoot r, Element e) {
027: super (dpc, r, e);
028: }
029:
030: public void setPropagate(boolean state) {
031: setPropagateElement(getElement(), state);
032: }
033:
034: Element getPropertiesElement() {
035: if (propertiesElement == null) {
036: propertiesElement = getPropertiesElement(getElement());
037: }
038:
039: return propertiesElement;
040: }
041:
042: static Element getPropertiesElement(Element e) {
043: if (e.getTagName().equals(PROPERTIES_TAG)) {
044: return e;
045: }
046:
047: Node n = e.getParentNode();
048: if (n == null) {
049: throw new DPError(
050: "XMLDPProperty.getPropertiesElement(): parent node was null");
051: }
052:
053: e = (Element) n;
054:
055: return getPropertiesElement(e);
056: }
057:
058: protected DPProperties getPropertiesFromThis() {
059: Element e = getPropertiesElement();
060: DPProperties dpp = (DPProperties) getObject(e);
061: if (dpp == null) {
062: dpp = XMLDPFactory.getInstance().getProperties(
063: getContext(), getRoot(), e);
064: putObject(dpp);
065: }
066:
067: return dpp;
068: }
069:
070: public DPProperties getProperties() {
071: DPProperties dpProperties = getPropertiesFromThis();
072:
073: if (dpProperties != null && dpProperties.isMerged()) {
074: // nothing
075: } else {
076: for (int i = 0; i < getMergers().size(); i++) {
077: DPProperty dpp = (DPProperty) getMergers().get(i);
078: DPProperties dppp = ((XMLDPProperty) dpp)
079: .getPropertiesFromThis();
080:
081: dpProperties.addMerger(dppp);
082: }
083: }
084:
085: return dpProperties;
086: }
087:
088: static void setPropagateElement(Element e, boolean state) {
089: String s = null;
090: if (state) {
091: s = Boolean.TRUE.toString();
092: } else {
093: s = Boolean.FALSE.toString();
094: }
095: e.setAttribute(PROPAGATE_KEY, s);
096: }
097:
098: public boolean isPropagate() {
099: if (!isDummy() && isThisPropagate()) {
100: return true;
101: }
102:
103: //
104: // for this, sealed is false, check mergers
105: //
106:
107: for (int i = 0; i < getMergers().size(); i++) {
108: DPProperty mergerDPP = (DPProperty) getMergers().get(i);
109: if (((XMLDPProperty) mergerDPP).isThisPropagate()) {
110: return true;
111: }
112: }
113:
114: //
115: // none of the mergers was true, so return false
116: //
117:
118: return false;
119: }
120:
121: public boolean isThisPropagate() {
122: //
123: // the default is true. the algorithm here is we search up from the
124: // property until either:
125: //
126: // 1. we find a place where propagate=false
127: // 2. we reach the properties node
128: //
129: // when #2 happens, we return true, because that's the default
130: //
131:
132: Element e = getElement();
133: if (!isPropagateElement(e)) {
134: return false;
135: }
136:
137: if (e.getTagName().equals(PROPERTIES_TAG)) {
138: //
139: // we reached the properties tag, and propagate was true.
140: // end search.
141: //
142: return true;
143: }
144:
145: Node parentNode = e.getParentNode();
146: if (parentNode.getNodeType() != Node.ELEMENT_NODE) {
147: throw new DPError(
148: "XMLDPProperty.isPropagate(): property's parent was not an element");
149: }
150: Element parentElement = (Element) parentNode;
151:
152: return isPropagateElement(parentElement);
153: }
154:
155: public static boolean isPropagateElement(Element e) {
156: String p = e.getAttribute(PROPAGATE_KEY);
157: if ((p == null) || (p.length() == 0)) {
158: return staticGetDefaultPropagateValue();
159: }
160: boolean propagate = Boolean.valueOf(p).booleanValue();
161: return propagate;
162: }
163:
164: public DPProperty copy(DPRoot dpr, boolean deep) {
165: Element elementCopy = copyElement(dpr, deep);
166: return XMLDPFactory.getInstance().getProperty(getContext(),
167: dpr, elementCopy);
168: }
169:
170: Element copyElement(DPRoot dpr, boolean deep) {
171: XMLDPRoot xmldpr = (XMLDPRoot) dpr;
172: Document ownerDocument = xmldpr.getElement().getOwnerDocument();
173: Element elementCopy = (Element) ownerDocument.importNode(
174: getElement(), deep);
175:
176: return elementCopy;
177: }
178:
179: public DPProperty createDummy(DPRoot dpr) {
180: DPProperty dpp = copy(dpr, false);
181:
182: dpp.setDummy(true);
183: dpp.setDefaults();
184:
185: return dpp;
186: }
187:
188: public boolean isNamed() {
189: if (getElement().hasAttribute(NAME_KEY)) {
190: return true;
191: }
192: return false;
193: }
194:
195: /// Map.Entry intrface impl ////////////////////////////
196:
197: public Object getKey() {
198: return getName();
199: }
200:
201: public void appendPropagateAttr(StringBuffer b) {
202: if (isPropagateElement(getElement()) != getDefaultPropagateValue()) {
203: b.append(" " + PROPAGATE_KEY + "=\"").append(
204: isThisPropagate()).append("\"");
205: }
206: }
207:
208: }
|