001: /* Copyright 2003 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.layout.simple;
007:
008: import java.util.Enumeration;
009: import java.util.Vector;
010:
011: import javax.xml.xpath.XPath;
012: import javax.xml.xpath.XPathConstants;
013: import javax.xml.xpath.XPathFactory;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.jasig.portal.PortalException;
018: import org.jasig.portal.layout.IUserLayout;
019: import org.jasig.portal.layout.LayoutEventListener;
020: import org.jasig.portal.layout.node.IUserLayoutFolderDescription;
021: import org.jasig.portal.layout.node.IUserLayoutNodeDescription;
022: import org.jasig.portal.layout.node.UserLayoutNodeDescription;
023: import org.jasig.portal.utils.XML;
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Element;
026: import org.w3c.dom.Node;
027: import org.w3c.dom.NodeList;
028: import org.xml.sax.ContentHandler;
029:
030: /**
031: * The simple user layout implementation. This
032: * layout is based on a Document.
033: *
034: * Prior to uPortal 2.5, this class existed in the org.jasig.portal.layout package.
035: * It was moved to its present package to reflect that it is part of the
036: * Simple Layout Management implementation.
037: *
038: * @author Ken Weiner, kweiner@unicon.net
039: * @version $Revision: 36590 $
040: */
041: public class SimpleLayout implements IUserLayout {
042:
043: private Document layout;
044: private String layoutId;
045: private String cacheKey;
046:
047: private Log log = LogFactory.getLog(getClass());
048:
049: public SimpleLayout(String layoutId, Document layout) {
050: this .layoutId = layoutId;
051: this .layout = layout;
052: }
053:
054: public void writeTo(ContentHandler ch) throws PortalException {
055: try {
056: XML.dom2sax(layout, ch);
057: } catch (Exception e) {
058: throw new PortalException(e);
059: }
060: }
061:
062: public void writeTo(String nodeId, ContentHandler ch)
063: throws PortalException {
064: try {
065: XML.dom2sax(layout.getElementById(nodeId), ch);
066: } catch (Exception e) {
067: throw new PortalException(e);
068: }
069: }
070:
071: public void writeTo(Document document) throws PortalException {
072: document.appendChild(document.importNode(layout
073: .getDocumentElement(), true));
074: }
075:
076: public void writeTo(String nodeId, Document document)
077: throws PortalException {
078: document.appendChild(document.importNode(layout
079: .getElementById(nodeId), true));
080: }
081:
082: public IUserLayoutNodeDescription getNodeDescription(String nodeId)
083: throws PortalException {
084: Element element = (Element) layout.getElementById(nodeId);
085: return UserLayoutNodeDescription
086: .createUserLayoutNodeDescription(element);
087: }
088:
089: public String getParentId(String nodeId) throws PortalException {
090: String parentId = null;
091: Element element = (Element) layout.getElementById(nodeId);
092: if (element != null) {
093: Node parent = element.getParentNode();
094: if (parent != null
095: && parent.getNodeType() == Node.ELEMENT_NODE) {
096: Element parentE = (Element) parent;
097: parentId = parentE.getAttribute("ID");
098: }
099: }
100: return parentId;
101: }
102:
103: public Enumeration getChildIds(String nodeId)
104: throws PortalException {
105: Vector v = new Vector();
106: IUserLayoutNodeDescription node = getNodeDescription(nodeId);
107: if (node instanceof IUserLayoutFolderDescription) {
108: Element element = (Element) layout.getElementById(nodeId);
109: for (Node n = element.getFirstChild(); n != null; n = n
110: .getNextSibling()) {
111: if (n.getNodeType() == Node.ELEMENT_NODE) {
112: Element e = (Element) n;
113: if (e.getAttribute("ID") != null) {
114: v.add(e.getAttribute("ID"));
115: }
116: }
117: }
118: }
119: return v.elements();
120: }
121:
122: public String getNextSiblingId(String nodeId)
123: throws PortalException {
124: String nextSiblingId = null;
125: Element element = (Element) layout.getElementById(nodeId);
126: if (element != null) {
127: Node sibling = element.getNextSibling();
128: // Find the next element node
129: while (sibling != null
130: && sibling.getNodeType() != Node.ELEMENT_NODE) {
131: sibling = sibling.getNextSibling();
132: }
133: if (sibling != null) {
134: Element e = (Element) sibling;
135: nextSiblingId = e.getAttribute("ID");
136: }
137: }
138: return nextSiblingId;
139: }
140:
141: public String getPreviousSiblingId(String nodeId)
142: throws PortalException {
143: String prevSiblingId = null;
144: Element element = (Element) layout.getElementById(nodeId);
145: if (element != null) {
146: Node sibling = element.getPreviousSibling();
147: // Find the previous element node
148: while (sibling != null
149: && sibling.getNodeType() != Node.ELEMENT_NODE) {
150: sibling = sibling.getPreviousSibling();
151: }
152: if (sibling != null) {
153: Element e = (Element) sibling;
154: prevSiblingId = e.getAttribute("ID");
155: }
156: }
157: return prevSiblingId;
158: }
159:
160: public String getCacheKey() throws PortalException {
161: return cacheKey;
162: }
163:
164: public boolean addLayoutEventListener(LayoutEventListener l) {
165: // TODO: Implement this!
166: return false;
167: }
168:
169: public boolean removeLayoutEventListener(LayoutEventListener l) {
170: // TODO: Implement this!
171: return false;
172: }
173:
174: public String getId() {
175: return layoutId;
176: }
177:
178: public String getNodeId(String fname) throws PortalException {
179: String nodeId = null;
180: NodeList nl = layout.getElementsByTagName("channel");
181: for (int i = 0; i < nl.getLength(); i++) {
182: Node node = nl.item(i);
183: if (node.getNodeType() == Node.ELEMENT_NODE) {
184: Element channelE = (Element) node;
185: if (fname.equals(channelE.getAttribute("fname"))) {
186: nodeId = channelE.getAttribute("ID");
187: break;
188: }
189: }
190: }
191: return nodeId;
192: }
193:
194: public Enumeration getNodeIds() throws PortalException {
195: Vector v = new Vector();
196: try {
197: String expression = "*";
198: XPathFactory fac = XPathFactory.newInstance();
199: XPath xpath = fac.newXPath();
200: NodeList nl = (NodeList) xpath.evaluate(expression, layout,
201: XPathConstants.NODESET);
202: for (int i = 0; i < nl.getLength(); i++) {
203: Node node = nl.item(i);
204: if (node.getNodeType() == Node.ELEMENT_NODE) {
205: Element e = (Element) node;
206: v.add(e.getAttribute("ID"));
207: }
208: }
209: } catch (Exception e) {
210: log.error("Exception getting node ids.", e);
211: }
212: return v.elements();
213: }
214:
215: public String getRootId() {
216: String rootNode = null;
217: try {
218:
219: String expression = "/layout/folder";
220: XPathFactory fac = XPathFactory.newInstance();
221: XPath xpath = fac.newXPath();
222: Element rootNodeE = (Element) xpath.evaluate(expression,
223: layout, XPathConstants.NODE);
224:
225: rootNode = rootNodeE.getAttribute("ID");
226: } catch (Exception e) {
227: log.error("Error getting root id.", e);
228: }
229: return rootNode;
230: }
231:
232: }
|