01: /*
02: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05:
06: package com.sun.portal.harness;
07:
08: import java.io.File;
09: import java.io.FileInputStream;
10:
11: import javax.xml.parsers.DocumentBuilder;
12: import javax.xml.parsers.DocumentBuilderFactory;
13:
14: import org.w3c.dom.Document;
15: import org.w3c.dom.Element;
16: import org.w3c.dom.Attr;
17: import org.w3c.dom.NodeList;
18: import org.w3c.dom.Node;
19:
20: import com.sun.portal.desktop.dp.xml.XMLDPTags;
21:
22: // Wrapper class for exercising one of the portal servers's pre-defined
23: // provider classes against the user's component xml. It simply provides
24: // the provider class to be instantiated, and the component file given
25: // the default file name.
26:
27: public abstract class ProviderSpecifier implements XMLDPTags {
28:
29: // getComponentXML will be called first, and if it changes defaultXML, the
30: // changed value will be used with getWrappedProvider
31:
32: public abstract String getComponentXML(String defaultXML)
33: throws ProviderHarnessException;
34:
35: public abstract String getWrappedProvider(String defaultXML)
36: throws ProviderHarnessException;
37:
38: protected String findProviderInXML(String xml)
39: throws ProviderHarnessException {
40:
41: if (xml == null) {
42: throw new ProviderHarnessException(
43: "Null component XML file in findProviderInXML().");
44: }
45:
46: File f = new File(xml);
47: if (!f.exists())
48: throw new ProviderHarnessException(
49: "Component XML file does not exist");
50:
51: try {
52: DocumentBuilderFactory dbf = DocumentBuilderFactory
53: .newInstance();
54: DocumentBuilder db = dbf.newDocumentBuilder();
55: db.setEntityResolver(new ParEntryEntityResolver());
56: Document doc = db.parse(new FileInputStream(f));
57:
58: Element elt = doc.getDocumentElement();
59:
60: if (elt == null) {
61: throw new ProviderHarnessException(
62: "No root node in document");
63: }
64:
65: NodeList kids = elt.getChildNodes();
66: for (int i = 0; i < kids.getLength(); ++i) {
67: Node child = kids.item(i);
68: if (child instanceof Element
69: && child.getNodeName().equals(CHANNEL_TAG)) {
70: Element chelt = (Element) child;
71:
72: Attr a = chelt.getAttributeNode("provider");
73: if (a == null) {
74: throw new ProviderHarnessException(
75: "channel tag has no provider.");
76: }
77:
78: return a.getValue();
79: }
80: }
81:
82: throw new ProviderHarnessException(CHANNEL_TAG
83: + " element not present in document.");
84:
85: } catch (Exception ex) {
86: throw new ProviderHarnessException(
87: "Exception reading XML document.");
88: }
89: }
90: }
|