KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > uriparameterizer > URIParameterizerImpl


1 /*
2  * Copyright 1999-2004 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  */

17
18 /* $Id: URIParameterizerImpl.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.cocoon.uriparameterizer;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.logger.AbstractLogEnabled;
26 import org.apache.avalon.framework.logger.Logger;
27 import org.apache.avalon.framework.parameters.Parameters;
28 import org.apache.avalon.framework.service.ServiceException;
29 import org.apache.avalon.framework.service.ServiceManager;
30 import org.apache.avalon.framework.service.Serviceable;
31 import org.apache.cocoon.components.source.SourceUtil;
32 import org.apache.cocoon.xml.AbstractXMLConsumer;
33 import org.apache.excalibur.source.Source;
34 import org.apache.excalibur.source.SourceResolver;
35 import org.apache.lenya.util.CacheMap;
36 import org.xml.sax.Attributes JavaDoc;
37
38 /**
39  * Default mplementation of the {@link URIParameterizer}.
40  */

41 public class URIParameterizerImpl
42     extends AbstractLogEnabled
43     implements URIParameterizer, Serviceable {
44
45     /**
46      * Consumer to handle URIParameterizer events.
47      */

48     public class URIParametrizerConsumer extends AbstractXMLConsumer {
49         private boolean inParamElement = false;
50         private String JavaDoc parameterValue = null;
51
52         private Logger logger;
53
54         /**
55          * Ctor.
56          * @param logger The logger to use.
57          */

58         public URIParametrizerConsumer(Logger logger) {
59             this.logger = logger;
60         }
61
62         /**
63          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
64          */

65         public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a) {
66             if (loc.equals("parameter")) {
67                 if (logger.isDebugEnabled()) {
68                     logger.debug("start Element " + uri + ":" + loc + ":" + raw);
69                 }
70                 inParamElement = true;
71             }
72         }
73
74         /**
75          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
76          */

77         public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a) {
78             if (loc.equals("parameter")) {
79                 if (logger.isDebugEnabled()) {
80                     logger.debug("stop Element " + uri + ":" + loc + ":" + raw);
81                 }
82                 inParamElement = false;
83             }
84
85             if (logger.isDebugEnabled()) {
86                 logger.debug("processing Element " + uri + ":" + loc + ":" + raw);
87             }
88         }
89
90         /**
91          * @see org.xml.sax.ContentHandler#characters(char[], int, int)
92          */

93         public void characters(char[] ch, int start, int len) {
94             if (inParamElement) {
95                 parameterValue = new String JavaDoc(ch, start, len);
96                 if (logger.isDebugEnabled()) {
97                     logger.debug("grab Element " + parameterValue);
98                 }
99             }
100         }
101
102         /**
103          * Returns the parameter value that was obtained.
104          * @return A string.
105          */

106         public String JavaDoc getParameter() {
107             return parameterValue;
108         }
109
110     }
111
112     /**
113      * Ctor.
114      */

115     public URIParameterizerImpl() {
116     }
117
118     /**
119      * Receives the URI parameters for a source.
120      * @param uri The URI.
121      * @param src The source.
122      * @param parameters The parameters.
123      * @return The URI parameters.
124      * @throws URIParameterizerException when something went wrong.
125      */

126     public Map JavaDoc parameterize(String JavaDoc uri, String JavaDoc src, Parameters parameters)
127         throws URIParameterizerException {
128
129         Map JavaDoc uriParameters = new HashMap JavaDoc();
130         SourceResolver resolver = null;
131
132         try {
133             resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
134             URIParametrizerConsumer xmlConsumer = new URIParametrizerConsumer(getLogger());
135
136             String JavaDoc[] parameterNames = parameters.getNames();
137
138             for (int i = 0; i < parameterNames.length; i++) {
139
140                 String JavaDoc key = uri + "_" + parameterNames[i];
141
142                 String JavaDoc value = (String JavaDoc) cache.get(key);
143
144                 if (value == null) {
145                     String JavaDoc parameterSrc = parameters.getParameter(parameterNames[i]) + "/" + src;
146
147                     Source inputSource = null;
148                     try {
149                         inputSource = resolver.resolveURI(parameterSrc);
150
151                         if (getLogger().isDebugEnabled()) {
152                             getLogger().debug("File resolved to " + inputSource.getURI());
153                         }
154
155                         SourceUtil.toSAX(inputSource, xmlConsumer);
156                     } finally {
157                         if (inputSource != null) {
158                             resolver.release(inputSource);
159                         }
160                     }
161                     value = xmlConsumer.getParameter();
162                     cache.put(key, value);
163                 }
164
165                 uriParameters.put(parameterNames[i], value);
166             }
167         } catch (Exception JavaDoc e) {
168             getLogger().error("URI parameterizing failed: ", e);
169             throw new URIParameterizerException(e);
170         } finally {
171             if (resolver != null) {
172                 manager.release(resolver);
173             }
174         }
175         return uriParameters;
176     }
177
178     protected static final int CACHE_CAPACITY = 1000;
179
180     private static Map JavaDoc cache = new CacheMap(CACHE_CAPACITY);
181
182     private ServiceManager manager;
183
184     /**
185      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
186      */

187     public void service(ServiceManager manager) throws ServiceException {
188         this.manager = manager;
189     }
190
191 }
192
Popular Tags