SAXReader reader = new SAXReader();
//reader.setEntityResolver(resolver);
Document doc = reader.read(new StringReader(xml));
Element root = doc.getRootElement();
Set userAttributes = new HashSet();
Iterator itr1 = root.elements("user-attribute").iterator();
while (itr1.hasNext()) {
Element userAttribute = (Element)itr1.next();
String name = userAttribute.elementText("name");
userAttributes.add(name);
}
itr1 = root.elements("portlet").iterator();
while (itr1.hasNext()) {
Element portlet = (Element)itr1.next();
String portletId = portlet.elementText("portlet-name");
if (servletContextName != null) {
portletId =
servletContextName + PortletConfigImpl.WAR_SEPARATOR +
portletId;
}
portletIds.add(portletId);
Portlet portletModel = (Portlet)portletsPool.get(portletId);
if (portletModel == null) {
portletModel = new Portlet(
new PortletPK(portletId, _SHARED_KEY, _SHARED_KEY));
portletsPool.put(portletId, portletModel);
}
if (servletContextName != null) {
portletModel.setWARFile(true);
}
portletModel.setPortletClass(portlet.elementText("portlet-class"));
Iterator itr2 = portlet.elements("init-param").iterator();
while (itr2.hasNext()) {
Element initParam = (Element)itr2.next();
portletModel.getInitParams().put(
initParam.elementText("name"),
initParam.elementText("value"));
}
Element expirationCache = portlet.element("expiration-cache");
if (expirationCache != null) {
portletModel.setExpCache(new Integer(GetterUtil.getInteger(
expirationCache.getText())));
}
itr2 = portlet.elements("supports").iterator();
while (itr2.hasNext()) {
Element supports = (Element)itr2.next();
String mimeType = supports.elementText("mime-type");
Iterator itr3 = supports.elements("portlet-mode").iterator();
while (itr3.hasNext()) {
Element portletMode = (Element)itr3.next();
Set mimeTypeModes =
(Set)portletModel.getPortletModes().get(mimeType);
if (mimeTypeModes == null) {
mimeTypeModes = new HashSet();
portletModel.getPortletModes().put(
mimeType, mimeTypeModes);
}
mimeTypeModes.add(portletMode.getTextTrim().toLowerCase());
}
}
Set supportedLocales = portletModel.getSupportedLocales();
supportedLocales.add(Locale.getDefault().getLanguage());
itr2 = portlet.elements("supported-locale").iterator();
while (itr2.hasNext()) {
Element supportedLocaleEl = (Element)itr2.next();
String supportedLocale = supportedLocaleEl.getText();
supportedLocales.add(supportedLocale);
}
portletModel.setResourceBundle(
portlet.elementText("resource-bundle"));
Element portletInfo = portlet.element("portlet-info");
String portletInfoTitle = null;
String portletInfoShortTitle = null;
String portletInfoKeyWords = null;
if (portletInfo != null) {
portletInfoTitle = portletInfo.elementText("title");
portletInfoShortTitle = portletInfo.elementText("short-title");
portletInfoKeyWords = portletInfo.elementText("keywords");
}
portletModel.setPortletInfo(new PortletInfo(
portletInfoTitle, portletInfoShortTitle, portletInfoKeyWords));
Element portletPreferences = portlet.element("portlet-preferences");
String defaultPreferences = null;
String prefsValidator = null;
if (portletPreferences != null) {
Element prefsValidatorEl =
portletPreferences.element("preferences-validator");
String prefsValidatorName = null;
if (prefsValidatorEl != null) {
prefsValidator = prefsValidatorEl.getText();
portletPreferences.remove(prefsValidatorEl);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLWriter writer = new XMLWriter(
baos, OutputFormat.createCompactFormat());
writer.write(portletPreferences);
defaultPreferences = baos.toString();
}
portletModel.setDefaultPreferences(defaultPreferences);
portletModel.setPreferencesValidator(prefsValidator);
if (!portletModel.isWARFile() &&
Validator.isNotNull(prefsValidator) &&
GetterUtil.getBoolean(PropsUtil.get(
PropsUtil.PREFERENCE_VALIDATE_ON_STARTUP))) {
try {
PreferencesValidator prefsValidatorObj =
PortalUtil.getPreferencesValidator(portletModel);
prefsValidatorObj.validate(
PortletPreferencesSerializer.fromDefaultXML(
defaultPreferences));
}
catch (Exception e) {
_log.warn(
"Portlet with the name " + portletId +
" does not have valid default preferences");
}
}
List roles = new ArrayList();
itr2 = portlet.elements("security-role-ref").iterator();
while (itr2.hasNext()) {
Element role = (Element)itr2.next();
roles.add(role.elementText("role-name"));
}
portletModel.setRolesArray((String[])roles.toArray(new String[0]));
portletModel.getUserAttributes().addAll(userAttributes);