1 package org.jahia.layout; 13 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileNotFoundException ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.util.Enumeration ; 20 import java.util.Vector ; 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 import javax.xml.transform.OutputKeys ; 26 import javax.xml.transform.Transformer ; 27 import javax.xml.transform.TransformerConfigurationException ; 28 import javax.xml.transform.TransformerException ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.dom.DOMSource ; 31 import javax.xml.transform.stream.StreamResult ; 32 33 import org.jahia.exceptions.JahiaException; 34 import org.jahia.registries.ServicesRegistry; 35 import org.jahia.utils.JahiaConsole; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.NamedNodeMap ; 39 import org.w3c.dom.Node ; 40 import org.w3c.dom.NodeList ; 41 import org.w3c.dom.Text ; 42 import org.xml.sax.SAXException ; 43 44 67 public class PortletsXMLSerializer { 68 69 private static final String CANT_READ_PORTLET_XML_MSG = "Can't read portlet XML file"; 70 private static final String ERROR_READING_PLML_FILE_MSG = "Error reading PLML file"; 71 private static final String PLML_TAG = "PLML"; 72 private static final String GROUP_PARAMETER_TAG = "GROUP_PARAMETER"; 73 private static final String PORTLET_PARAMETER_TAG = "PORTLET_PARAMETER"; 74 private static final String PARAMETER_TAG_NAME_ATTRIBUTE = "name"; 75 private static final String PARAMETER_TAG_ID_ATTRIBUTE = "id"; 76 private static final String PORTLETLIST_TAG = "PORTLETGROUP"; 77 private static final String PORTLET_TAG = "PORTLET"; 78 private static final String XML_ENCODING = "ISO-8859-1"; 79 80 private Document rootDocument = null; 81 private boolean addPortletGroup = false; 82 private File theFile; 83 private String theXMLfile; 84 private String theDefaultXMLfile; 85 private String paramAttrNode; 86 private Text theTextNode; 87 private Element portletGroupElement; 88 private Element plmlElement; 89 90 93 private Vector thePortletList; 94 private int thePageID; 95 private int maxColumn = 0; 96 97 100 PortletsXMLSerializer(String theTemplatesPathDirectory, String thefilename, 101 String theDefaultfilename, int thePageID) throws JahiaException { 102 initXMLSerializer(theTemplatesPathDirectory, thefilename, 103 theDefaultfilename, thePageID, "PortletList"); 104 } 105 106 107 110 PortletsXMLSerializer(String theTemplatesPathDirectory, String thefilename, 111 String theDefaultfilename, int thePageID, 112 String portletGroupName) throws JahiaException { 113 114 initXMLSerializer(theTemplatesPathDirectory, thefilename, 115 theDefaultfilename, thePageID, portletGroupName); 116 } 118 private void initXMLSerializer(String theTemplatesPathDirectory, 119 String thefilename, 120 String theDefaultfilename, int thePageID, 121 String portletGroupName) 122 throws JahiaException { 123 File thePortletsAPIDirectory = new File ( theTemplatesPathDirectory + 124 File.separator + 125 "portlets_api" + 126 File.separator ); 127 if(!thePortletsAPIDirectory.exists()) { 128 thePortletsAPIDirectory.mkdirs(); 129 } 130 String thePath = theTemplatesPathDirectory + File.separator + 131 "portlets_api" + File.separator; 132 this.addPortletGroup = false; 133 this.theFile = new File ( thePath + thefilename); 134 this.theXMLfile = thePath + thefilename; 135 this.theDefaultXMLfile = thePath + theDefaultfilename; 136 this.thePageID = thePageID; 137 138 if (!theFile.exists()) { 139 140 142 JahiaConsole.println("PortletsXMLSerializer constructor", 143 "Creating file " + theXMLfile + "..."); 144 145 try { 146 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 147 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 149 rootDocument = docBuilder.newDocument(); 150 Element plmlElement = rootDocument.createElement(PLML_TAG); 152 rootDocument.appendChild(plmlElement); 153 154 addPortletGroup(thePageID); 155 156 saveFile(theFile.toString()); 157 loadFile(theFile.toString()); 158 159 addPortletGroup = true; 160 161 } catch (Throwable t) { 162 t.printStackTrace(); 163 throw new JahiaException("PortletXMLSerializer", 164 "Error while creating new XML file", 165 JahiaException.CRITICAL_SEVERITY, 166 JahiaException.DATA_ERROR, 167 t); 168 } 169 170 171 } else { 172 174 JahiaConsole.println("PortletsXMLSerializer constructor", "Loading file " + theXMLfile + "..."); 175 try { 176 loadFile( theXMLfile ); 177 } catch (Throwable t) { 178 t.printStackTrace(); 179 throw new JahiaException ( "PortletsXMLSerializer", 180 "Error loading XML file", 181 JahiaException.CRITICAL_SEVERITY, 182 JahiaException.DATA_ERROR, 183 t ); 184 } 185 186 } 187 188 plmlElement = findPlml(rootDocument.getDocumentElement()); 189 portletGroupElement = findPortletGroup(plmlElement, thePageID); 190 } 191 192 private void createFile(String newFileName) 193 throws JahiaException { 194 196 saveFile(newFileName); 197 } 198 199 private void loadFile(String sourceFileName) 200 throws ParserConfigurationException , IOException , SAXException { 201 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 202 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 204 FileInputStream sourceStream = new FileInputStream (sourceFileName); 205 rootDocument = docBuilder.parse(sourceStream); 206 rootDocument.normalize(); 208 } 209 210 private void saveFile(String destinationFileName) 211 throws JahiaException 212 { 213 214 try { 215 216 rootDocument.normalize(); 218 TransformerFactory tfactory = TransformerFactory.newInstance(); 219 220 Transformer serializer = tfactory.newTransformer(); 223 224 serializer.setOutputProperty(OutputKeys.METHOD, "xml"); 225 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 226 FileOutputStream fileStream = new FileOutputStream (destinationFileName); 227 serializer.transform(new DOMSource (rootDocument), 228 new StreamResult (fileStream)); 229 230 } catch (TransformerConfigurationException tce) { 231 throw new JahiaException ("PortletXMLSerializer.saveFile", 232 "Error in XSL transformer configuration", 233 JahiaException.WARNING_SEVERITY, 234 JahiaException.CONFIG_ERROR, tce); 235 } catch (TransformerException te) { 236 throw new JahiaException ("PortletXMLSerializer.saveFile", 237 "Error in XSL transformer exception", 238 JahiaException.WARNING_SEVERITY, 239 JahiaException.CONFIG_ERROR, te); 240 } catch (FileNotFoundException fnfe) { 241 throw new JahiaException ("PortletXMLSerializer.saveFile", 242 "File not found exception", 243 JahiaException.WARNING_SEVERITY, 244 JahiaException.CONFIG_ERROR, fnfe); 245 } 246 247 } 248 249 252 private Element findPlml(Element plmlElement) throws JahiaException { 253 254 if (plmlElement == null) { 255 throw new JahiaException ("PortletXMLSerializer.findPlml", 256 ERROR_READING_PLML_FILE_MSG, 257 JahiaException.WARNING_SEVERITY, 258 JahiaException.CONFIG_ERROR); 259 } 260 261 if (plmlElement.getTagName().equals(PLML_TAG)) { 262 } else { 263 throw new JahiaException ("PortletXMLSerializer.findPlml", 264 ERROR_READING_PLML_FILE_MSG, 265 JahiaException.WARNING_SEVERITY, 266 JahiaException.CONFIG_ERROR); 267 } 268 269 return (Element ) plmlElement; 270 } 271 272 278 private Element findPortletGroup(Element groupParent, int groupID) 279 throws JahiaException { 280 NodeList groupList = groupParent.getElementsByTagName(PORTLETLIST_TAG); 282 for (int i = 0; i < groupList.getLength(); i++) { 283 Element groupElement = (Element ) groupList.item(i); 284 String pageIDStr = getGroupParameterValue(groupElement, "page_id"); 285 if (pageIDStr != null) { 286 if (groupID == Integer.parseInt(pageIDStr)) 288 return groupElement; 289 } 290 } 291 throw new JahiaException ("PortletXMLSerializer.findPortletGroup", 292 ERROR_READING_PLML_FILE_MSG, 293 JahiaException.WARNING_SEVERITY, 294 JahiaException.CONFIG_ERROR); 295 } 296 297 303 private Element findPortlet(Element portletParent, int portletID) 304 throws JahiaException { 305 NodeList portletList = portletParent.getElementsByTagName(PORTLET_TAG); 306 for (int i = 0; i < portletList.getLength(); i++) { 307 Element portletElement = (Element ) portletList.item(i); 308 String idStr = portletElement.getAttribute("id"); 309 if (idStr != null) { 310 if (portletID == Integer.parseInt(idStr)) 311 return portletElement; 312 } 313 } 314 throw new JahiaException ("PortletXMLSerializer.findPortlet", 315 ERROR_READING_PLML_FILE_MSG, 316 JahiaException.WARNING_SEVERITY, 317 JahiaException.CONFIG_ERROR); 318 } 319 320 323 private String getPortletParameterValue(Element paramParent, String parameterName) 324 throws JahiaException { 325 return getParameterValue(paramParent, PORTLET_PARAMETER_TAG, parameterName); 326 } 327 328 331 private String getGroupParameterValue(Element paramParent, String parameterName) 332 throws JahiaException { 333 return getParameterValue(paramParent, GROUP_PARAMETER_TAG, parameterName); 334 } 335 336 341 private String getParameterValue(Element paramParent, 342 String parameterTagName, 343 String parameterName) 344 throws JahiaException { 345 346 349 NodeList paramList = paramParent.getElementsByTagName(parameterTagName); 350 for (int i = 0; i < paramList.getLength(); i++) { 352 Element paramElement = (Element ) paramList.item(i); 353 String paramName = paramElement.getAttribute("name"); 355 if (paramName != null) { 356 if (paramName.equalsIgnoreCase(parameterName)) { 357 Node textNode = paramElement.getFirstChild(); 360 if (textNode.getNodeType() == Node.TEXT_NODE) { 361 return textNode.getNodeValue(); 363 } else { 364 throw new JahiaException(ERROR_READING_PLML_FILE_MSG, 366 "Value of parameter is not in correct format, should only be text", 367 JahiaException.CRITICAL_SEVERITY, 368 JahiaException.CONFIG_ERROR); 369 } 370 } else { 371 } 373 } else { 374 throw new JahiaException(ERROR_READING_PLML_FILE_MSG, 376 "Parameter not found !", 377 JahiaException.CRITICAL_SEVERITY, 378 JahiaException.CONFIG_ERROR); 379 } 380 } 381 return null; 383 } 384 385 386 390 private String getAttributeValue(Node paramParent, String attributeName) throws JahiaException { 391 392 NamedNodeMap attrib = paramParent.getAttributes(); 393 Node paramAttrib = attrib.getNamedItem(attributeName); 394 return paramAttrib.getNodeValue(); 395 396 } 398 399 402 public int getColumnCount() throws JahiaException { 403 404 maxColumn = 0; 405 406 Element plmlElement = findPlml(rootDocument.getDocumentElement()); 407 Element portletGroupElement = findPortletGroup(plmlElement, thePageID); 408 409 NodeList portletList = portletGroupElement.getElementsByTagName(PORTLET_TAG); 410 for (int i = 0; i < portletList.getLength(); i++) { 411 Element portletElement = (Element ) portletList.item(i); 412 String columnPosStr = getPortletParameterValue(portletElement, "column_position"); 413 int columnPos = Integer.parseInt(columnPosStr); 414 if (columnPos > maxColumn) { 415 maxColumn = columnPos; 416 } 417 } 418 419 return maxColumn; 420 } 422 429 private PortletBean xmlPortletToPortletBean(Element portletElement) 430 throws JahiaException { 431 String idStr = getAttributeValue(portletElement, PARAMETER_TAG_ID_ATTRIBUTE); 432 PortletBean thePortlet = new PortletBean(Integer.parseInt(idStr), 433 Integer.parseInt(getPortletParameterValue(portletElement, "source_id")), 434 Integer.parseInt(getPortletParameterValue(portletElement, "w")), 435 Integer.parseInt(getPortletParameterValue(portletElement, "h")), 436 Integer.parseInt(getPortletParameterValue(portletElement, "column_position")), 437 Integer.parseInt(getPortletParameterValue(portletElement, "row_position"))); 438 return thePortlet; 439 } 440 441 445 public PortletBean getPortlet(int thePortletID) throws JahiaException { 446 447 PortletBean thePortlet = null; 448 Element plmlElement = findPlml(rootDocument.getDocumentElement()); 449 Element portletGroupElement = findPortletGroup(plmlElement, thePageID); 450 Element portletElement = findPortlet(portletGroupElement, thePortletID); 451 thePortlet = xmlPortletToPortletBean(portletElement); 452 return thePortlet; 453 454 } 456 457 463 public Enumeration getPortlets() throws JahiaException { 464 465 thePortletList = new Vector (); 466 467 Element plmlElement = findPlml(rootDocument.getDocumentElement()); 468 Element portletGroupElement = findPortletGroup(plmlElement, thePageID); 469 NodeList portletNodeList = portletGroupElement.getElementsByTagName(PORTLET_TAG); 470 for (int i = 0; i < portletNodeList.getLength(); i++) { 471 Element portletElement = (Element ) portletNodeList.item(i); 472 PortletBean portletBean = xmlPortletToPortletBean(portletElement); 473 thePortletList.add(portletBean); 474 } 475 476 return thePortletList.elements(); 477 } 479 480 481 487 public Enumeration getPortletsFromColumn(int theColumn) throws JahiaException { 488 489 Enumeration allPortlets = getPortlets(); 490 Vector portletList = new Vector (); 491 while (allPortlets.hasMoreElements()) { 492 PortletBean portlet = (PortletBean) allPortlets.nextElement(); 493 if (portlet.getPortletColumn() == theColumn) { 494 portletList.add(portlet); 495 } 496 } 497 498 return thePortletList.elements(); 499 } 501 511 private void storePortletParameter(String parameterName, 512 String parameterValue, 513 Element portletElement) 514 throws JahiaException{ 515 NodeList parameterList = portletElement.getElementsByTagName(PORTLET_PARAMETER_TAG); 516 Element paramElement = null; 517 boolean foundParam = false; 518 for (int i = 0; i < parameterList.getLength(); i++) { 519 paramElement = (Element ) parameterList.item(i); 520 String paramNameValue = paramElement.getAttribute(PARAMETER_TAG_NAME_ATTRIBUTE); 521 if (paramNameValue != null) { 522 if (paramNameValue.equalsIgnoreCase(parameterName)) { 523 foundParam = true; 524 break; 525 } 526 } 527 } 528 if (!foundParam) { 529 Element parameterElement = rootDocument.createElement(PORTLET_PARAMETER_TAG); 531 parameterElement.setAttribute(PARAMETER_TAG_NAME_ATTRIBUTE, parameterName); 532 portletElement.appendChild(parameterElement); 533 Text parameterValueElement = rootDocument.createTextNode(""); 534 parameterElement.appendChild(parameterValueElement); 535 paramElement = parameterElement; 536 } 537 Node textNode = paramElement.getFirstChild(); 538 if (textNode.getNodeType() == Node.TEXT_NODE) { 539 Text paramValueText = (Text ) textNode; 540 paramValueText.setData(parameterValue); 541 } else { 542 throw new JahiaException("PortletsXMLSerializer", 543 "Invalid parameter value in DOM tree !", 544 JahiaException.CRITICAL_SEVERITY, 545 JahiaException.DATA_ERROR); 546 } 547 } 548 549 554 public void storePortlet(PortletBean portlet, 555 String currentUserName) 556 throws JahiaException { 557 Element plmlElement = findPlml(rootDocument.getDocumentElement()); 558 Element portletGroupElement = findPortletGroup(plmlElement, thePageID); 559 Element portletElement = null; 560 try { 561 portletElement = findPortlet(portletGroupElement, portlet.getPortletID()); 562 } catch (JahiaException je) { 563 Element newPortletElement = rootDocument.createElement(PORTLET_TAG); 565 newPortletElement.setAttribute("id", Integer.toString(update_count("portlet"))); 566 portletGroupElement.appendChild(newPortletElement); 567 portletElement = newPortletElement; } 569 570 storePortletParameter("source_id", Integer.toString(portlet.getPortletSourceID()), portletElement); 571 storePortletParameter("w", Integer.toString(portlet.getPortletW()), portletElement); 572 storePortletParameter("h", Integer.toString(portlet.getPortletH()), portletElement); 573 storePortletParameter("column_position", Integer.toString(portlet.getPortletColumn()), portletElement); 574 storePortletParameter("row_position", Integer.toString(portlet.getPortletRow()), portletElement); 575 576 saveFile(theFile.toString()); 578 579 } 580 581 584 public void deletePortlet(int theSourceID) 585 throws JahiaException { 586 Element plmlElement = findPlml (rootDocument.getDocumentElement()); 587 Element portletGroupElement = findPortletGroup(plmlElement, thePageID); 588 NodeList portletList = portletGroupElement.getElementsByTagName(PORTLET_TAG); 589 for (int i = 0; i < portletList.getLength(); i++) { 590 Element portletElement = (Element ) portletList.item(i); 591 String sourceIDStr = getPortletParameterValue(portletElement, "source_id"); 592 int sourceID = Integer.parseInt(sourceIDStr); 593 if (sourceID == theSourceID) { 594 portletGroupElement.removeChild(portletElement); 595 return; 596 } 597 } 598 599 saveFile(theFile.toString()); 601 602 } 604 608 public void addPortletGroup(int groupID) 609 throws JahiaException { 610 611 JahiaConsole.println("PortletsXMLSerializer.addPortletGroup", "groupID = " + Integer.toString(groupID) ); 612 613 if (addPortletGroup) { return; } 615 JahiaConsole.println("PortletsXMLSerializer.addPortletGroup", "Finding PLML group " + Integer.toString(groupID) ); 616 617 try { 618 619 Element plmlElement = findPlml(rootDocument.getDocumentElement()); 620 621 Element newPortletGroupElement = rootDocument.createElement(PORTLETLIST_TAG); 622 newPortletGroupElement.setAttribute( PARAMETER_TAG_ID_ATTRIBUTE, 623 Integer.toString(update_count("portletgroup"))); 624 plmlElement.appendChild(newPortletGroupElement); 625 626 Element groupParamElement = rootDocument.createElement(GROUP_PARAMETER_TAG); 627 groupParamElement.setAttribute(PARAMETER_TAG_NAME_ATTRIBUTE, "page_id"); 628 newPortletGroupElement.appendChild(groupParamElement); 629 630 Text groupParamText = rootDocument.createTextNode(Integer.toString(groupID)); 631 groupParamElement.appendChild(groupParamText); 632 633 } catch (Throwable t) { 634 t.printStackTrace(); 635 throw new JahiaException("PortletsXMLSerializer.addPortletGroup", 636 "Error while adding portlet group !", 637 JahiaException.CRITICAL_SEVERITY, 638 JahiaException.DATA_ERROR); 639 640 } 641 642 saveFile(theFile.toString()); 644 645 } 647 653 public int update_count(String theType) 654 throws JahiaException 655 { 656 int thePortletsCount = 0; 657 int theDesktopsCount = 0; 658 try { 659 660 if (theType.equals("portlet")) { 661 thePortletsCount = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( "jahiatemplates_portlet" ); 662 } else { 663 theDesktopsCount = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( "jahiatemplates_portletgroup" ); 664 } 665 666 } catch ( Exception se ) { 667 String errorMsg = "Error in update_count : " + se.getMessage(); 668 JahiaConsole.println( "JahiaDBManager", errorMsg + " -> BAILING OUT" ); 669 throw new JahiaException( "Cannot update count data to the database", 670 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY ); 671 } 672 if (theType.equals("portlet")) { 673 return thePortletsCount; 674 } else { 675 return theDesktopsCount; 676 } 677 678 } 680 } 681 | Popular Tags |