001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.dp.cli;
006:
007: import java.util.Map;
008: import java.util.Set;
009: import java.util.Iterator;
010: import java.util.Locale;
011: import java.util.ResourceBundle;
012: import java.util.PropertyResourceBundle;
013: import java.util.StringTokenizer;
014: import java.text.MessageFormat;
015:
016: import java.io.InputStream;
017: import java.io.Reader;
018: import java.io.ByteArrayInputStream;
019: import java.io.BufferedReader;
020: import java.io.IOException;
021:
022: import javax.xml.parsers.*;
023: import org.xml.sax.*;
024: import org.xml.sax.helpers.*;
025: import org.w3c.dom.*;
026:
027: import com.sun.portal.desktop.context.DPContext;
028: import com.sun.portal.desktop.dp.DPRoot;
029: import com.sun.portal.desktop.dp.DPNode;
030: import com.sun.portal.desktop.dp.DPChannel;
031: import com.sun.portal.desktop.dp.DPPropertyHolder;
032: import com.sun.portal.desktop.dp.DPContainerChannel;
033: import com.sun.portal.desktop.dp.DPProvider;
034: import com.sun.portal.desktop.dp.DPCollection;
035: import com.sun.portal.desktop.dp.DPAvailable;
036: import com.sun.portal.desktop.dp.DPSelected;
037: import com.sun.portal.desktop.dp.DPProperties;
038: import com.sun.portal.desktop.dp.DPProperty;
039: import com.sun.portal.desktop.dp.DPReferenceList;
040: import com.sun.portal.desktop.dp.xml.XMLDPFactory;
041: import com.sun.portal.desktop.dp.xml.XMLDPErrorHandler;
042: import com.sun.portal.desktop.dp.xml.XMLDPEntityResolver;
043: import com.sun.portal.desktop.dp.xml.XMLDPRoot;
044: import com.sun.portal.desktop.dp.xml.XMLDPCollection;
045: import com.sun.portal.desktop.dp.xml.XMLDPReferenceList;
046: import com.sun.portal.desktop.dp.xml.XMLDPChannel;
047: import com.sun.portal.desktop.dp.xml.XMLDPContainerChannel;
048: import com.sun.portal.desktop.dp.xml.XMLDPProvider;
049:
050: class DPAUtil {
051:
052: public static final String RESOURCE_BASE = "desktopDPA";
053: public static ResourceBundle rb = PropertyResourceBundle.getBundle(
054: RESOURCE_BASE, Locale.getDefault());
055:
056: public static final int CONTEXT_LINE_NUM = 5;
057:
058: public static void setLocale(java.util.Locale locale) {
059: // load resource bundle based on locale
060: rb = PropertyResourceBundle.getBundle(RESOURCE_BASE, locale);
061: }
062:
063: /**
064: * given a string representation of the locale (i.e. en_US)
065: * create a Locale obj.
066: */
067: public static Locale getLocale(String stringformat) {
068:
069: if (stringformat == null)
070: return Locale.getDefault();
071: StringTokenizer tk = new StringTokenizer(stringformat, "_");
072: String lang = "";
073: String country = "";
074: String variant = "";
075: if (tk.hasMoreTokens())
076: lang = tk.nextToken();
077: if (tk.hasMoreTokens())
078: country = tk.nextToken();
079: if (tk.hasMoreTokens())
080: variant = tk.nextToken();
081: return new java.util.Locale(lang, country, variant);
082:
083: }
084:
085: public static String getLocalizedString(String key) {
086: return rb.getString(key);
087: }
088:
089: public static String getLocalizedString(String key, Object[] objs) {
090:
091: if (objs != null && objs.length > 0) {
092: MessageFormat mf = new MessageFormat("");
093: mf.setLocale(rb.getLocale());
094: mf.applyPattern(rb.getString(key));
095: return mf.format(objs);
096: } else {
097: return rb.getString(key);
098: }
099: }
100:
101: /**
102: * Note that this method does NOT do DTD validation.
103: * However, if there is no doctype, then an exception is thrown.
104: */
105: public static Element getElement(DPContext adc,
106: InputStream xmlByteStream) throws DPAException {
107:
108: DocumentBuilderFactory dbf = DocumentBuilderFactory
109: .newInstance();
110:
111: DocumentBuilder db = null;
112: try {
113: dbf.setValidating(false);
114: db = dbf.newDocumentBuilder();
115: db.setErrorHandler(new XMLDPErrorHandler(adc));
116: db.setEntityResolver(new XMLDPEntityResolver());
117: } catch (Exception ex) {
118: throw new DPAException("errorDocumentBuilder", ex);
119: }
120:
121: Document d = null;
122: try {
123: xmlByteStream.reset();
124: InputSource is = new InputSource(xmlByteStream);
125:
126: d = db.parse(is);
127: } catch (SAXParseException spe) {
128: Object[] tokens = { Integer.toString(spe.getLineNumber()) };
129: throw new DPAException("errorXMLParseLine", spe, tokens);
130: } catch (Exception e) {
131: throw new DPAException("errorXMLParse", e);
132: }
133:
134: if (d.getDoctype() == null) {
135: throw new DPAException("errorMissingDocType");
136: }
137:
138: return d.getDocumentElement();
139: }
140:
141: /**
142: * combines content of newDpp into oldDpp. note that this one
143: * does not take merging into consideration.
144: */
145: public static void combineProperties(DPCollection oldDpp,
146: DPCollection newDpp) throws DPAException {
147:
148: combineCollections(oldDpp, newDpp);
149:
150: //
151: // reset merge attributes
152: //
153: XMLDPCollection xmlNewDpp = (XMLDPCollection) newDpp;
154: Element e = xmlNewDpp.getElement();
155: oldDpp.setMergeType(newDpp.getMergeType());
156: oldDpp.setLock(xmlNewDpp.isLockedElement(e));
157: oldDpp.setPropagate(xmlNewDpp.isPropagateElement(e));
158: oldDpp.setAdvanced(xmlNewDpp.isAdvancedElement(e));
159: }
160:
161: /**
162: * combines content of newDpl into oldDpl. note that this one
163: * does not take merging into consideration.
164: */
165: public static void combineLists(DPReferenceList oldDpl,
166: DPReferenceList newDpl) throws DPAException {
167:
168: combineCollections(oldDpl, newDpl);
169:
170: //
171: // reset merge attributes
172: //
173: XMLDPReferenceList xmlNewDpl = (XMLDPReferenceList) newDpl;
174: Element e = xmlNewDpl.getElement();
175: oldDpl.setMergeType(newDpl.getMergeType());
176: oldDpl.setLock(xmlNewDpl.isLockedElement(e));
177: oldDpl.setAdvanced(xmlNewDpl.isAdvancedElement(e));
178: }
179:
180: /**
181: * combines content of newDpc into oldDpc. note that this one
182: * does not take merging into consideration.
183: */
184: private static void combineCollections(DPCollection oldDpc,
185: DPCollection newDpc) throws DPAException {
186:
187: XMLDPCollection xnewDpc = (XMLDPCollection) newDpc;
188: Set names = xnewDpc.getNamesFromThis();
189:
190: for (Iterator i = names.iterator(); i.hasNext();) {
191: String name = (String) i.next();
192: DPProperty dpProperty = xnewDpc.getFromThis(name);
193: XMLDPCollection xoldDpc = (XMLDPCollection) oldDpc;
194: DPProperty oldDpProperty = xoldDpc.getFromThis(name);
195:
196: if (oldDpProperty == null) {
197: //
198: // add if not found
199: //
200: oldDpc.add(dpProperty);
201:
202: } else {
203:
204: if (dpProperty.getType() == oldDpProperty.getType()) {
205: if (dpProperty instanceof DPCollection) {
206: //
207: // call recursive combineCollections()
208: // if collection type is detected
209: //
210: DPCollection collection = (DPCollection) dpProperty;
211: DPCollection oldCollection = (DPCollection) oldDpProperty;
212: combineCollections(oldCollection, collection);
213: } else {
214: //
215: // replace existing property if non-collection type
216: //
217: oldDpc.remove(name);
218: oldDpc.add(dpProperty);
219: }
220: } else {
221: //
222: // raise error if types do not match
223: //
224: Object[] tokens = { name };
225: throw new DPAException("errorCombineConflict",
226: tokens);
227: }
228: }
229: }
230: }
231:
232: /**
233: * combines contents of newDpc into oldDpc. note that it
234: * does not take merging into consideration.
235: */
236: public static void combineChannels(DPChannel oldDpc,
237: DPChannel newDpc) throws DPAException {
238:
239: //
240: // raise error if types do not match
241: //
242: if (oldDpc.getType() != newDpc.getType()) {
243: Object[] tokens = { oldDpc.getName() };
244: throw new DPAException("errorCombineConflict", tokens);
245: }
246:
247: //
248: // combine properties
249: //
250: XMLDPChannel xoldDpc = (XMLDPChannel) oldDpc;
251: XMLDPChannel xnewDpc = (XMLDPChannel) newDpc;
252: DPProperties oprops = xoldDpc.getPropertiesFromThis();
253: DPProperties nprops = xnewDpc.getPropertiesFromThis();
254: combineProperties(oprops, nprops);
255:
256: if (oldDpc instanceof DPContainerChannel) {
257: //
258: // combine available/selected list
259: //
260: XMLDPContainerChannel xoldDpcc = (XMLDPContainerChannel) oldDpc;
261: XMLDPContainerChannel xnewDpcc = (XMLDPContainerChannel) newDpc;
262: DPAvailable odpa = xoldDpcc.getAvailableFromThis();
263: DPSelected odps = xoldDpcc.getSelectedFromThis();
264: DPAvailable ndpa = xnewDpcc.getAvailableFromThis();
265: DPSelected ndps = xnewDpcc.getSelectedFromThis();
266: combineLists(odpa, ndpa);
267: combineLists(odps, ndps);
268:
269: //
270: // container may contain child container(s)/channel(s)
271: //
272: Set names = xnewDpc.getChannelNamesFromThis();
273: for (Iterator i = names.iterator(); i.hasNext();) {
274: String name = (String) i.next();
275: DPChannel nch = xnewDpc.getChannelFromThis(name);
276: DPChannel och = null;
277: if ((och = xoldDpc.getChannelFromThis(name)) != null) {
278: //
279: // if found, recursively call combineChannels
280: //
281: combineChannels(och, nch);
282: } else {
283: //
284: // if not found, add
285: //
286: oldDpc.addChannel(nch);
287: }
288: }
289: }
290:
291: //
292: // reset merge attributes
293: //
294: XMLDPChannel xmlNewDpc = (XMLDPChannel) newDpc;
295: Element e = xmlNewDpc.getElement();
296: oldDpc.setProviderName(newDpc.getProviderName());
297: oldDpc.setMergeType(newDpc.getMergeType());
298: oldDpc.setLock(xmlNewDpc.isLockedElement(e));
299: oldDpc.setAdvanced(xmlNewDpc.isAdvancedElement(e));
300: }
301:
302: /**
303: * combines contents of newProv into oldProv. note that it
304: * does not take merging into consideration.
305: */
306: public static void combineProviders(DPProvider oldDpp,
307: DPProvider newDpp) throws DPAException {
308:
309: //
310: // combine properties
311: //
312: XMLDPProvider xoldDpp = (XMLDPProvider) oldDpp;
313: XMLDPProvider xnewDpp = (XMLDPProvider) newDpp;
314: DPProperties oprops = xoldDpp.getPropertiesFromThis();
315: DPProperties nprops = xnewDpp.getPropertiesFromThis();
316: combineProperties(oprops, nprops);
317:
318: //
319: // reset merge attributes
320: //
321: XMLDPProvider xmlNewDpp = (XMLDPProvider) newDpp;
322: Element e = xmlNewDpp.getElement();
323: oldDpp.setClassName(newDpp.getClassName());
324: oldDpp.setProviderVersion(newDpp.getProviderVersion());
325: oldDpp.setMergeType(newDpp.getMergeType());
326: oldDpp.setLock(xmlNewDpp.isLockedElement(e));
327: oldDpp.setAdvanced(xmlNewDpp.isAdvancedElement(e));
328: }
329:
330: /**
331: * combines contents of newDpc into oldDpc. note that it
332: * does not take merging into consideration.
333: */
334: public static void combineRoots(DPRoot oldDpr, DPRoot newDpr)
335: throws DPAException {
336:
337: //
338: // combine properties
339: //
340: XMLDPRoot xoldDpr = (XMLDPRoot) oldDpr;
341: XMLDPRoot xnewDpr = (XMLDPRoot) newDpr;
342: DPProperties oprops = xoldDpr.getPropertiesFromThis();
343: DPProperties nprops = xnewDpr.getPropertiesFromThis();
344: combineProperties(oprops, nprops);
345:
346: //
347: // combine channels
348: //
349: Set channels = xnewDpr.getChannelNamesFromThis();
350: for (Iterator i = channels.iterator(); i.hasNext();) {
351: String ch = (String) i.next();
352: DPChannel odpc = xoldDpr.getChannelFromThis(ch);
353: DPChannel ndpc = xnewDpr.getChannelFromThis(ch);
354: if (odpc != null) {
355: combineChannels(odpc, ndpc);
356: } else {
357: oldDpr.addChannel(ndpc);
358: }
359: }
360:
361: //
362: // combine providers
363: //
364: Set providers = xnewDpr.getProviderNamesFromThis();
365: for (Iterator i = providers.iterator(); i.hasNext();) {
366: String ch = (String) i.next();
367: DPProvider odpc = xoldDpr.getProviderFromThis(ch);
368: DPProvider ndpc = xnewDpr.getProviderFromThis(ch);
369: if (odpc != null) {
370: combineProviders(odpc, ndpc);
371: } else {
372: oldDpr.addProvider(ndpc);
373: }
374: }
375:
376: //
377: // reset merge attributes
378: //
379: XMLDPRoot xmlNewDpr = (XMLDPRoot) newDpr;
380: Element e = xmlNewDpr.getElement();
381: oldDpr.setVersion(newDpr.getVersion());
382: oldDpr.setMergeType(newDpr.getMergeType());
383: oldDpr.setLock(xmlNewDpr.isLockedElement(e));
384: oldDpr.setPriority(newDpr.getPriority());
385: oldDpr.setAdvanced(xmlNewDpr.isAdvancedElement(e));
386: }
387:
388: public static String getLines(Reader reader, int linenum) {
389: StringBuffer buf = new StringBuffer();
390: String line = null;
391: BufferedReader br = new BufferedReader(reader);
392:
393: try {
394: int i = 1;
395: int beg = linenum - CONTEXT_LINE_NUM;
396: int end = linenum + CONTEXT_LINE_NUM;
397: buf.append("\n");
398: while ((line = br.readLine()) != null) {
399: if (i == linenum) {
400: buf.append("(*)").append(line).append("\n");
401: } else if (i >= beg && i <= end) {
402: buf.append(" ").append(line).append("\n");
403: }
404: ++i;
405: }
406: buf.append("\n");
407: } catch (IOException ioe) {
408: return "";
409: }
410: return buf.toString();
411: }
412: }
|