KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > impl > tree > loader > marshallers > SchemeMarshaller


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.impl.tree.loader.marshallers;
10
11 import org.jboss.portal.cms.Content;
12 import org.jboss.portal.core.security.Scheme;
13 import org.jboss.portal.core.metadata.SchemeMetaData;
14 import org.jboss.portal.core.deployment.jboss.PortletAppDeployment;
15 import org.jboss.portal.core.plugins.security.ModelPlugin;
16 import org.jboss.portal.core.impl.tree.loader.ContentMarshaller;
17 import org.jboss.portal.common.util.XML;
18 import org.jboss.portal.common.util.SetMap;
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.Element JavaDoc;
21 import org.w3c.dom.Text JavaDoc;
22
23 import java.util.Set JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.io.ByteArrayInputStream JavaDoc;
26
27 /**
28  * Implementation for scheme object.
29  *
30  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
31  * @version $Revision: 1.1 $
32  */

33 public class SchemeMarshaller implements ContentMarshaller
34 {
35
36    public Object JavaDoc unmarshall(Content content) throws Exception JavaDoc
37    {
38       byte[] bytes = content.getBytes();
39       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
40       Document JavaDoc doc = XML.getDocumentBuilderFactory().newDocumentBuilder().parse(bais);
41       Element JavaDoc schemeElt = doc.getDocumentElement();
42
43       SchemeMetaData schemeMD = PortletAppDeployment.buildSchemeMetaData(schemeElt);
44       Scheme scheme = ModelPlugin.createScheme(schemeMD);
45
46       return scheme;
47    }
48
49    public Content marshall(Object JavaDoc o) throws Exception JavaDoc
50    {
51       Scheme scheme = (Scheme)o;
52       Document JavaDoc doc = XML.createDocument();
53       Element JavaDoc schemeElt = doc.createElement("scheme");
54       doc.appendChild(schemeElt);
55       marshall(scheme, new String JavaDoc[0], schemeElt);
56       String JavaDoc s = XML.toString(doc);
57       return new Content(s.getBytes("UTF-8"), "text/xml");
58    }
59
60    private void marshall(Scheme scheme, String JavaDoc[] parentPath, Element JavaDoc schemeElt)
61    {
62       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("/");
63       for (int i = 0; i < parentPath.length; i++)
64       {
65          String JavaDoc name = parentPath[i];
66          buffer.append(i == 0 ? "" : "/").append(name);
67       }
68       Element JavaDoc itemElt = schemeElt.getOwnerDocument().createElement("item");
69       schemeElt.appendChild(itemElt);
70       Element JavaDoc pathElt = schemeElt.getOwnerDocument().createElement("path");
71       Text JavaDoc text = pathElt.getOwnerDocument().createTextNode(buffer.toString());
72       pathElt.appendChild(text);
73       itemElt.appendChild(pathElt);
74
75       Set JavaDoc roles = scheme.getRoles(parentPath);
76       SetMap blih = new SetMap();
77       for (Iterator JavaDoc i = roles.iterator(); i.hasNext();)
78       {
79          String JavaDoc role = (String JavaDoc)i.next();
80          Set JavaDoc permissions = scheme.get(parentPath, role);
81          for (Iterator JavaDoc j = permissions.iterator(); j.hasNext();)
82          {
83             String JavaDoc permission = (String JavaDoc)j.next();
84             blih.put(permission, role);
85          }
86       }
87       for (Iterator JavaDoc i = blih.keySet().iterator(); i.hasNext();)
88       {
89          String JavaDoc permission = (String JavaDoc)i.next();
90          Element JavaDoc permissionElt = itemElt.getOwnerDocument().createElement("permission");
91
92          Element JavaDoc permisssionNameElt = permissionElt.getOwnerDocument().createElement("permission-name");
93          permisssionNameElt.appendChild(permisssionNameElt.getOwnerDocument().createTextNode(permission));
94          permissionElt.appendChild(permisssionNameElt);
95
96          for (Iterator JavaDoc j = blih.iterator(permission);j.hasNext();)
97          {
98             String JavaDoc role = (String JavaDoc)j.next();
99             Element JavaDoc roleNameElt = permissionElt.getOwnerDocument().createElement("role-name");
100             roleNameElt.appendChild(permissionElt.getOwnerDocument().createTextNode(role));
101             permissionElt.appendChild(roleNameElt);
102          }
103
104          //
105
itemElt.appendChild(permissionElt);
106       }
107
108       String JavaDoc[] childPath = new String JavaDoc[parentPath.length + 1];
109       System.arraycopy(parentPath, 0, childPath, 0, parentPath.length);
110       Set JavaDoc names = scheme.getChildrenNames(parentPath);
111       for (Iterator JavaDoc i = names.iterator(); i.hasNext();)
112       {
113          String JavaDoc name = (String JavaDoc)i.next();
114          childPath[parentPath.length] = name;
115          marshall(scheme, childPath, schemeElt);
116       }
117    }
118
119
120 }
121
Popular Tags