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 { 037 private List _elementModels; 038 039 private List _shareableElementModels; 040 041 /** @since 1.1 */ 042 private Visibility _visibility = Visibility.PUBLIC; 043 044 /** @since 1.1 */ 045 private Module _module; 046 047 /** @since 1.1 */ 048 private String _id; 049 050 /** 051 * @since 1.1 052 */ 053 public String getModuleId() 054 { 055 return _module.getModuleId(); 056 } 057 058 /** 059 * @since 1.1 060 */ 061 public String getId() 062 { 063 return _id; 064 } 065 066 /** 067 * @since 1.1 068 */ 069 public Visibility getVisibility() 070 { 071 return _visibility; 072 } 073 074 /** @since 1.1 */ 075 public boolean visibleToModule(String moduleId) 076 { 077 if (_visibility == Visibility.PUBLIC) 078 return true; 079 080 return getModuleId().equals(moduleId); 081 } 082 083 public void addElementModel(ElementModel model) 084 { 085 if (_elementModels == null) 086 _elementModels = new ArrayList(); 087 088 _elementModels.add(model); 089 _shareableElementModels = null; 090 } 091 092 public List getElementModel() 093 { 094 if (_shareableElementModels == null) 095 _shareableElementModels = _elementModels == null ? Collections.EMPTY_LIST : Collections 096 .unmodifiableList(_elementModels); 097 098 return _shareableElementModels; 099 } 100 101 public boolean canInstancesBeKeyed() 102 { 103 boolean emptyModel = _elementModels == null || _elementModels.isEmpty(); 104 105 if (emptyModel) 106 return false; 107 108 for (Iterator i = _elementModels.iterator(); i.hasNext();) 109 { 110 ElementModel model = (ElementModel) i.next(); 111 112 if (model.getKeyAttribute() == null) 113 return false; 114 } 115 116 return true; 117 } 118 119 /** 120 * Called by the {@link org.apache.hivemind.parse.DescriptorParser} to make sure that key 121 * attributes specified by the top-level elements actually are defined. 122 */ 123 public void validateKeyAttributes() 124 { 125 if (_elementModels == null) 126 return; 127 128 for (Iterator i = _elementModels.iterator(); i.hasNext();) 129 { 130 ElementModel em = (ElementModel) i.next(); 131 132 String key = em.getKeyAttribute(); 133 134 if (key == null) 135 continue; 136 137 AttributeModel keyAm = em.getAttributeModel(key); 138 139 if (keyAm == null) 140 throw new ApplicationRuntimeException("Key attribute \'" + key + "\' of element \'" 141 + em.getElementName() + "\' never declared.", em.getLocation(), null); 142 } 143 } 144 145 /** 146 * @since 1.1 147 */ 148 public void setVisibility(Visibility visibility) 149 { 150 _visibility = visibility; 151 } 152 153 /** 154 * @since 1.1 155 */ 156 public void setModule(Module module) 157 { 158 _module = module; 159 } 160 161 /** 162 * @since 1.1 163 */ 164 public void setId(String id) 165 { 166 _id = id; 167 } 168 169 /** @since 1.1 */ 170 171 public Module getDefiningModule() 172 { 173 return _module; 174 } 175 }