001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ElementDeclaration.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.uwyn.rife.engine.exceptions.ElementIdInvalidException;
011: import com.uwyn.rife.engine.exceptions.EngineException;
012: import com.uwyn.rife.ioc.HierarchicalProperties;
013: import com.uwyn.rife.ioc.PropertyValue;
014: import com.uwyn.rife.resources.ResourceFinder;
015: import java.util.Stack;
016:
017: class ElementDeclaration implements Cloneable {
018: private SiteBuilder mSiteBuilder = null;
019: private ElementInfoBuilder mElementInfoBuilder = null;
020: private GroupDeclaration mGroup = null;
021: private StateStore mStateStore = null;
022:
023: private String mDeclarationName = null;
024: private String mId = null;
025: private String mUrl = null;
026: private String mInherits = null;
027: private String mPre = null;
028: private ElementInfo mElementInfo = null;
029: private HierarchicalProperties mProperties = null;
030: private Stack<ElementDeclaration> mParentStack = null;
031: private Stack<ElementDeclaration> mPreStack = null;
032:
033: ElementDeclaration(SiteBuilder siteBuilder,
034: ResourceFinder resourceFinder, GroupDeclaration group,
035: StateStore stateStore, String declarationName) {
036: mSiteBuilder = siteBuilder;
037: mElementInfoBuilder = new ElementInfoBuilder(siteBuilder,
038: resourceFinder, this );
039: setGroup(group);
040: mStateStore = stateStore;
041: mDeclarationName = declarationName;
042: mProperties = new HierarchicalProperties();
043: mParentStack = new Stack<ElementDeclaration>();
044: mPreStack = new Stack<ElementDeclaration>();
045: }
046:
047: void setId(String id) throws EngineException {
048: if (null == id) {
049: id = SiteBuilder.generateId(mDeclarationName);
050: }
051:
052: if (null == id) {
053: mId = null;
054: return;
055: }
056:
057: if (id.startsWith(".") || id.endsWith(".")
058: || id.indexOf("..") != -1 || id.indexOf("^") != -1
059: || id.indexOf(":") != -1) {
060: throw new ElementIdInvalidException(id);
061: }
062:
063: mId = id;
064: }
065:
066: void setDeclarationName(String declarationName) {
067: mDeclarationName = declarationName;
068: }
069:
070: void setUrl(String url) {
071: mUrl = url;
072: }
073:
074: void setInherits(String inherits) {
075: mInherits = inherits;
076: }
077:
078: void setPre(String pre) {
079: mPre = pre;
080: }
081:
082: SiteBuilder getSiteBuilder() {
083: return mSiteBuilder;
084: }
085:
086: ElementInfoBuilder getElementInfoBuilder() {
087: return mElementInfoBuilder;
088: }
089:
090: void setGroup(GroupDeclaration group) {
091: if (mGroup != null) {
092: mGroup.removeElementDeclaration(this );
093: }
094:
095: if (group != null) {
096: group.addElementDeclaration(this );
097: }
098:
099: mGroup = group;
100: }
101:
102: GroupDeclaration getGroup() {
103: return mGroup;
104: }
105:
106: String getId() {
107: if (null == mId) {
108: setId(null);
109: }
110:
111: return mId;
112: }
113:
114: boolean hasDeclaredId() {
115: return mId != null;
116: }
117:
118: void setStateStore(StateStore stateStore) {
119: mStateStore = stateStore;
120: }
121:
122: StateStore getStateStore() {
123: return mStateStore;
124: }
125:
126: String getUrl() {
127: return mUrl;
128: }
129:
130: boolean hasDeclaredUrl() {
131: return mUrl != null;
132: }
133:
134: String getDeclarationName() {
135: if (null == mDeclarationName) {
136: // Try to generate a declaration name if the element id has been provided.
137: // A missing declaration name can mean two things:
138: // * the element is totally declared manually, or
139: // * the declaration is included in the element implementation as annotations
140: if (mId != null) {
141: if (AnnotationsElementDetector
142: .hasElementAnnotation(getElementInfoBuilder()
143: .getImplementation())) {
144: mDeclarationName = ElementInfoProcessorFactory.ANNOTATIONS_IDENTIFIER
145: + ":"
146: + getElementInfoBuilder()
147: .getImplementation();
148: } else {
149: mDeclarationName = ElementInfoProcessorFactory.MANUAL_IDENTIFIER
150: + ":" + mId;
151: }
152: }
153: }
154:
155: return mDeclarationName;
156: }
157:
158: String getInherits() {
159: return mInherits;
160: }
161:
162: String getPre() {
163: return mPre;
164: }
165:
166: void setElementInfo(ElementInfo elementInfo) {
167: mElementInfo = elementInfo;
168: }
169:
170: ElementInfo getElementInfo() {
171: return mElementInfo;
172: }
173:
174: void addProperty(String name, PropertyValue value) {
175: mProperties.put(name, value);
176: }
177:
178: public boolean hasProperty(String name) {
179: if (null == mProperties) {
180: return false;
181: }
182:
183: return mProperties.contains(name);
184: }
185:
186: HierarchicalProperties getProperties() {
187: return mProperties;
188: }
189:
190: Stack<ElementDeclaration> getParentStack() {
191: return mParentStack;
192: }
193:
194: Stack<ElementDeclaration> getPreStack() {
195: return mPreStack;
196: }
197:
198: public synchronized ElementDeclaration clone() {
199: ElementDeclaration new_elementdeclaration = null;
200: try {
201: new_elementdeclaration = (ElementDeclaration) super .clone();
202: if (mElementInfo != null) {
203: new_elementdeclaration.mElementInfo = mElementInfo
204: .clone();
205: if (mParentStack != null) {
206: new_elementdeclaration.mParentStack = new Stack<ElementDeclaration>();
207: new_elementdeclaration.mParentStack
208: .addAll(mParentStack);
209: }
210: if (mPreStack != null) {
211: new_elementdeclaration.mPreStack = new Stack<ElementDeclaration>();
212: new_elementdeclaration.mPreStack.addAll(mPreStack);
213: }
214: if (mProperties != null) {
215: new_elementdeclaration.mProperties = new HierarchicalProperties();
216: new_elementdeclaration.mProperties
217: .putAll(mProperties);
218: }
219: }
220: } catch (CloneNotSupportedException e) {
221: new_elementdeclaration = null;
222: }
223:
224: return new_elementdeclaration;
225: }
226: }
|