001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.ffj.util;
007:
008: import java.util.Iterator;
009: import java.util.Vector;
010: import java.util.StringTokenizer;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014:
015: import org.openide.xml.XMLUtil;
016:
017: import org.xml.sax.InputSource;
018:
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021: import org.w3c.dom.Node;
022: import org.w3c.dom.NodeList;
023: import org.w3c.dom.Text;
024:
025: public class PortletList implements PSConstants {
026:
027: public PortletList(File root) {
028: m_Root = root;
029: }
030:
031: public Iterator portletClasses() {
032: Vector v = new Vector();
033:
034: addClasses(m_Root, "", v);
035:
036: return v.iterator();
037: }
038:
039: public String portletName(String cls) {
040:
041: Element elt = null;
042: try {
043: elt = portletElement(cls);
044: } catch (Exception ex) {
045: return "? error obtaining portlet name for " + cls + " - "
046: + ex;
047: }
048: if (elt == null) {
049: return "? no .portlet file for " + cls;
050: }
051:
052: NodeList kids = elt.getChildNodes();
053:
054: for (int i = 0; i < kids.getLength(); ++i) {
055: Node nd = kids.item(i);
056: if (nd instanceof Element
057: && nd.getNodeName().equals("portlet-name")) {
058: Element pnelt = (Element) nd;
059: NodeList pnkids = pnelt.getChildNodes();
060: StringBuffer buf = new StringBuffer();
061: for (int j = 0; j < pnkids.getLength(); ++j) {
062: Node pnd = pnkids.item(j);
063: if (pnd instanceof Text) {
064: buf.append(pnd.getNodeValue().trim());
065: }
066: }
067: return buf.toString();
068: }
069: }
070:
071: return "? Portlet name not found for " + cls;
072: }
073:
074: public Element portletElement(String cls) throws Exception {
075:
076: File pf = portletXMLFile(cls);
077:
078: if (!pf.exists()) {
079: return null;
080: }
081:
082: Document doc = XMLUtil.parse(new InputSource(
083: new FileInputStream(pf)), false, false, null, null);
084: return doc.getDocumentElement();
085: }
086:
087: public File portletXMLFile(String cls) {
088: return new File(m_Root, classToPath(cls) + "." + PORTLET_EXT);
089: }
090:
091: // Recursive descent to find all portlet classes, indicated by
092: // a .java file and a .portlet file of the same name
093:
094: private void addClasses(File dir, String pfx, Vector v) {
095: File f[] = dir.listFiles();
096: for (int i = 0; i < f.length; ++i) {
097: if (f[i].isDirectory()) {
098: addClasses(f[i], pfx.length() == 0 ? f[i].getName()
099: : pfx + "." + f[i].getName(), v);
100: continue;
101: }
102: String name = f[i].getName();
103: int idx = name.lastIndexOf('.');
104: if (idx <= 0) {
105: continue;
106: }
107: String ext = name.substring(idx + 1);
108: if (ext.equals("java")) {
109: name = name.substring(0, idx);
110: File pxml = new File(f[i].getParent(), name + "."
111: + PORTLET_EXT);
112: if (pxml.exists()) {
113: if (pfx.length() == 0) {
114: v.add(name);
115: } else {
116: v.add(pfx + "." + name);
117: }
118: }
119: }
120: }
121: }
122:
123: private String classToPath(String cls) {
124:
125: StringBuffer buf = new StringBuffer();
126: String pfx = "";
127:
128: StringTokenizer toks = new StringTokenizer(cls, ".");
129: while (toks.hasMoreTokens()) {
130: buf.append(pfx);
131: pfx = File.separator;
132: buf.append(toks.nextToken());
133: }
134:
135: return buf.toString();
136: }
137:
138: private File m_Root;
139: }
|