KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > tree > TreeLocalizer


1 /*
2  * $Header: /home/cvs/jakarta-slide/projector/src/java/org/apache/slide/projector/processor/tree/TreeLocalizer.java,v 1.4 2004/08/03 14:30:02 dflorey Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/08/03 14:30:02 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.projector.processor.tree;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 import org.apache.slide.projector.Context;
36 import org.apache.slide.projector.EnvironmentConsumer;
37 import org.apache.slide.projector.Processor;
38 import org.apache.slide.projector.Result;
39 import org.apache.slide.projector.Store;
40 import org.apache.slide.projector.descriptor.LocaleValueDescriptor;
41 import org.apache.slide.projector.descriptor.ParameterDescriptor;
42 import org.apache.slide.projector.descriptor.RequiredEnvironmentDescriptor;
43 import org.apache.slide.projector.descriptor.ResultDescriptor;
44 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
45 import org.apache.slide.projector.descriptor.StateDescriptor;
46 import org.apache.slide.projector.descriptor.StringValueDescriptor;
47 import org.apache.slide.projector.descriptor.TreeValueDescriptor;
48 import org.apache.slide.projector.engine.ProcessorManager;
49 import org.apache.slide.projector.i18n.DefaultMessage;
50 import org.apache.slide.projector.i18n.MessageManager;
51 import org.apache.slide.projector.i18n.MessageNotFoundException;
52 import org.apache.slide.projector.i18n.ParameterMessage;
53 import org.apache.slide.projector.processor.SimpleProcessor;
54 import org.apache.slide.projector.value.ArrayValue;
55 import org.apache.slide.projector.value.LocaleValue;
56 import org.apache.slide.projector.value.MapValue;
57 import org.apache.slide.projector.value.StringValue;
58 import org.apache.slide.projector.value.Value;
59
60 /**
61  * The TreeLocalizer class
62  *
63  */

64 public class TreeLocalizer implements Processor, EnvironmentConsumer {
65     private static Logger JavaDoc logger = Logger.getLogger(TreeLocalizer.class.getName());
66
67     public final static String JavaDoc LOCALE = "locale";
68     
69     protected final static String JavaDoc IMAGE_IDENTIFIER = "image";
70
71     private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[] {
72         new ParameterDescriptor(SimpleProcessor.INPUT, new ParameterMessage("treeLocalizer/input"), new TreeValueDescriptor(new StringValueDescriptor()))
73     };
74     
75     private final static ResultDescriptor resultDescriptor = new ResultDescriptor(
76             new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR },
77             new ResultEntryDescriptor[] {
78                 new ResultEntryDescriptor(SimpleProcessor.OUTPUT, new DefaultMessage("treeLocalizer/output"), ArrayValue.CONTENT_TYPE, false)
79             });
80
81     private final static RequiredEnvironmentDescriptor[] requiredEnvironmentDescriptors = new RequiredEnvironmentDescriptor[] {
82             new RequiredEnvironmentDescriptor(LOCALE, Store.SESSION, new DefaultMessage("treeLocalized/requiredEnvironment/locale"), new LocaleValueDescriptor(), new LocaleValue(Locale.getDefault()))
83     };
84
85     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
86         Value []resources = ((ArrayValue)parameter.get(SimpleProcessor.INPUT)).getArray();
87         Locale JavaDoc locale = ((LocaleValue)context.getStore(Store.SESSION).get(LOCALE)).getLocale();
88         return new Result(StateDescriptor.OK, SimpleProcessor.OUTPUT, localize(resources, locale, context));
89     }
90
91     private ArrayValue localize(Value []resources, Locale JavaDoc locale, Context context) throws Exception JavaDoc {
92         List JavaDoc localizedResources = new ArrayList JavaDoc();
93         for ( int i = 0; i < resources.length; i++ ) {
94             if ( resources[i] instanceof StringValue ) {
95                 Map JavaDoc localizedMap = new HashMap JavaDoc();
96                 try {
97                     localizedMap.put(TreeRenderer.ID, ProcessorManager.getInstance().process(ProcessorManager.URL, resources[i], context));
98                     Map JavaDoc entries = MessageManager.getEntries(((StringValue)resources[i]).toString(), locale);
99                     for ( Iterator JavaDoc j = entries.entrySet().iterator(); j.hasNext(); ) {
100                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)j.next();
101                         String JavaDoc key = (String JavaDoc)entry.getKey();
102                         String JavaDoc value = (String JavaDoc)entry.getValue();
103                         if ( key.startsWith(IMAGE_IDENTIFIER) ) {
104                             key = key.substring(IMAGE_IDENTIFIER.length()+1);
105                             localizedMap.put(key, ProcessorManager.getInstance().process(ProcessorManager.BINARY, value, "url", context));
106                         } else {
107                             localizedMap.put(key, new StringValue(value));
108                         }
109                     }
110                 } catch ( MessageNotFoundException exception ) {
111                     logger.log(Level.SEVERE, "No message found while localizing tree", exception);
112                 }
113                 localizedResources.add(new MapValue(localizedMap));
114             } else if ( resources[i] instanceof ArrayValue ) {
115                 localizedResources.add(localize(((ArrayValue)resources[i]).getArray(), locale, context));
116             }
117         }
118         Value[] localizedResourceArray = new Value[localizedResources.size()];
119         return new ArrayValue((Value [])localizedResources.toArray(localizedResourceArray));
120     }
121
122     public ParameterDescriptor[] getParameterDescriptors() {
123         return parameterDescriptors;
124     }
125
126     public ResultDescriptor getResultDescriptor() {
127         return resultDescriptor;
128     }
129
130     public RequiredEnvironmentDescriptor[] getRequiredEnvironmentDescriptors() {
131         return requiredEnvironmentDescriptors;
132     }
133 }
134
Popular Tags