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:
019: /* $Id: DublinCoreModule.java 169299 2005-05-09 12:00:43Z jwkaltz $ */
020:
021: package org.apache.lenya.cms.cocoon.components.modules.input;
022:
023: import java.util.Arrays;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import org.apache.avalon.framework.configuration.Configuration;
028: import org.apache.avalon.framework.configuration.ConfigurationException;
029: import org.apache.lenya.cms.metadata.Element;
030: import org.apache.lenya.cms.metadata.MetaData;
031: import org.apache.lenya.cms.metadata.MetaDataException;
032: import org.apache.lenya.cms.metadata.MetaDataRegistry;
033: import org.apache.lenya.cms.publication.Document;
034:
035: /**
036: * <p>
037: * Input module to access meta data values. Use the name of the element as
038: * input module parameter.
039: * </p>
040: * <p>Configuration:</p>
041: * <pre>
042: * <component-instance logger="sitemap.modules.input.dublincore" name="[...]"
043: * class="org.apache.lenya.cms.cocoon.components.modules.input.MetaDataModule"
044: * namespace="[namespace URI of the element set]"/>
045: * </pre>
046: * <p>Usage examples:</p>
047: * <ul>
048: * <li><code>{dublincore:title}</code></li>
049: * <li><code>{myMetData:myElementName}</code></li>
050: * </ul>
051: */
052: public class MetaDataModule extends AbstractPageEnvelopeModule {
053:
054: /**
055: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
056: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
057: */
058: public Object getAttribute(String name, Configuration modeConf,
059: Map objectModel) throws ConfigurationException {
060: Object value;
061:
062: MetaData metaData = getCustomMetaData(objectModel);
063:
064: if (!metaData.isValidAttribute(name)) {
065: throw new ConfigurationException("The attribute [" + name
066: + "] is not supported!");
067: }
068:
069: try {
070: value = metaData.getFirstValue(name);
071: } catch (MetaDataException e) {
072: throw new ConfigurationException(
073: "Obtaining custom meta data value for [" + name
074: + "] failed: ", e);
075: }
076:
077: return value;
078: }
079:
080: /**
081: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
082: * java.util.Map)
083: */
084: public Iterator getAttributeNames(Configuration modeConf,
085: Map objectModel) throws ConfigurationException {
086:
087: MetaDataRegistry registry = null;
088: try {
089: registry = (MetaDataRegistry) this .manager
090: .lookup(MetaDataRegistry.ROLE);
091: Element[] elements = registry.getElementSet(
092: this .namespaceUri).getElements();
093: String[] keys = new String[elements.length];
094: for (int i = 0; i < keys.length; i++) {
095: keys[i] = elements[i].getName();
096: }
097: return Arrays.asList(keys).iterator();
098: } catch (Exception e) {
099: throw new ConfigurationException(e.getMessage(), e);
100: } finally {
101: this .manager.release(registry);
102: }
103: }
104:
105: /**
106: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
107: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
108: */
109: public Object[] getAttributeValues(String name,
110: Configuration modeConf, Map objectModel)
111: throws ConfigurationException {
112: Object[] values;
113: MetaData metaData = getCustomMetaData(objectModel);
114:
115: if (!metaData.isValidAttribute(name)) {
116: throw new ConfigurationException("The attribute [" + name
117: + "] is not supported!");
118: }
119:
120: try {
121: values = metaData.getValues(name);
122: } catch (MetaDataException e) {
123: throw new ConfigurationException(
124: "Obtaining custom meta data value for [" + name
125: + "] failed: ", e);
126: }
127:
128: return values;
129: }
130:
131: protected MetaData getCustomMetaData(Map objectModel)
132: throws ConfigurationException {
133: // FIXME: There seems to be no reason to pass the attribute name to get the page envelope.
134: Document document = getEnvelope(objectModel, "").getDocument();
135: if (document == null) {
136: throw new ConfigurationException(
137: "There is no document for this page envelope!");
138: }
139: MetaData metaData = null;
140: try {
141: metaData = document.getMetaData(this .namespaceUri);
142: } catch (MetaDataException e) {
143: throw new ConfigurationException(
144: "Obtaining custom meta data value for [" + document
145: + "] failed: ", e);
146: }
147: return metaData;
148: }
149:
150: private String namespaceUri;
151:
152: public void configure(Configuration conf)
153: throws ConfigurationException {
154: super .configure(conf);
155: this .namespaceUri = conf.getAttribute("namespace");
156: }
157:
158: }
|