📄 portletsxmlserializer.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//package org.jahia.layout;import java.io.*;import java.util.*;import javax.xml.parsers.*;import org.w3c.dom.*;import org.xml.sax.*;import org.jahia.exceptions.*; // JahiaExceptionsimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.registries.*; // ServicesRegistryimport javax.xml.transform.*;import javax.xml.transform.sax.*;import javax.xml.transform.dom.*;import javax.xml.transform.stream.*;/** * This class is responsible for loading up data from an XML file that contains * the portlet configuration of a specific user. * * Format of XML document is as follows: * <?xml version="1.0" encoding="ISO-8859-1"?> * <plml> * <portletgroup id=""> * <group_parameter name="page_number">1</group_parameter> * <portlet id=""> * <portlet_parameter name="source_id">12</portlet_parameter> * <portlet_parameter name="column_position">1</portlet_parameter> * <portlet_parameter name="row_position">1</portlet_parameter> * other parameters... * </portlet> * other portlets * </portletgroup> * other portlet lists (also known as pages) * </plml> * * @author Serge Huber * */public class PortletsXMLSerializer { private static final String CANT_READ_PORTLET_XML_MSG = "Can't read portlet XML file"; private static final String ERROR_READING_PLML_FILE_MSG = "Error reading PLML file"; private static final String PLML_TAG = "PLML"; private static final String GROUP_PARAMETER_TAG = "GROUP_PARAMETER"; private static final String PORTLET_PARAMETER_TAG = "PORTLET_PARAMETER"; private static final String PARAMETER_TAG_NAME_ATTRIBUTE = "name"; private static final String PARAMETER_TAG_ID_ATTRIBUTE = "id"; private static final String PORTLETLIST_TAG = "PORTLETGROUP"; private static final String PORTLET_TAG = "PORTLET"; private static final String XML_ENCODING = "ISO-8859-1"; private Document rootDocument = null; private boolean addPortletGroup = false; private File theFile; private String theXMLfile; private String theDefaultXMLfile; private String paramAttrNode; private Text theTextNode; private Element portletGroupElement; private Element plmlElement; /** * @associates PortletBean */ private Vector thePortletList; private int thePageID; private int maxColumn = 0; /** * Constructor is initialized with the path to the Portlets XML file Descriptor */ PortletsXMLSerializer(String theTemplatesPathDirectory, String thefilename, String theDefaultfilename, int thePageID) throws JahiaException { initXMLSerializer(theTemplatesPathDirectory, thefilename, theDefaultfilename, thePageID, "PortletList"); } /** * Constructor is initialized with the path to the Portlets XML file Descriptor */ PortletsXMLSerializer(String theTemplatesPathDirectory, String thefilename, String theDefaultfilename, int thePageID, String portletGroupName) throws JahiaException { initXMLSerializer(theTemplatesPathDirectory, thefilename, theDefaultfilename, thePageID, portletGroupName); } // end Constructor private void initXMLSerializer(String theTemplatesPathDirectory, String thefilename, String theDefaultfilename, int thePageID, String portletGroupName) throws JahiaException { File thePortletsAPIDirectory = new File( theTemplatesPathDirectory + File.separator + "portlets_api" + File.separator ); if(!thePortletsAPIDirectory.exists()) { thePortletsAPIDirectory.mkdirs(); } String thePath = theTemplatesPathDirectory + File.separator + "portlets_api" + File.separator; this.addPortletGroup = false; this.theFile = new File( thePath + thefilename); this.theXMLfile = thePath + thefilename; this.theDefaultXMLfile = thePath + theDefaultfilename; this.thePageID = thePageID; if (!theFile.exists()) { // file does not exist, we must initialize it JahiaConsole.println("PortletsXMLSerializer constructor", "Creating file " + theXMLfile + "..."); try { DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); //dfactory.setValidating(true); // create only parsers that are validating DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); rootDocument = docBuilder.newDocument(); // let's now create the PLML tag and save the file Element plmlElement = rootDocument.createElement(PLML_TAG); rootDocument.appendChild(plmlElement); addPortletGroup(thePageID); saveFile(theFile.toString()); loadFile(theFile.toString()); addPortletGroup = true; } catch (Throwable t) { t.printStackTrace(); throw new JahiaException("PortletXMLSerializer", "Error while creating new XML file", JahiaException.CRITICAL, JahiaException.DATA_ERROR, t); } } else { // file exists JahiaConsole.println("PortletsXMLSerializer constructor", "Loading file " + theXMLfile + "..."); try { loadFile( theXMLfile ); } catch (Throwable t) { t.printStackTrace(); throw new JahiaException ( "PortletsXMLSerializer", "Error loading XML file", JahiaException.CRITICAL, JahiaException.DATA_ERROR, t ); } } plmlElement = findPlml(rootDocument.getDocumentElement()); portletGroupElement = findPortletGroup(plmlElement, thePageID); } private void createFile(String newFileName) throws JahiaException { /** @todo code to create minimal DOM, or maybe even do this directly * in the constructor code */ saveFile(newFileName); } private void loadFile(String sourceFileName) throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); // dfactory.setValidating(true); // create only parsers that are validating DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); FileInputStream sourceStream = new FileInputStream(sourceFileName); rootDocument = docBuilder.parse(new InputSource(sourceStream)); rootDocument.normalize(); // clean up DOM tree a little } private void saveFile(String destinationFileName) throws JahiaException { try { rootDocument.normalize(); // cleanup DOM tree a little TransformerFactory tfactory = TransformerFactory.newInstance(); // This creates a transformer that does a simple identity transform, // and thus can be used for all intents and purposes as a serializer. Transformer serializer = tfactory.newTransformer(); Properties oprops = new Properties(); oprops.put("method", "xml"); oprops.put("indent-amount", "2"); serializer.setOutputProperties(oprops); FileOutputStream fileStream = new FileOutputStream(destinationFileName); serializer.transform(new DOMSource(rootDocument), new StreamResult(fileStream)); } catch (TransformerConfigurationException tce) { throw new JahiaException ("PortletXMLSerializer.saveFile", "Error in XSL transformer configuration", JahiaException.WARNING, JahiaException.CONFIG_ERROR, tce); } catch (TransformerException te) { throw new JahiaException ("PortletXMLSerializer.saveFile", "Error in XSL transformer exception", JahiaException.WARNING, JahiaException.CONFIG_ERROR, te); } catch (FileNotFoundException fnfe) { throw new JahiaException ("PortletXMLSerializer.saveFile", "File not found exception", JahiaException.WARNING, JahiaException.CONFIG_ERROR, fnfe); } } /** * Finds the PLML tag in the tree specified root element. */ private Element findPlml(Element plmlElement) throws JahiaException { if (plmlElement == null) { throw new JahiaException ("PortletXMLSerializer.findPlml", ERROR_READING_PLML_FILE_MSG, JahiaException.WARNING, JahiaException.CONFIG_ERROR); } if (plmlElement.getTagName().equals(PLML_TAG)) { } else { throw new JahiaException ("PortletXMLSerializer.findPlml", ERROR_READING_PLML_FILE_MSG, JahiaException.WARNING, JahiaException.CONFIG_ERROR); } return (Element) plmlElement; } /** * Finds the portlet group Element tag corresponding to the specified * group ID * @returns Element on success, null on failure to find group corresponding * to id. */ private Element findPortletGroup(Element groupParent, int groupID) throws JahiaException { //JahiaConsole.println("PortletXMLSerializer.findPortletGroup", "Looking for group with id = " + Integer.toString(groupID) ); NodeList groupList = groupParent.getElementsByTagName(PORTLETLIST_TAG); for (int i = 0; i < groupList.getLength(); i++) { Element groupElement = (Element) groupList.item(i); String pageIDStr = getGroupParameterValue(groupElement, "page_id"); if (pageIDStr != null) { //JahiaConsole.println("PortletXMLSerializer.findPortletGroup", "Found pageID string = [" + pageIDStr + "]"); if (groupID == Integer.parseInt(pageIDStr)) return groupElement; } } throw new JahiaException ("PortletXMLSerializer.findPortletGroup", ERROR_READING_PLML_FILE_MSG, JahiaException.WARNING, JahiaException.CONFIG_ERROR); } /** * Finds the portlet Element tag corresponding to the specified * portlet ID * @returns Element on success, null on failure to find portlet corresponding * to id. */ private Element findPortlet(Element portletParent, int portletID) throws JahiaException { NodeList portletList = portletParent.getElementsByTagName(PORTLET_TAG); for (int i = 0; i < portletList.getLength(); i++) { Element portletElement = (Element) portletList.item(i); String idStr = portletElement.getAttribute("id"); if (idStr != null) { if (portletID == Integer.parseInt(idStr)) return portletElement; } } throw new JahiaException ("PortletXMLSerializer.findPortlet", ERROR_READING_PLML_FILE_MSG, JahiaException.WARNING, JahiaException.CONFIG_ERROR); } /** * Retrieves the value of a specified portlet parameter */ private String getPortletParameterValue(Element paramParent, String parameterName) throws JahiaException { return getParameterValue(paramParent, PORTLET_PARAMETER_TAG, parameterName); } /** * Retrieves the value of a specified portlet group parameter */ private String getGroupParameterValue(Element paramParent, String parameterName) throws JahiaException { return getParameterValue(paramParent, GROUP_PARAMETER_TAG, parameterName); } /** * Processes through all the parameter tags of a given node to find the * value of a certain named parameter * */ private String getParameterValue(Element paramParent, String parameterTagName, String parameterName) throws JahiaException { //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Searching for parameter " + parameterName + // " in tag " + parameterTagName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -