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: package org.apache.cocoon.portal.tools.transformation;
018:
019: import java.io.IOException;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.activity.Disposable;
025: import org.apache.avalon.framework.parameters.ParameterException;
026: import org.apache.avalon.framework.parameters.Parameters;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.cocoon.ProcessingException;
030: import org.apache.cocoon.environment.SourceResolver;
031: import org.apache.cocoon.portal.tools.PortalTool;
032: import org.apache.cocoon.portal.tools.PortalToolFunction;
033: import org.apache.cocoon.portal.tools.PortalToolManager;
034: import org.apache.cocoon.transformation.AbstractSAXTransformer;
035: import org.apache.cocoon.xml.AttributesImpl;
036: import org.xml.sax.SAXException;
037:
038: /**
039: * Adds the navigation etc. to the document
040: *
041: * @version $Id: PortalToolsLayoutTransformer.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public class PortalToolsLayoutTransformer extends
044: AbstractSAXTransformer implements Disposable /*, Parameterizable */{
045:
046: public static final String ROLE = PortalToolsLayoutTransformer.class
047: .getName();
048:
049: private PortalToolManager pm;
050: private String selected;
051:
052: /* (non-Javadoc)
053: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
054: */
055: public void service(ServiceManager manager) throws ServiceException {
056: super .service(manager);
057: pm = (PortalToolManager) manager.lookup(PortalToolManager.ROLE);
058: }
059:
060: /* (non-Javadoc)
061: * @see org.apache.cocoon.transformation.AbstractSAXTransformer#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
062: */
063: public void setup(SourceResolver resolver, Map objectModel,
064: String src, Parameters par) throws ProcessingException,
065: SAXException, IOException {
066: super .setup(resolver, objectModel, src, par);
067: try {
068: selected = par.getParameter("selected");
069: } catch (ParameterException e) {
070: // does not matter, default handling
071: }
072: }
073:
074: /* (non-Javadoc)
075: * @see org.apache.avalon.framework.activity.Disposable#dispose()
076: */
077: public void dispose() {
078: if (this .manager != null) {
079: this .manager.release(pm);
080: this .pm = null;
081: }
082: super .dispose();
083: }
084:
085: /* (non-Javadoc)
086: * @see org.xml.sax.ContentHandler#startDocument()
087: */
088: public void startDocument() throws SAXException {
089: super .startDocument();
090: AttributesImpl a = new AttributesImpl();
091:
092: // took the div-tag as root, because it does not matter in the output, if it passes the xsl transformation
093: super .startPrefixMapping("i18n",
094: "http://apache.org/cocoon/i18n/2.1");
095: super .startElement("", "div", "div", a);
096: super .startElement("", "tab-layout", "tab-layout", a);
097: Collection tools = pm.getToolsWithFunctions();
098: for (Iterator it = tools.iterator(); it.hasNext();) {
099: AttributesImpl attr = new AttributesImpl();
100: PortalTool pt = (PortalTool) it.next();
101: attr.addCDATAAttribute("parameter", "tools/functions/"
102: + pt.getId());
103: attr.addCDATAAttribute("name", pt.getName());
104: attr.addCDATAAttribute("http://apache.org/cocoon/i18n/2.1",
105: "attr", "i18n:attr", "name");
106: if (selected != null) {
107: if (selected.equals(pt.getId())) {
108: attr.addCDATAAttribute("selected", "true");
109: }
110: }
111: super .startElement("", "named-item", "named-item", attr);
112: super .endElement("", "named-item", "named-item");
113: }
114: super .endElement("", "tab-layout", "tab-layout");
115: if (selected != null) {
116: PortalTool ct = pm.getTool(selected);
117: if (ct != null) {
118: super .startElement("", "tool-functions",
119: "tool-functions", a);
120: Collection funs = ct.getFunctions();
121: for (Iterator it = funs.iterator(); it.hasNext();) {
122: PortalToolFunction ptf = (PortalToolFunction) it
123: .next();
124: AttributesImpl attr = new AttributesImpl();
125: attr.addCDATAAttribute("parameter",
126: "tools/plugins/" + ct.getId() + "/"
127: + ptf.getFunction());
128: attr.addCDATAAttribute("name", ptf.getName());
129: attr.addCDATAAttribute(
130: "http://apache.org/cocoon/i18n/2.1",
131: "attr", "i18n:attr", "name");
132: super
133: .startElement("", "function", "function",
134: attr);
135: super .endElement("", "function", "function");
136: }
137: super
138: .endElement("", "tool-functions",
139: "tool-functions");
140: }
141: }
142: }
143:
144: /* (non-Javadoc)
145: * @see org.xml.sax.ContentHandler#endDocument()
146: */
147: public void endDocument() throws SAXException {
148: super .endElement("", "div", "div");
149: super .endPrefixMapping("i18n");
150: selected = null;
151: super.endDocument();
152: }
153:
154: }
|