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.portlets.rpad.simple;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Locale;
22  import java.util.StringTokenizer;
23  
24  import org.apache.jetspeed.portlets.rpad.PortletApplication;
25  import org.xml.sax.Attributes;
26  import org.xml.sax.helpers.DefaultHandler;
27  
28  public class SimpleConfigHandler extends DefaultHandler
29  {
30  
31      private List portletApplications;
32  
33      private PortletApplication portletApplication;
34  
35      private List qNameList;
36  
37      public SimpleConfigHandler()
38      {
39          portletApplications = new ArrayList();
40          qNameList = new ArrayList();
41      }
42  
43      public void startElement(String uri, String localName, String qName,
44              Attributes attributes)
45      {
46          if ("repository".equals(qName))
47          {
48              //TODO version
49              //TODO id
50          }
51          else if ("portlet".equals(qName))
52          {
53              portletApplication = new PortletApplication();
54  
55              String artifactId = attributes.getValue("id");
56              if (artifactId != null)
57              {
58                  portletApplication.setArtifactId(artifactId);
59              }
60  
61              String groupId = attributes.getValue("group");
62              if (groupId != null)
63              {
64                  portletApplication.setGroupId(groupId);
65              }
66  
67              String version = attributes.getValue("version");
68              if (version != null)
69              {
70                  portletApplication.setVersion(version);
71              }
72          }
73          synchronized (qNameList)
74          {
75              qNameList.add(qName);
76          }
77      }
78  
79      public void characters(char[] ch, int start, int length)
80      {
81          if (qNameList.size() < 1)
82          {
83              return;
84          }
85  
86          String value = new String(ch, start, length);
87          String qName = (String) qNameList.get(qNameList.size() - 1);
88          String parentQName;
89          if (qNameList.size() - 2 >= 0)
90          {
91              parentQName = (String) qNameList.get(qNameList.size() - 2);
92          }
93          else
94          {
95              parentQName = "";
96          }
97          if ("portletSpecVersion".equals(qName))
98          {
99              portletApplication.setPortletSpecVersion(value);
100         }
101         else if ("packaging".equals(qName))
102         {
103             portletApplication.setPackaging(value);
104         }
105         else if ("name".equals(qName))
106         {
107             if ("publisher".equals(parentQName))
108             {
109                 portletApplication.setPublisherName(value);
110             }
111             else if ("license".equals(parentQName))
112             {
113                 portletApplication.setLicenseName(value);
114             }
115             else
116             {
117                 portletApplication.setName(value);
118             }
119         }
120         else if ("description".equals(qName))
121         {
122             portletApplication.setDescription(value);
123         }
124         else if ("tag".equals(qName))
125         {
126             portletApplication.addTag(value);
127         }
128         else if ("url".equals(qName))
129         {
130             if ("publisher".equals(parentQName))
131             {
132                 portletApplication.setPublisherUrl(value);
133             }
134             else if ("license".equals(parentQName))
135             {
136                 portletApplication.setLicenseUrl(value);
137             }
138         }
139         else if ("binaryURL".equals(qName))
140         {
141             portletApplication.setBinaryUrl(value);
142         }
143         else if ("sourceURL".equals(qName))
144         {
145             portletApplication.setSourceUrl(value);
146         }
147         else if ("imageURL".equals(qName))
148         {
149             portletApplication.setImageUrl(value);
150         }
151         //TODO dependencies
152         //TODO license
153         else if ("compiledJDKVersion".equals(qName))
154         {
155             portletApplication.setCompiledJDKVersion(value);
156         }
157         else if ("locale".equals(qName))
158         {
159             Locale l = getLocaleFromString(value);
160             if (l != null)
161             {
162                 portletApplication.addSupportedLocale(l);
163             }
164         }
165 
166     }
167 
168     private Locale getLocaleFromString(String localeString)
169     {
170         StringTokenizer st = new StringTokenizer(localeString, "_-");
171         String[] buf = new String[3];
172         int count = 0;
173         while (st.hasMoreTokens())
174         {
175             buf[count] = st.nextToken();
176             count++;
177         }
178         if (count > 2)
179         {
180             return new Locale(buf[0], buf[1], buf[2]);
181         }
182         else if (count > 1)
183         {
184             return new Locale(buf[0], buf[1]);
185         }
186         else if (count > 0)
187         {
188             return new Locale(buf[0]);
189         }
190         return null;
191     }
192 
193     public void endElement(String uri, String localName, String qName)
194     {
195         if ("portlet".equals(qName))
196         {
197             portletApplications.add(portletApplication);
198             portletApplication = null;
199         }
200 
201         synchronized (qNameList)
202         {
203             if (qNameList.size() < 1)
204             {
205                 throw new IllegalStateException("The stacked QName is 0.");
206             }
207             String stackedQName = (String) qNameList
208                     .remove(qNameList.size() - 1);
209             if (!qName.equals(stackedQName))
210             {
211                 throw new IllegalStateException("The expected QName is "
212                         + stackedQName + ". But the current value is " + qName);
213             }
214         }
215     }
216 
217     /***
218      * @return the portletApplications
219      */
220     public List getPortletApplications()
221     {
222         return portletApplications;
223     }
224 
225     /***
226      * @param portletApplications the portletApplications to set
227      */
228     public void setPortletApplications(List portletApplications)
229     {
230         this.portletApplications = portletApplications;
231     }
232 }