📄 portletsxmlserializer.java
字号:
NodeList paramList = paramParent.getElementsByTagName(parameterTagName); //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Found " + Integer.toString(paramList.getLength()) + " tags"); for (int i = 0; i < paramList.getLength(); i++) { Element paramElement = (Element) paramList.item(i); //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Examining parameter " + Integer.toString(i) + " named " + paramElement.getTagName()); String paramName = paramElement.getAttribute("name"); if (paramName != null) { if (paramName.equalsIgnoreCase(parameterName)) { //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Found parameter name, looking for value... "); // we have found the parameter value Node textNode = paramElement.getFirstChild(); if (textNode.getNodeType() == Node.TEXT_NODE) { //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Found parameter value = [" + textNode.getNodeValue() + "]"); return textNode.getNodeValue(); } else { //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Not a text node ?!"); throw new JahiaException(ERROR_READING_PLML_FILE_MSG, "Value of parameter is not in correct format, should only be text", JahiaException.CRITICAL, JahiaException.CONFIG_ERROR); } } else { //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Attribute case is not correct [" + paramName + "]"); } } else { //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Parameter not found"); throw new JahiaException(ERROR_READING_PLML_FILE_MSG, "Parameter not found !", JahiaException.CRITICAL, JahiaException.CONFIG_ERROR); } } //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "No parameter found, returning null... "); return null; } /** * Find the value of a certain named attribute * */ private String getAttributeValue(Node paramParent, String attributeName) throws JahiaException { NamedNodeMap attrib = paramParent.getAttributes(); Node paramAttrib = attrib.getNamedItem(attributeName); return paramAttrib.getNodeValue(); } // end getAttributeValue() /** * Retrieves a column count for the specified page */ public int getColumnCount() throws JahiaException { maxColumn = 0; Element plmlElement = findPlml(rootDocument.getDocumentElement()); Element portletGroupElement = findPortletGroup(plmlElement, thePageID); NodeList portletList = portletGroupElement.getElementsByTagName(PORTLET_TAG); for (int i = 0; i < portletList.getLength(); i++) { Element portletElement = (Element) portletList.item(i); String columnPosStr = getPortletParameterValue(portletElement, "column_position"); int columnPos = Integer.parseInt(columnPosStr); if (columnPos > maxColumn) { maxColumn = columnPos; } } return maxColumn; } // end getColumnCount() /** * Builds a PortletBean object from a specified portletElement node in the * DOM tree. * Warning : no verification for the positioning is done here ! * @param portletElement DOM Element of the portlet node * @param */ private PortletBean xmlPortletToPortletBean(Element portletElement) throws JahiaException { String idStr = getAttributeValue(portletElement, PARAMETER_TAG_ID_ATTRIBUTE); PortletBean thePortlet = new PortletBean(Integer.parseInt(idStr), Integer.parseInt(getPortletParameterValue(portletElement, "source_id")), Integer.parseInt(getPortletParameterValue(portletElement, "w")), Integer.parseInt(getPortletParameterValue(portletElement, "h")), Integer.parseInt(getPortletParameterValue(portletElement, "column_position")), Integer.parseInt(getPortletParameterValue(portletElement, "row_position"))); return thePortlet; } /** * Retrieves a portle bean from the DOM tree * @param thePortletID id of the portlet */ public PortletBean getPortlet(int thePortletID) throws JahiaException { PortletBean thePortlet = null; Element plmlElement = findPlml(rootDocument.getDocumentElement()); Element portletGroupElement = findPortletGroup(plmlElement, thePageID); Element portletElement = findPortlet(portletGroupElement, thePortletID); thePortlet = xmlPortletToPortletBean(portletElement); return thePortlet; } // end getPortlet() /** * Retrieves a list of portlet settings * * @author J閞鬽e B閐at * */ public Enumeration getPortlets() throws JahiaException { thePortletList = new Vector(); Element plmlElement = findPlml(rootDocument.getDocumentElement()); Element portletGroupElement = findPortletGroup(plmlElement, thePageID); NodeList portletNodeList = portletGroupElement.getElementsByTagName(PORTLET_TAG); for (int i = 0; i < portletNodeList.getLength(); i++) { Element portletElement = (Element) portletNodeList.item(i); PortletBean portletBean = xmlPortletToPortletBean(portletElement); thePortletList.add(portletBean); } return thePortletList.elements(); } // end getPortletList() /** * Retrieves a list of portlets for the specified column * * @todo this should probably be moved to the portlets manager * */ public Enumeration getPortletsFromColumn(int theColumn) throws JahiaException { Enumeration allPortlets = getPortlets(); Vector portletList = new Vector(); while (allPortlets.hasMoreElements()) { PortletBean portlet = (PortletBean) allPortlets.nextElement(); if (portlet.getPortletColumn() == theColumn) { portletList.add(portlet); } } return thePortletList.elements(); } // end getPortletsFromColumn() /** * Stores a portlet parameter. If the parameter does not exist it is created * and used for storage. This allows for dynamic creation of new objects, * with just a small performance overhead. * * @param parameterName name of the parameter to set * @param parameterValue value to set for the parameter * @param portletElement the parent portlet element in which to store the * parameter value */ private void storePortletParameter(String parameterName, String parameterValue, Element portletElement) throws JahiaException{ NodeList parameterList = portletElement.getElementsByTagName(PORTLET_PARAMETER_TAG); Element paramElement = null; boolean foundParam = false; for (int i = 0; i < parameterList.getLength(); i++) { paramElement = (Element) parameterList.item(i); String paramNameValue = paramElement.getAttribute(PARAMETER_TAG_NAME_ATTRIBUTE); if (paramNameValue != null) { if (paramNameValue.equalsIgnoreCase(parameterName)) { foundParam = true; break; } } } if (!foundParam) { // parameter tag was not found, let's create it. Element parameterElement = rootDocument.createElement(PORTLET_PARAMETER_TAG); parameterElement.setAttribute(PARAMETER_TAG_NAME_ATTRIBUTE, parameterName); portletElement.appendChild(parameterElement); Text parameterValueElement = rootDocument.createTextNode(""); parameterElement.appendChild(parameterValueElement); paramElement = parameterElement; } Node textNode = paramElement.getFirstChild(); if (textNode.getNodeType() == Node.TEXT_NODE) { Text paramValueText = (Text) textNode; paramValueText.setData(parameterValue); } else { throw new JahiaException("PortletsXMLSerializer", "Invalid parameter value in DOM tree !", JahiaException.CRITICAL, JahiaException.DATA_ERROR); } } /** * Serializes a PortletBean object to a DOM tree. The necessary update or * creation calls are made to make sure that the necessary ressources are * created if this is a new portlet. */ public void storePortlet(PortletBean portlet, String currentUserName) throws JahiaException { Element plmlElement = findPlml(rootDocument.getDocumentElement()); Element portletGroupElement = findPortletGroup(plmlElement, thePageID); Element portletElement = null; try { portletElement = findPortlet(portletGroupElement, portlet.getPortletID()); } catch (JahiaException je) { // the portlet does not yet exist in the DOM tree, let's create it. Element newPortletElement = rootDocument.createElement(PORTLET_TAG); newPortletElement.setAttribute("id", Integer.toString(update_count("portlet"))); portletGroupElement.appendChild(newPortletElement); portletElement = newPortletElement; // we will use this new element from now on } storePortletParameter("source_id", Integer.toString(portlet.getPortletSourceID()), portletElement); storePortletParameter("w", Integer.toString(portlet.getPortletW()), portletElement); storePortletParameter("h", Integer.toString(portlet.getPortletH()), portletElement); storePortletParameter("column_position", Integer.toString(portlet.getPortletColumn()), portletElement); storePortletParameter("row_position", Integer.toString(portlet.getPortletRow()), portletElement); // commit the changes to the XML file saveFile(theFile.toString()); } /** * delete a portlet by specifying it's source ID parameter value */ public void deletePortlet(int theSourceID) throws JahiaException { Element plmlElement = findPlml (rootDocument.getDocumentElement()); Element portletGroupElement = findPortletGroup(plmlElement, thePageID); NodeList portletList = portletGroupElement.getElementsByTagName(PORTLET_TAG); for (int i = 0; i < portletList.getLength(); i++) { Element portletElement = (Element) portletList.item(i); String sourceIDStr = getPortletParameterValue(portletElement, "source_id"); int sourceID = Integer.parseInt(sourceIDStr); if (sourceID == theSourceID) { portletGroupElement.removeChild(portletElement); return; } } // commit the changes to the XML file saveFile(theFile.toString()); } // end deletePortlet /** * Add new Portlet Group to the Portlets XML file * @param groupID the new group ID to insert */ public void addPortletGroup(int groupID) throws JahiaException { JahiaConsole.println("PortletsXMLSerializer.addPortletGroup", "groupID = " + Integer.toString(groupID) ); if (addPortletGroup) { return; } // small optimization JahiaConsole.println("PortletsXMLSerializer.addPortletGroup", "Finding PLML group " + Integer.toString(groupID) ); try { Element plmlElement = findPlml(rootDocument.getDocumentElement()); Element newPortletGroupElement = rootDocument.createElement(PORTLETLIST_TAG); newPortletGroupElement.setAttribute( PARAMETER_TAG_ID_ATTRIBUTE, Integer.toString(update_count("portletgroup"))); plmlElement.appendChild(newPortletGroupElement); Element groupParamElement = rootDocument.createElement(GROUP_PARAMETER_TAG); groupParamElement.setAttribute(PARAMETER_TAG_NAME_ATTRIBUTE, "page_id"); newPortletGroupElement.appendChild(groupParamElement); Text groupParamText = rootDocument.createTextNode(Integer.toString(groupID)); groupParamElement.appendChild(groupParamText); } catch (Throwable t) { t.printStackTrace(); throw new JahiaException("PortletsXMLSerializer.addPortletGroup", "Error while adding portlet group !", JahiaException.CRITICAL, JahiaException.DATA_ERROR); } // commit the changes to the XML file saveFile(theFile.toString()); } // end addPortletGroup /** * Update portlet autoid or portletgroup autoid * * @author J閞鬽e B閐at * */ public int update_count(String theType) throws JahiaException { int thePortletsCount = 0; int theDesktopsCount = 0; try { if (theType.equals("portlet")) { thePortletsCount = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( "jahiatemplates_portlet" ); } else { theDesktopsCount = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( "jahiatemplates_portletgroup" ); } } catch ( Exception se ) { String errorMsg = "Error in update_count : " + se.getMessage(); JahiaConsole.println( "JahiaDBManager", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot update count data to the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } if (theType.equals("portlet")) { return thePortletsCount; } else { return theDesktopsCount; } } // end update_count}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -