001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.metadata;
019:
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.activity.Initializable;
025: import org.apache.avalon.framework.configuration.Configurable;
026: import org.apache.avalon.framework.configuration.Configuration;
027: import org.apache.avalon.framework.configuration.ConfigurationException;
028: import org.apache.avalon.framework.logger.AbstractLogEnabled;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceManager;
031: import org.apache.avalon.framework.service.Serviceable;
032: import org.apache.avalon.framework.thread.ThreadSafe;
033:
034: /**
035: * Avalon-based element set.
036: */
037: public class ConfigurableElementSet extends AbstractLogEnabled
038: implements ElementSet, Configurable, ThreadSafe, Initializable,
039: Serviceable {
040:
041: private String namespaceUri;
042: private Map elements = new HashMap();
043:
044: public void configure(Configuration config)
045: throws ConfigurationException {
046:
047: this .namespaceUri = config.getAttribute("name");
048:
049: Configuration[] attributeConfigs = config
050: .getChildren("element");
051: for (int i = 0; i < attributeConfigs.length; i++) {
052: String name = attributeConfigs[i].getAttribute("name");
053: boolean isMultiple = attributeConfigs[i]
054: .getAttributeAsBoolean("multiple", false);
055: boolean isEditable = attributeConfigs[i]
056: .getAttributeAsBoolean("editable", false);
057: String actionOnCopy = attributeConfigs[i].getAttribute(
058: "onCopy", "copy");
059: ElementImpl element = new ElementImpl(name, isMultiple,
060: isEditable);
061: int action;
062: if (actionOnCopy.equalsIgnoreCase("copy")) {
063: action = Element.ONCOPY_COPY;
064: } else if (actionOnCopy.equalsIgnoreCase("ignore")) {
065: action = Element.ONCOPY_IGNORE;
066: } else if (actionOnCopy.equalsIgnoreCase("delete")) {
067: action = Element.ONCOPY_DELETE;
068: } else {
069: throw new ConfigurationException("The action ["
070: + actionOnCopy + "] is not supported.");
071: }
072: try {
073: element.setActionOnCopy(action);
074: } catch (MetaDataException e) {
075: throw new RuntimeException(e);
076: }
077: this .elements.put(name, element);
078: }
079:
080: }
081:
082: public Element[] getElements() {
083: Collection values = this .elements.values();
084: return (Element[]) values.toArray(new Element[values.size()]);
085: }
086:
087: public Element getElement(String name) {
088: return (Element) this .elements.get(name);
089: }
090:
091: public String getNamespaceUri() {
092: return this .namespaceUri;
093: }
094:
095: public boolean containsElement(String name) {
096: return this .elements.keySet().contains(name);
097: }
098:
099: public void initialize() throws Exception {
100: MetaDataRegistry registry = null;
101: try {
102: registry = (MetaDataRegistry) this .manager
103: .lookup(MetaDataRegistry.ROLE);
104: registry.register(getNamespaceUri(), this );
105: } finally {
106: if (registry != null) {
107: this .manager.release(registry);
108: }
109: }
110: }
111:
112: private ServiceManager manager;
113:
114: public void service(ServiceManager manager) throws ServiceException {
115: this.manager = manager;
116: }
117:
118: }
|