001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.rpad.simple;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021: import java.util.Locale;
022: import java.util.StringTokenizer;
023:
024: import org.apache.jetspeed.portlets.rpad.PortletApplication;
025: import org.xml.sax.Attributes;
026: import org.xml.sax.helpers.DefaultHandler;
027:
028: public class SimpleConfigHandler extends DefaultHandler {
029:
030: private List portletApplications;
031:
032: private PortletApplication portletApplication;
033:
034: private List qNameList;
035:
036: public SimpleConfigHandler() {
037: portletApplications = new ArrayList();
038: qNameList = new ArrayList();
039: }
040:
041: public void startElement(String uri, String localName,
042: String qName, Attributes attributes) {
043: if ("repository".equals(qName)) {
044: //TODO version
045: //TODO id
046: } else if ("portlet".equals(qName)) {
047: portletApplication = new PortletApplication();
048:
049: String artifactId = attributes.getValue("id");
050: if (artifactId != null) {
051: portletApplication.setArtifactId(artifactId);
052: }
053:
054: String groupId = attributes.getValue("group");
055: if (groupId != null) {
056: portletApplication.setGroupId(groupId);
057: }
058:
059: String version = attributes.getValue("version");
060: if (version != null) {
061: portletApplication.setVersion(version);
062: }
063: }
064: synchronized (qNameList) {
065: qNameList.add(qName);
066: }
067: }
068:
069: public void characters(char[] ch, int start, int length) {
070: if (qNameList.size() < 1) {
071: return;
072: }
073:
074: String value = new String(ch, start, length);
075: String qName = (String) qNameList.get(qNameList.size() - 1);
076: String parentQName;
077: if (qNameList.size() - 2 >= 0) {
078: parentQName = (String) qNameList.get(qNameList.size() - 2);
079: } else {
080: parentQName = "";
081: }
082: if ("portletSpecVersion".equals(qName)) {
083: portletApplication.setPortletSpecVersion(value);
084: } else if ("packaging".equals(qName)) {
085: portletApplication.setPackaging(value);
086: } else if ("name".equals(qName)) {
087: if ("publisher".equals(parentQName)) {
088: portletApplication.setPublisherName(value);
089: } else if ("license".equals(parentQName)) {
090: portletApplication.setLicenseName(value);
091: } else {
092: portletApplication.setName(value);
093: }
094: } else if ("description".equals(qName)) {
095: portletApplication.setDescription(value);
096: } else if ("tag".equals(qName)) {
097: portletApplication.addTag(value);
098: } else if ("url".equals(qName)) {
099: if ("publisher".equals(parentQName)) {
100: portletApplication.setPublisherUrl(value);
101: } else if ("license".equals(parentQName)) {
102: portletApplication.setLicenseUrl(value);
103: }
104: } else if ("binaryURL".equals(qName)) {
105: portletApplication.setBinaryUrl(value);
106: } else if ("sourceURL".equals(qName)) {
107: portletApplication.setSourceUrl(value);
108: } else if ("imageURL".equals(qName)) {
109: portletApplication.setImageUrl(value);
110: }
111: //TODO dependencies
112: //TODO license
113: else if ("compiledJDKVersion".equals(qName)) {
114: portletApplication.setCompiledJDKVersion(value);
115: } else if ("locale".equals(qName)) {
116: Locale l = getLocaleFromString(value);
117: if (l != null) {
118: portletApplication.addSupportedLocale(l);
119: }
120: }
121:
122: }
123:
124: private Locale getLocaleFromString(String localeString) {
125: StringTokenizer st = new StringTokenizer(localeString, "_-");
126: String[] buf = new String[3];
127: int count = 0;
128: while (st.hasMoreTokens()) {
129: buf[count] = st.nextToken();
130: count++;
131: }
132: if (count > 2) {
133: return new Locale(buf[0], buf[1], buf[2]);
134: } else if (count > 1) {
135: return new Locale(buf[0], buf[1]);
136: } else if (count > 0) {
137: return new Locale(buf[0]);
138: }
139: return null;
140: }
141:
142: public void endElement(String uri, String localName, String qName) {
143: if ("portlet".equals(qName)) {
144: portletApplications.add(portletApplication);
145: portletApplication = null;
146: }
147:
148: synchronized (qNameList) {
149: if (qNameList.size() < 1) {
150: throw new IllegalStateException(
151: "The stacked QName is 0.");
152: }
153: String stackedQName = (String) qNameList.remove(qNameList
154: .size() - 1);
155: if (!qName.equals(stackedQName)) {
156: throw new IllegalStateException(
157: "The expected QName is " + stackedQName
158: + ". But the current value is " + qName);
159: }
160: }
161: }
162:
163: /**
164: * @return the portletApplications
165: */
166: public List getPortletApplications() {
167: return portletApplications;
168: }
169:
170: /**
171: * @param portletApplications the portletApplications to set
172: */
173: public void setPortletApplications(List portletApplications) {
174: this.portletApplications = portletApplications;
175: }
176: }
|