1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.model.impl;
16  
17  import com.liferay.portal.kernel.util.Validator;
18  import com.liferay.portal.kernel.xml.Document;
19  import com.liferay.portal.kernel.xml.Element;
20  import com.liferay.portal.kernel.xml.SAXReaderUtil;
21  import com.liferay.portlet.journal.model.JournalStructure;
22  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
23  
24  import java.util.Iterator;
25  
26  /**
27   * <a href="JournalStructureImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class JournalStructureImpl
32      extends JournalStructureModelImpl implements JournalStructure {
33  
34      public JournalStructureImpl() {
35      }
36  
37      public String getMergedXsd() {
38          String parentStructureId = getParentStructureId();
39  
40          String xsd = getXsd();
41  
42          if (Validator.isNull(parentStructureId)) {
43              return xsd;
44          }
45  
46          try {
47              JournalStructure parentStructure =
48                  JournalStructureLocalServiceUtil.getStructure(
49                      getGroupId(), parentStructureId);
50  
51              Document doc = SAXReaderUtil.read(getXsd());
52  
53              Element root = doc.getRootElement();
54  
55              Document parentDoc = SAXReaderUtil.read(
56                  parentStructure.getMergedXsd());
57  
58              Element parentRoot = parentDoc.getRootElement();
59  
60              addParentStructureId(parentRoot, parentStructureId);
61  
62              root.content().addAll(0, parentRoot.content());
63  
64              xsd = root.asXML();
65          }
66          catch (Exception e) {
67          }
68  
69          return xsd;
70      }
71  
72      protected void addParentStructureId(
73          Element parentEl, String parentStructureId) {
74  
75          Iterator<Element> itr = parentEl.elements(_DYNAMIC_ELEMENT).iterator();
76  
77          while (itr.hasNext()) {
78              Element dynamicEl = itr.next();
79  
80              dynamicEl.addAttribute(_PARENT_STRUCTURE_ID, parentStructureId);
81  
82              addParentStructureId(dynamicEl, parentStructureId);
83          }
84      }
85  
86      private static final String _DYNAMIC_ELEMENT = "dynamic-element";
87  
88      private static final String _PARENT_STRUCTURE_ID = "parent-structure-id";
89  
90  }