KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > tools > transformation > PortalToolsLayoutTransformer


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.tools.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.parameters.ParameterException;
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.cocoon.ProcessingException;
29 import org.apache.cocoon.environment.SourceResolver;
30 import org.apache.cocoon.portal.tools.PortalTool;
31 import org.apache.cocoon.portal.tools.PortalToolFunction;
32 import org.apache.cocoon.portal.tools.PortalToolManager;
33 import org.apache.cocoon.transformation.AbstractSAXTransformer;
34 import org.apache.cocoon.xml.AttributesImpl;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /**
38  * Adds the navigation etc. to the document
39  *
40  * @version $Id: PortalToolsLayoutTransformer.java 168366 2005-05-05 18:23:18Z vgritsenko $
41  */

42 public class PortalToolsLayoutTransformer extends AbstractSAXTransformer
43                                           implements Disposable /*, Parameterizable */ {
44
45     public static final String JavaDoc ROLE = PortalToolsLayoutTransformer.class.getName();
46
47     private PortalToolManager pm;
48     private String JavaDoc selected;
49
50     /* (non-Javadoc)
51      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
52      */

53     public void service(ServiceManager manager) throws ServiceException {
54         super.service(manager);
55         pm = (PortalToolManager) manager.lookup(PortalToolManager.ROLE);
56     }
57
58     /* (non-Javadoc)
59      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
60      */

61     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src,
62             Parameters par) throws ProcessingException, SAXException JavaDoc,
63             IOException JavaDoc {
64         super.setup(resolver, objectModel, src, par);
65         try {
66             selected = par.getParameter("selected");
67         } catch (ParameterException e) {
68             // does not matter, default handling
69
}
70     }
71
72     /* (non-Javadoc)
73      * @see org.apache.avalon.framework.activity.Disposable#dispose()
74      */

75     public void dispose() {
76         if (this.manager != null) {
77             this.manager.release(pm);
78             this.pm = null;
79         }
80         super.dispose();
81     }
82
83
84     /* (non-Javadoc)
85      * @see org.xml.sax.ContentHandler#startDocument()
86      */

87     public void startDocument() throws SAXException JavaDoc {
88         super.startDocument();
89         AttributesImpl a = new AttributesImpl();
90
91         // took the div-tag as root, because it does not matter in the output, if it passes the xsl transformation
92
super.startPrefixMapping("i18n", "http://apache.org/cocoon/i18n/2.1");
93         super.startElement("", "div","div", a);
94         super.startElement("", "tab-layout", "tab-layout", a);
95             Collection JavaDoc tools = pm.getToolsWithFunctions();
96             for(Iterator JavaDoc it = tools.iterator(); it.hasNext();) {
97                 AttributesImpl attr = new AttributesImpl();
98                 PortalTool pt = (PortalTool) it.next();
99                 attr.addCDATAAttribute("parameter", "tools/functions/" + pt.getId());
100                 attr.addCDATAAttribute("name", pt.getName());
101                 attr.addCDATAAttribute("http://apache.org/cocoon/i18n/2.1", "attr", "i18n:attr", "name");
102                 if(selected != null) {
103                     if(selected.equals(pt.getId())) {
104                         attr.addCDATAAttribute("selected", "true");
105                     }
106                 }
107                 super.startElement("","named-item", "named-item", attr);
108                 super.endElement("", "named-item", "named-item");
109             }
110         super.endElement("", "tab-layout", "tab-layout");
111         if(selected != null) {
112             PortalTool ct = pm.getTool(selected);
113             if(ct != null) {
114                 super.startElement("", "tool-functions", "tool-functions", a);
115                 Collection JavaDoc funs = ct.getFunctions();
116                 for(Iterator JavaDoc it = funs.iterator();it.hasNext();) {
117                         PortalToolFunction ptf = (PortalToolFunction) it.next();
118                         AttributesImpl attr = new AttributesImpl();
119                         attr.addCDATAAttribute("parameter", "tools/plugins/" + ct.getId() + "/" + ptf.getFunction());
120                         attr.addCDATAAttribute("name", ptf.getName());
121                         attr.addCDATAAttribute("http://apache.org/cocoon/i18n/2.1", "attr", "i18n:attr", "name"); super.startElement("", "function", "function", attr);
122                         super.endElement("", "function", "function");
123                 }
124                 super.endElement("", "tool-functions", "tool-functions");
125             }
126         }
127     }
128
129
130     /* (non-Javadoc)
131      * @see org.xml.sax.ContentHandler#endDocument()
132      */

133     public void endDocument() throws SAXException JavaDoc {
134         super.endElement("", "div", "div");
135         super.endPrefixMapping("i18n");
136         selected= null;
137         super.endDocument();
138     }
139
140 }
141
Popular Tags