001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.container.binding;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.util.ArrayList;
011: import java.util.List;
012: import java.util.Locale;
013:
014: import javax.portlet.PortletMode;
015:
016: import org.apache.pluto.om.common.DescriptionSet;
017: import org.apache.pluto.om.common.DisplayNameSet;
018: import org.apache.pluto.om.common.LanguageSet;
019: import org.apache.pluto.om.common.ParameterSet;
020: import org.apache.pluto.om.common.PreferenceSet;
021: import org.apache.pluto.om.portlet.ContentTypeSet;
022: import org.apache.pluto.om.portlet.PortletApplicationDefinition;
023: import org.apache.pluto.om.portlet.PortletDefinitionList;
024: import org.apache.pluto.om.servlet.WebApplicationDefinition;
025: import org.jasig.portal.container.om.common.DescriptionSetImpl;
026: import org.jasig.portal.container.om.common.DisplayNameSetImpl;
027: import org.jasig.portal.container.om.common.LanguageSetImpl;
028: import org.jasig.portal.container.om.common.ParameterSetImpl;
029: import org.jasig.portal.container.om.common.PreferenceSetImpl;
030: import org.jasig.portal.container.om.common.SecurityRoleRefImpl;
031: import org.jasig.portal.container.om.common.SecurityRoleRefSetImpl;
032: import org.jasig.portal.container.om.portlet.ContentTypeImpl;
033: import org.jasig.portal.container.om.portlet.ContentTypeSetImpl;
034: import org.jasig.portal.container.om.portlet.PortletApplicationDefinitionImpl;
035: import org.jasig.portal.container.om.portlet.PortletDefinitionImpl;
036: import org.jasig.portal.container.om.portlet.PortletDefinitionListImpl;
037: import org.jasig.portal.container.om.portlet.UserAttributeImpl;
038: import org.jasig.portal.container.om.portlet.UserAttributeListImpl;
039: import org.jasig.portal.i18n.LocaleManager;
040: import org.jasig.portal.utils.DocumentFactory;
041: import org.jasig.portal.utils.XML;
042: import org.w3c.dom.Document;
043: import org.w3c.dom.Element;
044: import org.w3c.dom.NodeList;
045: import org.xml.sax.SAXException;
046:
047: /**
048: * Parses a <code>portlet.xml</code> file and produces data structures.
049: * @author Ken Weiner, kweiner@unicon.net
050: * @version $Revision: 35013 $
051: */
052: public class PortletApplicationUnmarshaller {
053:
054: private InputStream inputStream = null;
055: private String contextName = null;
056: private Document doc = null; // Might want to consider SAX instead of DOM parsing
057: private PortletApplicationDefinitionImpl portletApplicationDefinition = null;
058:
059: public PortletApplicationUnmarshaller() {
060: portletApplicationDefinition = new PortletApplicationDefinitionImpl();
061: }
062:
063: /**
064: * Initializer that takes an input stream to the <code>portlet.xml</code> file.
065: * @param inputStream an input stream to the contents of the <code>portlet.xml</code> file
066: * @throws IOException
067: * @throws SAXException
068: */
069: public void init(InputStream inputStream, String contextName)
070: throws IOException, SAXException {
071: this .inputStream = inputStream;
072: this .contextName = contextName;
073: this .doc = DocumentFactory.getDocumentFromStream(inputStream);
074: }
075:
076: /**
077: * Returns a PortletApplicationDefinition object that was populated with data
078: * from the <code>portlet.xml</code> file.
079: * @return the portlet application definition
080: */
081: public PortletApplicationDefinition getPortletApplicationDefinition(
082: WebApplicationDefinition webApplicationDefinition) {
083: Element portletAppE = doc.getDocumentElement();
084: portletApplicationDefinition.setId(contextName);
085: portletApplicationDefinition.setVersion(portletAppE
086: .getAttribute("version"));
087: portletApplicationDefinition
088: .setPortletDefinitionList(getPortletDefinitions(
089: portletAppE, webApplicationDefinition));
090: portletApplicationDefinition
091: .setUserAttributes(getUserAttributes(portletAppE));
092: portletApplicationDefinition
093: .setWebApplicationDefinition(webApplicationDefinition);
094: return portletApplicationDefinition;
095: }
096:
097: private PortletDefinitionList getPortletDefinitions(
098: Element portletAppE,
099: WebApplicationDefinition webApplicationDefinition) {
100: PortletDefinitionListImpl portletDefinitions = new PortletDefinitionListImpl();
101:
102: NodeList portletNL = portletAppE
103: .getElementsByTagName("portlet");
104: for (int i = 0; i < portletNL.getLength(); i += 1) {
105: Element portletE = (Element) portletNL.item(i);
106: String portletName = XML.getChildElementText(portletE,
107: "portlet-name");
108: String portletDefinitionId = contextName + "."
109: + portletName;
110: PortletDefinitionImpl portletDefinition = new PortletDefinitionImpl();
111: portletDefinition.setId(portletDefinitionId);
112: portletDefinition.setClassName(XML.getChildElementText(
113: portletE, "portlet-class"));
114: portletDefinition.setName(portletName);
115: portletDefinition
116: .setDisplayNames(getDisplayNames(portletE));
117: portletDefinition
118: .setDescriptions(getDescriptions(portletE));
119: portletDefinition.setLanguages(getLanguages(portletE));
120: portletDefinition
121: .setInitParameters(getInitParameters(portletE));
122: portletDefinition.setPreferences(getPreferences(portletE));
123: portletDefinition
124: .setContentTypes(getContentTypes(portletE));
125: portletDefinition
126: .setServletDefinition(webApplicationDefinition
127: .getServletDefinitionList()
128: .get(portletName));
129: portletDefinition
130: .setPortletApplicationDefinition(portletApplicationDefinition);
131: portletDefinition.setExpirationCache(XML
132: .getChildElementText(portletE, "expiration-cache"));
133: portletDefinition
134: .setInitSecurityRoleRefSet(getSecurityRoleRefs(portletE));
135:
136: portletDefinitions.add(portletDefinitionId,
137: portletDefinition);
138: }
139: return portletDefinitions;
140: }
141:
142: private DisplayNameSet getDisplayNames(Element portletE) {
143: DisplayNameSetImpl displayNames = new DisplayNameSetImpl();
144: NodeList displayNameNL = portletE
145: .getElementsByTagName("display-name");
146: for (int i = 0; i < displayNameNL.getLength(); i += 1) {
147: Element displayNameE = (Element) displayNameNL.item(i);
148: String displayName = XML.getElementText(displayNameE);
149: displayNames.add(displayName, Locale.getDefault());
150: }
151: return displayNames;
152: }
153:
154: private DescriptionSet getDescriptions(Element portletE) {
155: DescriptionSetImpl descriptions = new DescriptionSetImpl();
156: NodeList descriptionNL = portletE
157: .getElementsByTagName("description");
158: for (int i = 0; i < descriptionNL.getLength(); i += 1) {
159: Element descriptionE = (Element) descriptionNL.item(i);
160: String description = XML.getElementText(descriptionE);
161: descriptions.add(description, Locale.getDefault());
162: }
163: return descriptions;
164: }
165:
166: private LanguageSet getLanguages(Element portletE) {
167: // Check for a resource bundle element
168: String resources = null;
169: NodeList resourceBundleNL = portletE
170: .getElementsByTagName("resource-bundle");
171: if (resourceBundleNL.getLength() > 0) {
172: Element resourceBundleE = (Element) resourceBundleNL
173: .item(0); // there should only be one
174: String resourceBundle = XML.getElementText(resourceBundleE);
175: if (resourceBundle.trim().length() > 0) {
176: resources = resourceBundle;
177: }
178: }
179:
180: // The portlet-info element is optional if the resource-bundle element is specified
181: String title = null;
182: String shortTitle = null;
183: String keywords = null;
184: NodeList portletInfoNL = portletE
185: .getElementsByTagName("portlet-info");
186: if (portletInfoNL.getLength() > 0) {
187: Element portletInfoE = (Element) portletInfoNL.item(0); // there should only be one
188: title = XML.getChildElementText(portletInfoE, "title");
189: shortTitle = XML.getChildElementText(portletInfoE,
190: "short-title");
191: keywords = XML
192: .getChildElementText(portletInfoE, "keywords");
193: }
194:
195: LanguageSetImpl languages = new LanguageSetImpl(title,
196: shortTitle, keywords, resources);
197: languages.setClassLoader(Thread.currentThread()
198: .getContextClassLoader());
199: NodeList supportedLocaleNL = portletE
200: .getElementsByTagName("supported-locale");
201: for (int i = 0; i < supportedLocaleNL.getLength(); i += 1) {
202: Element supportedLocaleE = (Element) supportedLocaleNL
203: .item(i);
204: languages.addLocale(LocaleManager.parseLocale(XML
205: .getElementText(supportedLocaleE)));
206: }
207: return languages;
208: }
209:
210: private ParameterSet getInitParameters(Element portletE) {
211: ParameterSetImpl parameters = new ParameterSetImpl();
212: NodeList initParamNL = portletE
213: .getElementsByTagName("init-param");
214: for (int i = 0; i < initParamNL.getLength(); i += 1) {
215: Element initParamE = (Element) initParamNL.item(i);
216: parameters.add(XML.getChildElementText(initParamE, "name"),
217: XML.getChildElementText(initParamE, "value"));
218: }
219: return parameters;
220: }
221:
222: private PreferenceSet getPreferences(Element portletE) {
223: PreferenceSetImpl preferences = new PreferenceSetImpl();
224: NodeList portletPreferencesNL = portletE
225: .getElementsByTagName("portlet-preferences");
226: if (portletPreferencesNL.getLength() > 0) {
227: Element portletPreferencesE = (Element) portletPreferencesNL
228: .item(0); // there should only be one
229: NodeList preferenceNL = portletPreferencesE
230: .getElementsByTagName("preference");
231: for (int i = 0; i < preferenceNL.getLength(); i += 1) {
232: Element preferenceE = (Element) preferenceNL.item(i);
233: String name = XML.getChildElementText(preferenceE,
234: "name");
235: List values = new ArrayList(1); // There is usually just one value
236: NodeList valueNL = preferenceE
237: .getElementsByTagName("value");
238: for (int j = 0; j < valueNL.getLength(); j += 1) {
239: Element valueE = (Element) valueNL.item(j);
240: values.add(XML.getElementText(valueE));
241: }
242: boolean readOnly = Boolean.valueOf(
243: XML.getChildElementText(preferenceE,
244: "read-only")).booleanValue();
245: preferences.add(name, values, readOnly);
246: }
247: preferences.setPreferencesValidator(XML
248: .getChildElementText(portletPreferencesE,
249: "preferences-validator"));
250: }
251: return preferences;
252: }
253:
254: private SecurityRoleRefSetImpl getSecurityRoleRefs(Element portletE) {
255: SecurityRoleRefSetImpl securityRoleRefs = new SecurityRoleRefSetImpl();
256: NodeList securityRoleRefsNL = portletE
257: .getElementsByTagName("security-role-ref");
258: for (int i = 0; i < securityRoleRefsNL.getLength(); i += 1) {
259: Element securityRoleRefE = (Element) securityRoleRefsNL
260: .item(i);
261: SecurityRoleRefImpl securityRoleRef = new SecurityRoleRefImpl();
262: securityRoleRef.setDescription(XML.getChildElementText(
263: securityRoleRefE, "description"));
264: securityRoleRef.setRoleName(XML.getChildElementText(
265: securityRoleRefE, "role-name"));
266: securityRoleRef.setRoleLink(XML.getChildElementText(
267: securityRoleRefE, "role-link"));
268: securityRoleRefs.add(securityRoleRef);
269: }
270: return securityRoleRefs;
271: }
272:
273: private ContentTypeSet getContentTypes(Element portletE) {
274: ContentTypeSetImpl contentTypes = new ContentTypeSetImpl();
275: NodeList supportsNL = portletE.getElementsByTagName("supports");
276: for (int i = 0; i < supportsNL.getLength(); i += 1) {
277: Element supportsE = (Element) supportsNL.item(i);
278: ContentTypeImpl contentType = new ContentTypeImpl();
279: contentType.setContentType(XML.getChildElementText(
280: supportsE, "mime-type"));
281: NodeList portletModeNL = supportsE
282: .getElementsByTagName("portlet-mode");
283: for (int j = 0; j < portletModeNL.getLength(); j += 1) {
284: Element portletModeE = (Element) portletModeNL.item(j);
285: contentType.addPortletMode(new PortletMode(XML
286: .getElementText(portletModeE)));
287: }
288: contentTypes.add(contentType);
289: }
290: return contentTypes;
291: }
292:
293: private UserAttributeListImpl getUserAttributes(Element portletAppE) {
294: UserAttributeListImpl userAttributes = new UserAttributeListImpl();
295: NodeList userAttributesNL = portletAppE
296: .getElementsByTagName("user-attribute");
297: for (int i = 0; i < userAttributesNL.getLength(); i += 1) {
298: Element userAttributeE = (Element) userAttributesNL.item(i);
299: UserAttributeImpl userAttribute = new UserAttributeImpl();
300: userAttribute.setDescription(XML.getChildElementText(
301: userAttributeE, "description"));
302: userAttribute.setName(XML.getChildElementText(
303: userAttributeE, "name"));
304: userAttributes.add(userAttribute);
305: }
306: return userAttributes;
307: }
308:
309: }
|