View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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.jetspeed.tools.pamanager.rules;
18  
19  import java.util.Locale;
20  
21  import org.apache.commons.digester.Rule;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.apache.jetspeed.om.common.GenericMetadata;
27  import org.apache.jetspeed.om.common.LocalizedField;
28  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
29  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
30  
31  import org.xml.sax.Attributes;
32  
33  /***
34   * This class helps load internationalized fields
35   * 
36   * @author <a href="mailto:jford@apache.org">Jeremy Ford </a>
37   * @version $Id: LocalizedFieldRule.java 516448 2007-03-09 16:25:47Z ate $
38   */
39  public class LocalizedFieldRule extends Rule
40  {
41      protected final static Log log = LogFactory.getLog(LocalizedFieldRule.class);
42  
43      /***
44       * Handle the beginning of an XML element.
45       * 
46       * @param attributes
47       *            The attributes of this element
48       * @exception Exception
49       *                if a processing error occurs
50       */
51      public void begin(String namespace, String name, Attributes attributes) throws Exception
52      {
53  
54          if (digester.getLogger().isDebugEnabled())
55              digester.getLogger().debug("Setting localized field " + name);
56          
57          Object obj = digester.peek();
58          if (null == obj)
59          {
60              digester.push(null);
61              return;
62          }
63          GenericMetadata metadata = null;
64          if (obj instanceof MutablePortletApplication)
65          {
66              metadata = ((MutablePortletApplication) obj).getMetadata();
67          }
68          if (obj instanceof PortletDefinitionComposite)
69          {
70              metadata = ((PortletDefinitionComposite) obj).getMetadata();
71          }
72          if (metadata != null)
73          {
74              LocalizedField child = metadata.createLocalizedField();
75  
76              if (name.equals("metadata"))
77              {
78                  String nameAttr = attributes.getValue("name");
79                  child.setName(nameAttr);
80              }
81              else
82              {
83                  child.setName(name);
84              }
85              String language = attributes.getValue("xml:lang");
86              Locale locale = null;
87              if (language == null)
88              {
89                  locale = new Locale("en");
90              }
91              else
92              {
93                  locale = new Locale(language);
94              }
95  
96              child.setLocale(locale);
97              digester.push(child);
98          }
99          else
100         {
101             digester.push(null);
102         }
103     }
104 
105     public void body(String namespace, String name, String text) throws Exception
106     {
107         LocalizedField child = (LocalizedField) digester.peek(0);
108         if (child != null)
109         {
110             child.setValue(text);
111         }
112     }
113 
114     public void end(String namespace, String name) throws Exception
115     {
116         LocalizedField child = (LocalizedField) digester.pop();
117         if (child != null)
118         {
119             Object obj = digester.peek();
120             if (null == obj)
121             {
122                 digester.push(null);
123                 return;
124             }
125             GenericMetadata metadata = null;
126             if (obj instanceof MutablePortletApplication)
127             {
128                 metadata = ((MutablePortletApplication) obj).getMetadata();
129             }
130             if (obj instanceof PortletDefinitionComposite)
131             {
132                 metadata = ((PortletDefinitionComposite) obj).getMetadata();
133             }
134             if (null != metadata)
135             {
136                 metadata.addField(child);
137             }    
138         }
139     }
140 }