01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.portal.components.modules.input;
18:
19: import java.util.Map;
20:
21: import org.apache.avalon.framework.configuration.Configuration;
22: import org.apache.avalon.framework.configuration.ConfigurationException;
23: import org.apache.avalon.framework.service.ServiceException;
24: import org.apache.cocoon.portal.PortalService;
25: import org.apache.commons.jxpath.JXPathContext;
26:
27: /**
28: * This input module gives access to information stored in a layout object
29: * by using JXPathExpressions.
30: * The syntax to use is LAYOUT_ID/PATH or LAYOUT_KEY:LAYOUT_ID/PATH
31: *
32: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
33: * @version CVS $Id: LayoutModule.java 433543 2006-08-22 06:22:54Z crossley $
34: */
35: public class LayoutModule extends AbstractModule {
36:
37: /* (non-Javadoc)
38: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
39: */
40: public Object getAttribute(String name, Configuration modeConf,
41: Map objectModel) throws ConfigurationException {
42: PortalService portalService = null;
43: try {
44:
45: portalService = (PortalService) this .manager
46: .lookup(PortalService.ROLE);
47:
48: int pos = name.indexOf('/');
49: String path;
50: if (pos == -1) {
51: path = null;
52: } else {
53: path = name.substring(pos + 1);
54: name = name.substring(0, pos);
55: }
56: // is the layout key specified?
57: pos = name.indexOf(':');
58: String layoutKey = null;
59: String layoutId = name;
60: if (pos != -1) {
61: layoutKey = name.substring(0, pos);
62: layoutId = name.substring(pos + 1);
63: }
64:
65: // get the layout
66: final Object layout = portalService.getComponentManager()
67: .getProfileManager().getPortalLayout(layoutKey,
68: layoutId);
69: Object value = layout;
70: if (layout != null && path != null) {
71: final JXPathContext jxpathContext = JXPathContext
72: .newContext(layout);
73: value = jxpathContext.getValue(path);
74: }
75: return value;
76: } catch (ServiceException e) {
77: throw new ConfigurationException(
78: "Unable to lookup portal service.", e);
79: } finally {
80: this.manager.release(portalService);
81: }
82: }
83: }
|