001: /*
002: * Copyright 2002 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.portlet.cli;
007:
008: import java.util.List;
009: import java.util.ArrayList;
010: import java.util.Iterator;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileNotFoundException;
015: import java.io.FileOutputStream;
016: import java.io.OutputStreamWriter;
017: import java.io.IOException;
018:
019: import org.jdom.Document;
020: import org.jdom.Element;
021: import org.jdom.JDOMException;
022: import org.jdom.Namespace;
023: import org.jdom.input.SAXBuilder;
024:
025: /**
026: * This class creates test channels in an output display profile
027: * from a list of portlets defined in the TCKTestCases XML file.
028: * The output display profile can be loaded using the dpadmin
029: * command to load the test channel definitions:
030: * <code>./dpadmin modify -m -u "<Admdin DN>" -w <Password> -d "<Target
031: * node DN>" <output display profile path></code>
032: *
033: * This class can get two arguments: -i for location of the input
034: * TCK test cases xml file, -o for the output display profile path.
035: *
036: */
037: public class DPAddTCKPortlets {
038:
039: //*****************************************************
040: // Element name constants
041: //******************************************************
042: private static String PORTLET_NAMESPACE = "http://java.sun.com/xml/ns/portlet/portletTCK_1_0.xsd";
043:
044: private static String TEST_PORTLET = "test_portlet";
045: private static String PORTLET_NAME = "portlet_name";
046: private static String APP_NAME = "app_name";
047: private List _portlets = new ArrayList();
048: private List _portletChannels = new ArrayList();
049:
050: private void getPortlets(String pathname) {
051:
052: File masterFile = null;
053: FileInputStream inputStream = null;
054: Document doc = null;
055: SAXBuilder builder = new SAXBuilder(false);
056: Namespace namespace = Namespace.getNamespace(PORTLET_NAMESPACE);
057:
058: System.out.println("Reading TCK test portlets from: "
059: + pathname);
060:
061: try {
062: masterFile = new File(pathname);
063: inputStream = new FileInputStream(masterFile);
064: doc = builder.build(inputStream);
065: } catch (FileNotFoundException ex) {
066: ex.printStackTrace();
067: } catch (IOException e) {
068: e.printStackTrace();
069: } catch (JDOMException je) {
070: je.printStackTrace();
071: } finally {
072: try {
073: inputStream.close();
074: } catch (IOException ioe) {
075: ioe.printStackTrace();
076: }
077: }
078:
079: Iterator testCasesIter = doc.getRootElement().getChildren()
080: .iterator();
081: while (testCasesIter.hasNext()) {
082: Element testCaseElement = (Element) testCasesIter.next();
083:
084: // Get the name of the test
085: List testList = testCaseElement.getChildren(TEST_PORTLET,
086: namespace);
087:
088: // A portlet channel name will be: app_name + '0' +
089: // portlet_name, with all instances of '_' or '/' be
090: // replaced with '0'
091:
092: for (Iterator i = testList.iterator(); i.hasNext();) {
093: Element testElement = (Element) i.next();
094: String portletName = testElement.getChildTextTrim(
095: PORTLET_NAME, namespace);
096: String modPortletName = new String(portletName);
097: String appName = testElement.getChildTextTrim(APP_NAME,
098: namespace);
099: modPortletName = modPortletName.replace('_', '0');
100: modPortletName = modPortletName.replace('/', '0');
101: StringBuffer buf = new StringBuffer(appName.replace(
102: '_', '0'));
103: buf.append("0").append(modPortletName);
104: buf.append("|").append(appName);
105:
106: // Add it to the portlet channels list
107: _portletChannels.add(buf.toString());
108: // Add it to the portlets list
109: _portlets.add(portletName);
110: }
111: }
112: }
113:
114: private void writeOneChannel(String combinedName,
115: String portletName, OutputStreamWriter writer)
116: throws IOException {
117:
118: StringBuffer buf = new StringBuffer(128);
119: StringBuffer buf1 = new StringBuffer(128);
120: StringBuffer buf2 = new StringBuffer(128);
121: StringBuffer buf3 = new StringBuffer(128);
122: int index = combinedName.indexOf('|');
123: String portletChannelName = combinedName.substring(0, index);
124: String appName = combinedName.substring(index + 1, combinedName
125: .length());
126:
127: // construct the provider name
128: buf.append("__Portlet__");
129: buf.append(appName).append(".");
130: buf.append(portletName);
131:
132: buf1.append("<Channel name=\"");
133: buf1.append(portletChannelName);
134: buf1.append("\" ");
135: buf1.append("provider=\"");
136: buf1.append(buf.toString());
137: buf1.append("\">");
138: buf1.append("\n");
139: writer.write(buf1.toString());
140: writer.write(" <Properties>\n");
141:
142: buf2.append(" <String name=\"title\" value=\"");
143: buf2.append(portletName);
144: buf2.append("\"/>\n");
145: writer.write(buf2.toString());
146:
147: buf3.append(" <String name=\"description\" value=\"");
148: buf3.append(portletName);
149: buf3.append("\"/>\n");
150: writer.write(buf3.toString());
151:
152: writer
153: .write(" <String name=\"width\" value=\"thick\"/>\n");
154: writer.write(" </Properties>\n");
155: writer.write("</Channel>\n");
156: }
157:
158: private void addPortletsToDP(String pathname) {
159: File dpXMLFile = null;
160: FileOutputStream outputStream = null;
161: OutputStreamWriter writer = null;
162:
163: System.out.println("Adding test channels into: " + pathname);
164: try {
165: dpXMLFile = new File(pathname);
166: outputStream = new FileOutputStream(dpXMLFile);
167: writer = new OutputStreamWriter(outputStream, "UTF-8");
168: writer
169: .write("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n");
170: writer
171: .write("<!DOCTYPE DisplayProfile SYSTEM \"jar://resources/psdp.dtd\">\n");
172: writer
173: .write("<DisplayProfile version=\"1.0\" priority=\"10\">\n");
174: writer.write("<Properties/>\n");
175: writer.write("<Channels>\n");
176:
177: for (int i = 0; i < _portlets.size(); i++) {
178: writeOneChannel((String) _portletChannels.get(i),
179: (String) _portlets.get(i), writer);
180: }
181:
182: writer.write("</Channels>\n");
183: writer.write("<Providers/>\n");
184: writer.write("</DisplayProfile>\n");
185: } catch (FileNotFoundException ex) {
186: ex.printStackTrace();
187: } catch (SecurityException se) {
188: se.printStackTrace();
189: } catch (IOException ie) {
190: ie.printStackTrace();
191: } finally {
192: try {
193: writer.close();
194: outputStream.close();
195: } catch (IOException e) {
196: e.printStackTrace();
197: }
198:
199: }
200:
201: }
202:
203: public static void main(String[] args) {
204:
205: DPAddTCKPortlets dpAddTCKPortlets = new DPAddTCKPortlets();
206: String inputFileName = "portletTCKTestCases.xml";
207: String outputFileName = "dp-addchan.xml";
208:
209: int len = args.length;
210:
211: for (int i = 0; i < len; i++) {
212: if (i < len && args[i].equals("-i")) {
213: i++;
214: inputFileName = args[i++];
215: }
216: if (i < len && args[i].equals("-o")) {
217: i++;
218: outputFileName = args[i++];
219: }
220: }
221:
222: dpAddTCKPortlets.getPortlets(inputFileName);
223: dpAddTCKPortlets.addPortletsToDP(outputFileName);
224: }
225: }
|