001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.hivemind.schema.impl;
016:
017: import java.util.ArrayList;
018: import java.util.Collections;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.hivemind.ApplicationRuntimeException;
023: import org.apache.hivemind.internal.Module;
024: import org.apache.hivemind.internal.Visibility;
025: import org.apache.hivemind.parse.BaseAnnotationHolder;
026: import org.apache.hivemind.schema.AttributeModel;
027: import org.apache.hivemind.schema.ElementModel;
028: import org.apache.hivemind.schema.Schema;
029:
030: /**
031: * Implementation of {@link org.apache.hivemind.schema.Schema}.
032: *
033: * @author Howard Lewis Ship
034: */
035: public class SchemaImpl extends BaseAnnotationHolder implements Schema {
036: private List _elementModels;
037:
038: private List _shareableElementModels;
039:
040: /** @since 1.1 */
041: private Visibility _visibility = Visibility.PUBLIC;
042:
043: /** @since 1.1 */
044: private Module _module;
045:
046: /** @since 1.1 */
047: private String _id;
048:
049: /**
050: * @since 1.1
051: */
052: public String getModuleId() {
053: return _module.getModuleId();
054: }
055:
056: /**
057: * @since 1.1
058: */
059: public String getId() {
060: return _id;
061: }
062:
063: /**
064: * @since 1.1
065: */
066: public Visibility getVisibility() {
067: return _visibility;
068: }
069:
070: /** @since 1.1 */
071: public boolean visibleToModule(String moduleId) {
072: if (_visibility == Visibility.PUBLIC)
073: return true;
074:
075: return getModuleId().equals(moduleId);
076: }
077:
078: public void addElementModel(ElementModel model) {
079: if (_elementModels == null)
080: _elementModels = new ArrayList();
081:
082: _elementModels.add(model);
083: _shareableElementModels = null;
084: }
085:
086: public List getElementModel() {
087: if (_shareableElementModels == null)
088: _shareableElementModels = _elementModels == null ? Collections.EMPTY_LIST
089: : Collections.unmodifiableList(_elementModels);
090:
091: return _shareableElementModels;
092: }
093:
094: public boolean canInstancesBeKeyed() {
095: boolean emptyModel = _elementModels == null
096: || _elementModels.isEmpty();
097:
098: if (emptyModel)
099: return false;
100:
101: for (Iterator i = _elementModels.iterator(); i.hasNext();) {
102: ElementModel model = (ElementModel) i.next();
103:
104: if (model.getKeyAttribute() == null)
105: return false;
106: }
107:
108: return true;
109: }
110:
111: /**
112: * Called by the {@link org.apache.hivemind.parse.DescriptorParser} to make sure that key
113: * attributes specified by the top-level elements actually are defined.
114: */
115: public void validateKeyAttributes() {
116: if (_elementModels == null)
117: return;
118:
119: for (Iterator i = _elementModels.iterator(); i.hasNext();) {
120: ElementModel em = (ElementModel) i.next();
121:
122: String key = em.getKeyAttribute();
123:
124: if (key == null)
125: continue;
126:
127: AttributeModel keyAm = em.getAttributeModel(key);
128:
129: if (keyAm == null)
130: throw new ApplicationRuntimeException(
131: "Key attribute \'" + key + "\' of element \'"
132: + em.getElementName()
133: + "\' never declared.", em
134: .getLocation(), null);
135: }
136: }
137:
138: /**
139: * @since 1.1
140: */
141: public void setVisibility(Visibility visibility) {
142: _visibility = visibility;
143: }
144:
145: /**
146: * @since 1.1
147: */
148: public void setModule(Module module) {
149: _module = module;
150: }
151:
152: /**
153: * @since 1.1
154: */
155: public void setId(String id) {
156: _id = id;
157: }
158:
159: /** @since 1.1 */
160:
161: public Module getDefiningModule() {
162: return _module;
163: }
164: }
|