0001: /*
0002: * Enhydra Java Application Server Project
0003: *
0004: * The contents of this file are subject to the Enhydra Public License
0005: * Version 1.1 (the "License"); you may not use this file except in
0006: * compliance with the License. You may obtain a copy of the License on
0007: * the Enhydra web site ( http://www.enhydra.org/ ).
0008: *
0009: * Software distributed under the License is distributed on an "AS IS"
0010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
0011: * the License for the specific terms governing rights and limitations
0012: * under the License.
0013: *
0014: * The Initial Developer of the Enhydra Application Server is Lutris
0015: * Technologies, Inc. The Enhydra Application Server and portions created
0016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
0017: * All Rights Reserved.
0018: *
0019: * Contributor(s):
0020: *
0021: * $Id: XMLConfigFile.java,v 1.3 2007-10-19 10:35:46 sinisa Exp $
0022: */
0023: package org.enhydra.util;
0024:
0025: import java.io.File;
0026: import java.io.FileInputStream;
0027: import java.io.FileOutputStream;
0028: import java.io.IOException;
0029: import java.io.OutputStream;
0030: import java.util.StringTokenizer;
0031: import java.util.Vector;
0032:
0033: import javax.xml.parsers.DocumentBuilder;
0034: import javax.xml.parsers.DocumentBuilderFactory;
0035:
0036: import org.apache.xml.serialize.OutputFormat;
0037: import org.apache.xml.serialize.XMLSerializer;
0038: import org.w3c.dom.Document;
0039: import org.w3c.dom.Node;
0040: import org.w3c.dom.NodeList;
0041: import org.xml.sax.EntityResolver;
0042: import org.xml.sax.InputSource;
0043:
0044: import com.lutris.util.Config;
0045: import com.lutris.util.ConfigException;
0046: import com.lutris.util.KeywordValueException;
0047: import com.lutris.util.KeywordValueTable;
0048: import com.lutris.util.ParseException;
0049:
0050: /**
0051: * XMLConfigFile is used to manipulate application's web.xml file to read its
0052: * configuration parameters.
0053: *
0054: * @see Config, ConfigFileInterface, AbsConfigFile
0055: * @author Tanja Jovanovic
0056: * @version 1.0
0057: */
0058:
0059: public class XMLConfigFile extends AbsConfigFile {
0060:
0061: /**
0062: * Document in which is xml document parsed.
0063: */
0064: private Document doc = null;
0065:
0066: /**
0067: * Root element (tag <web-app>) of document doc.
0068: */
0069: private Node xmlDoc = null;
0070:
0071: /**
0072: * Version of xml declaration of this xml file.
0073: */
0074: private String version;
0075:
0076: /**
0077: * Encoding of xml declaration of this xml file.
0078: */
0079: private String encoding;
0080:
0081: /**
0082: * Default constructor for an empty config file.
0083: */
0084: public XMLConfigFile() {
0085: super ();
0086: version = "";
0087: encoding = "";
0088: }
0089:
0090: /**
0091: * Constructor from an InputStream.
0092: * @param inputStream The input stream from which to parse the config file.
0093: * @exception ConfigException
0094: */
0095: /*
0096: public XMLConfigFile (InputStream inputStream) throws ConfigException {
0097: }
0098: */
0099:
0100: /**
0101: * Constructor from a File. Allows to later write back the configuration to the
0102: * same file.
0103: * @param file The local file to parse.
0104: * @exception IOException
0105: * @exception ConfigException
0106: */
0107: public XMLConfigFile(File file) throws ConfigException, IOException {
0108: super (file);
0109: version = "";
0110: encoding = "";
0111: // read version and endocing from xml declaration, if it exists
0112: this .readXmlDeclaration();
0113: // String name = file.getName();
0114: // handle public id from xml declaration
0115: // instead search on the Internet, the local dtd file is used
0116: EntityResolver er = new PublicIdResolver();
0117: DocumentBuilderFactory factory = DocumentBuilderFactory
0118: .newInstance();
0119: factory.setValidating(false);
0120: factory.setIgnoringElementContentWhitespace(false);
0121: try {
0122: DocumentBuilder builder = factory.newDocumentBuilder();
0123: builder.setEntityResolver(er);
0124: doc = builder.parse(file);
0125: } catch (Exception ex) {
0126: //ex.printStackTrace();
0127: throw new ConfigException(
0128: "Error in reading configuration parameters from file.");
0129:
0130: }
0131:
0132: NodeList nl = doc.getChildNodes();
0133: boolean found = false;
0134: int i = 0;
0135:
0136: // find root element of the Document (tag <web-app>)
0137: while (i < nl.getLength() && !found) {
0138: xmlDoc = nl.item(i);
0139: if (xmlDoc.getNodeName().equalsIgnoreCase("web-app")
0140: && xmlDoc.getNodeType() == Document.ELEMENT_NODE)
0141: found = true;
0142: i++;
0143: }
0144: // tj 08.02.2004 needed for JNDI with <context-param> tag
0145: // tj 06.05.2004 needed for JNDI with <env-entry> tag
0146: // tj 24.05.2004 put under comment: there is JNDI SPI for parsing unpacked web.xml file
0147: /*
0148: try {
0149: this.parseTree();
0150: }
0151: catch (Exception e) {
0152: throw new ConfigException(ConfigException.SYNTAX, e.getMessage());
0153: }
0154: */
0155: }
0156:
0157: /**
0158: * Constructor from a KeywordValueTable.
0159: * @param kvt A KeywordValueTable from which to populate the xml file.
0160: * @exception ConfigException
0161: */
0162: public XMLConfigFile(KeywordValueTable kvt) throws ConfigException {
0163: super (kvt);
0164: version = "";
0165: encoding = "";
0166: }
0167:
0168: /**
0169: * Reads application configuration parameters by using JNDI Context.
0170: */
0171: protected void readJndi() throws ConfigException {
0172: try {
0173: if (jndiAdapt == null) {
0174: try {
0175: jndiAdapt = new JNDIAdapter(this .file
0176: .getAbsolutePath(),
0177: "org.enhydra.spi.webxml.WebXmlInitialContextFactory");
0178: } catch (Exception e) {
0179: jndiAdapt = new JNDIAdapter();
0180: }
0181: }
0182: String[] leafKeys = jndiAdapt.leafKeys();
0183: if (leafKeys != null) {
0184: int leafKeysLength = leafKeys.length;
0185: String newKey = null;
0186: String stringValue;
0187: String[] stringValues;
0188: for (int i = 0; i < leafKeysLength; i++) {
0189: String leafKey = leafKeys[i];
0190: if (leafKey != null) {
0191: if (jndiAdapt.isArray(leafKey)) {
0192: newKey = jndiAdapt.removeArrayMark(leafKey);
0193: if (newKey != null) {
0194: stringValues = jndiAdapt
0195: .getStrings(newKey);
0196: newKey = jndiAdapt
0197: .makeConfigString(newKey);
0198: config.set(newKey, stringValues);
0199: if (!order.contains(newKey))
0200: order.addElement(newKey);
0201: comments.put(newKey, "");
0202: }
0203: } // if (jndiAdapt.isArray(leafKey))
0204: else {
0205: Object ovalue = jndiAdapt.get(leafKey);
0206: newKey = jndiAdapt
0207: .makeConfigString(leafKey);
0208:
0209: // stringValue = (String)jndiAdapt.get(leafKey);
0210: if (ovalue instanceof java.lang.String) {
0211: stringValue = (String) ovalue;
0212: if (stringValue.startsWith("jndi:")) {
0213: stringValue = stringValue
0214: .substring(5);
0215: Object resource = jndiAdapt
0216: .getResource(stringValue);
0217:
0218: if (resource != null) {
0219: config.set(newKey, resource);
0220: jndiParameterNames.put(newKey,
0221: stringValue);
0222: } else {
0223: config.set(newKey, "jndi:"
0224: + stringValue);
0225: }
0226: } // if (stringValue.startsWith("jndi:"))
0227: else {
0228: config.set(newKey, stringValue);
0229: }
0230: if (!order.contains(newKey))
0231: order.addElement(newKey);
0232: comments.put(newKey, "");
0233: } // if (ovalue instanceof java.lang.String)
0234: else {
0235: if (ovalue instanceof java.lang.Boolean) {
0236: Boolean booleanValue = (Boolean) ovalue;
0237: config.set(newKey, booleanValue);
0238: } // if (ovalue instanceof java.lang.Boolean)
0239: else {
0240: if (ovalue instanceof java.lang.Integer) {
0241: Integer integerValue = (Integer) ovalue;
0242: config
0243: .set(newKey,
0244: integerValue);
0245: } // if (ovalue instanceof java.lang.Integer)
0246: else {
0247: if (ovalue instanceof java.lang.Long) {
0248: Long longValue = (Long) ovalue;
0249: config.set(newKey,
0250: longValue);
0251: } // if (ovalue instanceof java.lang.Long)
0252: else {
0253: if (ovalue instanceof java.lang.Double) {
0254: Double doubleValue = (Double) ovalue;
0255: config.set(newKey,
0256: doubleValue);
0257: } // if (ovalue instanceof java.lang.Double)
0258: else {
0259: }
0260: }
0261: }
0262: }
0263: if (!order.contains(newKey)) {
0264: order.addElement(newKey);
0265: }
0266: comments.put(newKey, "");
0267: }
0268: } // else from if (jndiAdapt.isArray(leafKey))
0269: } // if (leafKey != null)
0270: } // for
0271: } // if (leafKeys != null)
0272: } // try
0273: catch (Exception e) {
0274: //System.err.println("Error readJndi");
0275: //e.printStackTrace();
0276: throw new ConfigException(
0277: "Error in reading JNDI configuration parameters.");
0278: }
0279: }
0280:
0281: /**
0282: * Reads version and encoding from xml declaration from xml file.
0283: * If there is no declaration defined, the default values are used in writting
0284: * into the xml file.
0285: * Default values are: version = "1.0" and encoding = "UTF-8".
0286: * @throws IOException
0287: */
0288: private void readXmlDeclaration() throws IOException {
0289: String xmlHeader = "";
0290: // int length;
0291: FileInputStream fi = new FileInputStream(file);
0292: byte[] bChar = new byte[1];
0293: int ok = fi.read(bChar);
0294: String tempString = "";
0295: if (ok != -1) {
0296: String ch = new String(bChar);
0297: if (ch.equalsIgnoreCase("<")) {
0298: tempString = tempString + ch;
0299: ok = fi.read(bChar);
0300: StringBuffer tempStringBuffer = new StringBuffer(
0301: tempString);
0302: while (ok != -1
0303: && !new String(bChar).equalsIgnoreCase(">")) {
0304: tempStringBuffer.append(new String(bChar));
0305: ok = fi.read(bChar);
0306: }
0307: tempString = tempStringBuffer.toString();
0308: if (ok != -1) {
0309: tempString = tempString + new String(bChar);
0310: if (tempString.length() >= 5) {
0311: if (tempString.substring(0, 5)
0312: .equalsIgnoreCase("<?xml")) {
0313: xmlHeader = tempString;
0314: }
0315: }
0316: }
0317: }
0318: }
0319: if (xmlHeader != null) {
0320: int index, startQuote, endQuote;
0321: index = xmlHeader.indexOf("version");
0322: if (index != -1) {
0323: startQuote = xmlHeader.indexOf("\"", (index + 7));
0324: if (startQuote != -1) {
0325: endQuote = xmlHeader.indexOf("\"", startQuote + 1);
0326: if ((endQuote != -1) && (startQuote + 1 < endQuote)) {
0327: version = xmlHeader.substring(startQuote + 1,
0328: endQuote);
0329: }
0330: }
0331: }
0332: index = xmlHeader.indexOf("encoding");
0333: if (index != -1) {
0334: startQuote = xmlHeader.indexOf("\"", (index + 8));
0335: if (startQuote != -1) {
0336: endQuote = xmlHeader.indexOf("\"", startQuote + 1);
0337: if ((endQuote != -1) && (startQuote + 1 < endQuote)) {
0338: encoding = xmlHeader.substring(startQuote + 1,
0339: endQuote);
0340: }
0341: }
0342: }
0343: }
0344: fi.close();
0345: }
0346:
0347: // tj 08.02.2004 needed for JNDI with <context-param> tag
0348: /**
0349: * Parses the Document doc, got by transformation of the xml file.
0350: * All configuration parameters (names, values and comments) are put in
0351: * the Config object's HashTable.
0352: * @throws ParseException
0353: * @throws KeywordValueException
0354: */
0355: /*
0356: private void parseTree() throws ParseException, KeywordValueException {
0357: Node web = xmlDoc;
0358: Node contextParam = null;
0359: Node param = null;
0360: Node paramValue = null;
0361: Node deleteNode = null;
0362:
0363: if ((web != null) && (web.getNodeName().equals("web-app"))) {
0364: contextParam = web.getFirstChild();
0365: while (contextParam != null) {
0366: if (contextParam.getNodeName().equals("context-param")){
0367: param = contextParam.getFirstChild();
0368: String paramNameString = null;
0369: String paramValueString = null;
0370: String commentString = null;
0371: while (param != null) {
0372: if (param.getNodeName().equals("param-name")){
0373: paramValue = param.getFirstChild();
0374: try {
0375: paramNameString = paramValue.getNodeValue();
0376: }
0377: catch (Exception ex ){
0378: throw new KeywordValueException("Error in parsing <param-name> tag.");
0379: }
0380: }
0381: else {
0382: if (param.getNodeName().equals("param-value")){
0383: paramValue = param.getFirstChild();
0384: if (paramValue == null){
0385: paramValueString = new String("");
0386: param.appendChild(doc.createTextNode(paramValueString));
0387: paramValue = param.getFirstChild();
0388: }
0389:
0390: else {
0391: try {
0392: paramValueString = paramValue.getNodeValue();
0393: }
0394: catch (Exception ex) {
0395: throw new KeywordValueException(
0396: "Error in parsing <param-value> tag.");
0397: }
0398: }
0399: }
0400: else{
0401: if (param.getNodeName().equals("description")){
0402: paramValue = param.getFirstChild();
0403: if (paramValue != null) {
0404: commentString = paramValue.getNodeValue();
0405: }
0406: else{
0407: deleteNode=param;
0408: commentString = "";
0409: }
0410: }
0411: }
0412: }
0413:
0414: param = param.getNextSibling();
0415: if (deleteNode != null){
0416: contextParam.removeChild((Node)(deleteNode));
0417: deleteNode = null;
0418: }
0419: } // while (param != null)
0420: if ((paramNameString == null) || (paramValueString == null)) {
0421: throw new KeywordValueException(
0422: "Error in parsing configuration parameters.");
0423: }
0424: if (commentString == null)
0425: commentString = "";
0426: paramNameString = paramNameString.trim();
0427:
0428: int len = paramNameString.length();
0429: if ((len>2) && (paramNameString.substring(len-2).equals("[]"))){
0430: paramNameString = paramNameString.substring(0, len-2);
0431: StringTokenizer tok = new StringTokenizer(paramValueString,
0432: new String(","));
0433: String[] stringArray = new String[tok.countTokens()];
0434: int i = 0;
0435: while (tok.hasMoreTokens()) {
0436: stringArray[i] = tok.nextToken().trim();
0437: i++;
0438: }
0439: addEntry(jndiAdapt.makeConfigString(paramNameString), stringArray, commentString);
0440: if (jndiAdapt != null) {
0441: String jndiName = JNDIAdapter.makeContextString(paramNameString);
0442: try {
0443: jndiAdapt.set(jndiName+"[]", paramValueString);
0444: }
0445: catch(Exception ex){}
0446: }
0447: }
0448: else {
0449: addEntry(jndiAdapt.makeConfigString(paramNameString), paramValueString, commentString);
0450: if (jndiAdapt != null) {
0451: String jndiName = JNDIAdapter.makeContextString(paramNameString);
0452: try {
0453: jndiAdapt.set(paramNameString, paramValueString);
0454: }
0455: catch(Exception ex){}
0456:
0457: }
0458: }
0459: } // if (contextParam.getNodeName().equals("context-param"))
0460: contextParam = contextParam.getNextSibling();
0461: } // while (contextParam != null)
0462: } // if
0463: */
0464:
0465: // tj 06.05.2004 needed for JNDI with <env-entry> tag
0466: /**
0467: * Parses the Document doc, got by transformation of the xml file.
0468: * All configuration parameters (names, values and comments) are put in
0469: * the Config object's HashTable.
0470: * @throws ParseException
0471: * @throws KeywordValueException
0472: */
0473:
0474: private void parseTree() throws ParseException,
0475: KeywordValueException {
0476: Node web = xmlDoc;
0477: Node envParam = null;
0478: Node param = null;
0479: Node paramValue = null;
0480: // Node deleteNode = null;
0481:
0482: if ((web != null) && (web.getNodeName().equals("web-app"))) {
0483: envParam = web.getFirstChild();
0484: while (envParam != null) {
0485: if (envParam.getNodeName().equals("env-entry")) {
0486: param = envParam.getFirstChild();
0487: String paramNameString = null;
0488: String paramValueString = null;
0489: String paramTypeString = null;
0490: String commentString = null;
0491:
0492: while (param != null) {
0493: if (param.getNodeName()
0494: .equals("env-entry-name")) {
0495: paramValue = param.getFirstChild();
0496: try {
0497: paramNameString = paramValue
0498: .getNodeValue();
0499: } catch (Exception ex) {
0500: throw new KeywordValueException(
0501: "Error in parsing <env-entry-name> tag.");
0502: }
0503: } else {
0504: if (param.getNodeName().equals(
0505: "env-entry-value")) {
0506: paramValue = param.getFirstChild();
0507: if (paramValue == null) {
0508: paramValueString = "";
0509: param
0510: .appendChild(doc
0511: .createTextNode(paramValueString));
0512: paramValue = param.getFirstChild();
0513: }
0514:
0515: else {
0516: try {
0517: paramValueString = paramValue
0518: .getNodeValue();
0519: } catch (Exception ex) {
0520: throw new KeywordValueException(
0521: "Error in parsing <env-entry-value> tag.");
0522: }
0523: }
0524: } else {
0525: if (param.getNodeName().equals(
0526: "env-entry-type")) {
0527: paramValue = param.getFirstChild();
0528: if (paramValue != null) {
0529: paramTypeString = paramValue
0530: .getNodeValue();
0531: }
0532: // else{
0533: // deleteNode=param;
0534: // paramTypeString = "";
0535: // }
0536: }
0537: }
0538: }
0539:
0540: param = param.getNextSibling();
0541: /*if (deleteNode != null){
0542: envParam.removeChild((Node)(deleteNode));
0543: deleteNode = null;
0544: }*/
0545: } // while (param != null)
0546: if ((paramNameString == null)
0547: || (paramValueString == null)
0548: || (paramTypeString == null)) {
0549: throw new KeywordValueException(
0550: "Error in parsing configuration parameters.");
0551: }
0552: // if (commentString == null)
0553: commentString = "";
0554: paramNameString = paramNameString.trim();
0555:
0556: int len = paramNameString.length();
0557: if ((len > 2)
0558: && (paramNameString.substring(len - 2)
0559: .equals("[]"))) {
0560: paramNameString = paramNameString.substring(0,
0561: len - 2);
0562: StringTokenizer tok = new StringTokenizer(
0563: paramValueString, ",");
0564: String[] stringArray = new String[tok
0565: .countTokens()];
0566: int i = 0;
0567: while (tok.hasMoreTokens()) {
0568: stringArray[i] = tok.nextToken().trim();
0569: i++;
0570: }
0571: addEntry(JNDIAdapter
0572: .makeConfigString(paramNameString),
0573: stringArray, commentString);
0574: if (jndiAdapt != null) {
0575: String jndiName = JNDIAdapter
0576: .makeContextString(paramNameString);
0577: try {
0578: jndiAdapt.set(jndiName + "[]",
0579: paramValueString);
0580: } catch (Exception ex) {
0581: }
0582: }
0583: } else {
0584: addEntry(JNDIAdapter
0585: .makeConfigString(paramNameString),
0586: paramValueString, commentString);
0587: if (jndiAdapt != null) {
0588: // String jndiName = JNDIAdapter.makeContextString(paramNameString);
0589: try {
0590: jndiAdapt.set(paramNameString,
0591: paramValueString);
0592: } catch (Exception ex) {
0593: }
0594:
0595: }
0596: }
0597: } // if (envParam.getNodeName().equals("env-entry"))
0598: envParam = envParam.getNextSibling();
0599: } // while (envParam != null)
0600: } // if
0601: }
0602:
0603: /**
0604: * Writes out a xml file to the OutputStream specified. Note that Objects
0605: * other than String or String[] will be converted into a String.
0606: * @param outputStream The output stream on which to write the config file.
0607: */
0608: public void write(OutputStream outputStream) {
0609: try {
0610: // tj 08.02.2004 needed for JNDI with <context-param> tag
0611: // this.updateTree();
0612:
0613: this .updateEnvEntryTagInTree();// tj 08.02.2004 JNDI with <env-entry> tag
0614:
0615: } catch (Exception ex) {
0616: System.out.println("Error in writting file.");
0617: //ex.printStackTrace();
0618: }
0619: try {
0620: FileOutputStream os = new FileOutputStream(file);
0621: OutputFormat of = new OutputFormat();
0622: of.setIndenting(true);
0623: of.setIndent(2);
0624: // of.setMethod(Method.XML);
0625: // of.setPreserveSpace(false);
0626: of.setLineWidth(79);
0627: if (!version.equals("")) {
0628: of.setVersion(version);
0629: }
0630: if (!encoding.equals("")) {
0631: of.setEncoding(encoding);
0632: }
0633: XMLSerializer out = new XMLSerializer(os, of);
0634: out.serialize(doc);
0635: os.close();
0636: } catch (Exception ex) {
0637: System.out.println("Error in writing file.");
0638: //ex.printStackTrace();
0639: }
0640: }
0641:
0642: // tj 08.02.2004 needed for JNDI with <context-param> tag
0643: /**
0644: * Updates the document doc with changes (added, apdated and removed entries).
0645: * @throws KeywordValueException
0646: */
0647: /*
0648: private void updateTree() throws KeywordValueException {
0649: Node web = xmlDoc;
0650: Node contextParam = null;
0651: Node placeToAdd = null;
0652: Node param = null;
0653: Node paramValue = null;
0654: Vector allParams = (Vector)order.clone();
0655: Node deleteNode = null;
0656:
0657: if ((web != null) && (web.getNodeName().equals("web-app"))) {
0658: contextParam = web.getFirstChild();
0659: while (contextParam != null) {
0660: if (contextParam.getNodeName().equals("context-param")){
0661: // placeToAdd = contextParam;
0662: param = contextParam.getFirstChild();
0663: String paramNameString = null;
0664: String commentString = null;
0665: Node paramValueNode = null;
0666: Node commentNode = null;
0667: Node descriptionNode = null;
0668: String arrayIndicator = "";
0669: int paramLength =0;
0670: while (param != null) {
0671: if (param.getNodeName().equals("param-name")){
0672: paramValue = param.getFirstChild();
0673: paramNameString = paramValue.getNodeValue();
0674: paramLength = paramNameString.length();
0675: if ((paramLength>2)&&(paramNameString.substring(paramLength-2).equals("[]"))){
0676: paramNameString = paramNameString.substring(0, paramLength-2);
0677: arrayIndicator = "[]";
0678: }
0679: }
0680: else {
0681: if (param.getNodeName().equals("param-value")){
0682: paramValueNode = param;
0683: paramValue = param.getFirstChild();
0684: paramValueNode = paramValue;
0685: }
0686: else{
0687: if (param.getNodeName().equals("description")){
0688: // commentNode = param;
0689: descriptionNode = param;
0690: paramValue = param.getFirstChild();
0691: commentNode = paramValue;
0692: if (paramValue != null){
0693: commentString = paramValue.getNodeValue();
0694: }
0695: else {
0696: commentString = "";
0697: }
0698: }
0699: }
0700: }
0701: param = param.getNextSibling();
0702: } // while (param != null)
0703: if (allParams.contains(paramNameString)){
0704: boolean doComment = true;
0705: Object newValue;
0706: String newValueString;
0707: String newComment;
0708: newValue = config.get(paramNameString);
0709: newComment = (String)comments.get(paramNameString);
0710: if (arrayIndicator.equals("")) { // node isn't array
0711: if (!newValue.getClass().isArray()){ // conf param is not array
0712: newValueString = newValue.toString();
0713: paramValueNode.setNodeValue(newValueString);
0714: // paramValueNode.setNodeValue("hello");
0715: allParams.remove(paramNameString);
0716: }
0717: else { // node isn't array but the config param is
0718: deleteNode = contextParam;
0719: doComment = false;
0720: }
0721: }
0722: else { // node is array
0723: if (!newValue.getClass().isArray()){ // conf param is not array
0724: deleteNode = contextParam;
0725: doComment = false;
0726: }
0727: else { // node is array and the config param is also
0728: String[] s = (String[]) newValue;
0729: int len = s.length;
0730: if (len > 0) {
0731: newValueString = s[0].trim();
0732: if (s.length > 1){
0733: for (int i = 1; i < s.length; i++) {
0734: newValueString = newValueString.concat(", ");
0735: newValueString = newValueString.concat(s[i].trim());
0736: }
0737: }
0738: }
0739: else
0740: newValueString = new String("");
0741: paramValueNode.setNodeValue(newValueString);
0742: allParams.remove(paramNameString);
0743: }
0744: }
0745: // paramValueNode.setNodeValue(newValue);
0746: if (doComment) {
0747: if (commentNode != null) {
0748: if (!newComment.equals(""))
0749: commentNode.setNodeValue(newComment);
0750: else
0751: contextParam.removeChild(descriptionNode);
0752: }
0753: else {
0754: if ( (newComment != null) && (!newComment.equals(""))) {
0755: if (descriptionNode != null) {
0756: descriptionNode.appendChild(doc.createTextNode( (String)
0757: newComment));
0758: }
0759: else {
0760: descriptionNode = doc.createElement("description");
0761: descriptionNode.appendChild(doc.createTextNode( ( (String)
0762: newComment)));
0763: contextParam.appendChild(descriptionNode);
0764: }
0765: }
0766: }
0767: }
0768: // allParams.remove(paramNameString);
0769: }
0770: else {
0771: deleteNode = contextParam;
0772: }
0773:
0774:
0775: } // if (contextParam.getNodeName().equals("context-param"))
0776: contextParam = contextParam.getNextSibling();
0777: if (deleteNode != null){
0778: web.removeChild((Node)deleteNode);
0779: deleteNode = null;
0780: }
0781: } // while (contextParam != null)
0782: } // if
0783: // if (placeToAdd != null) {
0784: // System.out.println("placeToAdd != null");
0785: // placeToAdd = placeToAdd.getNextSibling();
0786: // }
0787: // else {
0788: // System.out.println("placeToAdd == null");
0789: placeToAdd = findPlaceInTree();
0790: // }
0791: Node newNode;
0792: for (int i=0; i<allParams.size(); i++) {
0793: newNode = createNewContextParam((String)allParams.elementAt(i));
0794: // placeToAdd = web.insertBefore(newNode, placeToAdd);
0795: web.insertBefore(newNode, placeToAdd);
0796: }
0797: } // end of updateTree() method
0798: */
0799:
0800: /**
0801: * Updates the document doc with changes (added, apdated and removed entries).
0802: * @throws KeywordValueException
0803: */
0804: private void updateEnvEntryTagInTree() throws KeywordValueException {
0805: // tj 08.02.2004 for JNDI with <env-entry> tag
0806: Node web = xmlDoc;
0807: Node envEntryParam = null;
0808: Node placeToAdd = null;
0809: Node param = null;
0810: Node paramValue = null;
0811: Vector allParams = (Vector) order.clone();
0812: Node deleteNode = null;
0813: if ((web != null) && (web.getNodeName().equals("web-app"))) {
0814: envEntryParam = web.getFirstChild();
0815: while (envEntryParam != null) {
0816: if (envEntryParam.getNodeName().equals("env-entry")) {
0817: // placeToAdd = envEntryParam;
0818: param = envEntryParam.getFirstChild();
0819: String paramNameString = null;
0820: String paramType = null;
0821: String commentString = null;
0822: Node paramValueNode = null;
0823: Node paramNodeForNullValue = null;
0824: Node commentNode = null;
0825: Node descriptionNode = null;
0826: String arrayIndicator = "";
0827: int paramLength = 0;
0828: while (param != null) {
0829: if (param.getNodeName()
0830: .equals("env-entry-name")) {
0831: paramValue = param.getFirstChild();
0832: paramNameString = paramValue.getNodeValue();
0833: paramLength = paramNameString.length();
0834: if ((paramLength > 2)
0835: && (paramNameString
0836: .substring(paramLength - 2)
0837: .equals("[]"))) {
0838: paramNameString = paramNameString
0839: .substring(0, paramLength - 2);
0840: arrayIndicator = "[]";
0841: }
0842: } else {
0843: if (param.getNodeName().equals(
0844: "env-entry-value")) {
0845: paramNodeForNullValue = param;
0846: paramValue = param.getFirstChild();
0847: paramValueNode = paramValue;
0848: } else {
0849: if (param.getNodeName().equals(
0850: "env-entry-type")) {
0851: paramValue = param.getFirstChild();
0852: paramType = paramValue
0853: .getNodeValue();
0854: } else {
0855: if (param.getNodeName().equals(
0856: "description")) {
0857: // commentNode = param;
0858: descriptionNode = param;
0859: paramValue = param
0860: .getFirstChild();
0861: commentNode = paramValue;
0862: if (paramValue != null) {
0863: commentString = paramValue
0864: .getNodeValue();
0865: } else {
0866: commentString = "";
0867: }
0868: }
0869: }
0870: }
0871: }
0872: param = param.getNextSibling();
0873: } // while (param != null)
0874: if (allParams.contains(JNDIAdapter
0875: .makeConfigString(paramNameString))) {
0876: boolean doComment = true;
0877: Object newValue;
0878: String newValueString;
0879: String newComment;
0880: newValue = config.get(JNDIAdapter
0881: .makeConfigString(paramNameString));
0882: newComment = (String) comments.get(JNDIAdapter
0883: .makeConfigString(paramNameString));
0884: if (arrayIndicator.equals("")) { // node isn't array
0885: if ((!newValue.getClass().isArray())
0886: && (paramType.trim()
0887: .equals("java.lang.String"))) {
0888: // env-entry param is not array
0889: //Enumeration e = jndiParameterNames.keys();
0890: //while (e.hasMoreElements()) {
0891: //System.out.println("elem: " + (String)e.nextElement());
0892: //}
0893: if (jndiParameterNames
0894: .containsKey(JNDIAdapter
0895: .makeConfigString(paramNameString))) {
0896: newValueString = "jndi:"
0897: + (String) jndiParameterNames
0898: .get(JNDIAdapter
0899: .makeConfigString(paramNameString));
0900: // jndiParameterNames.remove(JNDIAdapter.makeConfigString(paramNameString));
0901: } else {
0902: newValueString = newValue
0903: .toString();
0904: }
0905:
0906: if (paramValueNode == null) {
0907: paramNodeForNullValue
0908: .appendChild(doc
0909: .createTextNode(newValueString));
0910: } else
0911: paramValueNode
0912: .setNodeValue(newValueString);
0913: allParams
0914: .remove(jndiAdapt
0915: .makeConfigString(paramNameString));
0916: } else { // node isn't array but the config param is
0917: deleteNode = envEntryParam;
0918: doComment = false;
0919: }
0920: } else { // node is array
0921: if (!newValue.getClass().isArray()) { // env-entry param is not array
0922: deleteNode = envEntryParam;
0923: doComment = false;
0924: } else {
0925: if (paramType.trim().equals(
0926: "java.lang.String")) { // env-entry is array and the config param is also
0927: String[] s = (String[]) newValue;
0928: int len = s.length;
0929: if (len > 0) {
0930: newValueString = s[0].trim();
0931: if (s.length > 1) {
0932: for (int i = 1; i < s.length; i++) {
0933: newValueString = newValueString
0934: .concat(", ");
0935: newValueString = newValueString
0936: .concat(s[i]
0937: .trim());
0938: }
0939: }
0940: } else
0941: newValueString = "";
0942: paramValueNode
0943: .setNodeValue(newValueString);
0944: allParams
0945: .remove(JNDIAdapter
0946: .makeConfigString(paramNameString));
0947: } else {
0948: deleteNode = envEntryParam;
0949: doComment = false;
0950: }
0951: }
0952: }
0953: // paramValueNode.setNodeValue(newValue);
0954: if (doComment) {
0955: if (commentNode != null) {
0956: if (!newComment.equals(""))
0957: commentNode
0958: .setNodeValue(newComment);
0959: else
0960: envEntryParam
0961: .removeChild(descriptionNode);
0962: } else {
0963: if ((newComment != null)
0964: && (!newComment.equals(""))) {
0965: if (descriptionNode != null) {
0966: descriptionNode
0967: .appendChild(doc
0968: .createTextNode((String) newComment));
0969: } else {
0970: descriptionNode = doc
0971: .createElement("description");
0972: descriptionNode
0973: .appendChild(doc
0974: .createTextNode(((String) newComment)));
0975: envEntryParam
0976: .appendChild(descriptionNode);
0977: }
0978: }
0979: }
0980: }
0981: // allParams.remove(paramNameString);
0982: } else {
0983: deleteNode = envEntryParam;
0984: }
0985:
0986: } // if (envEntryParam.getNodeName().equals("context-param"))
0987: envEntryParam = envEntryParam.getNextSibling();
0988: if (deleteNode != null) {
0989: web.removeChild((Node) deleteNode);
0990: deleteNode = null;
0991: }
0992: } // while (envEntryParam != null)
0993: } // if
0994:
0995: // tj 08.02.2004 needed for JNDI with <context-param> tag
0996: // placeToAdd = findPlaceInTree();
0997: // tj 08.02.2004 needed for JNDI with <env-entry> tag
0998: // placeToAdd = findEnvEntryPlaceInTree();
0999: // placeToAdd = findPlaceInTree();
1000: Node newNode;
1001: for (int i = 0; i < allParams.size(); i++) {
1002: // tj 08.02.2004 needed for JNDI with <context-param> tag
1003: // newNode = createNewContextParam((String)allParams.elementAt(i));
1004: // tj 08.02.2004 needed for JNDI with <env-entry> tag
1005: newNode = createNewEnvEntry((String) allParams.elementAt(i));// config form of key
1006:
1007: // placeToAdd = web.insertBefore(newNode, placeToAdd);
1008: if (newNode != null)
1009: web.insertBefore(newNode, placeToAdd);
1010: }
1011: } // end of updateEnvEntryTagInTree() method
1012:
1013: // tj 08.02.2004 needed for JNDI with <context-param> tag
1014: /**
1015: * Creates new <context-param> node with all necessary sub-nodes for new entry.
1016: * @param key Name of the parameter for which is new node created.
1017: * @return Created <context-param> node with its sub-nodes.
1018: * @throws KeywordValueException
1019: */
1020: /*
1021: private Node createNewContextParam (String key) throws KeywordValueException {
1022: Node newNodeContext = null;
1023: Node newNode = null;
1024: Object value = null;
1025: String valStr = null;
1026: String[] strings = null;
1027: try {
1028: newNodeContext = doc.createElement("context-param");
1029:
1030: newNode = doc.createElement("param-name");
1031: value = config.get(key);
1032: if (value.getClass().isArray())
1033: valStr = key.concat("[]");
1034: else
1035: valStr = key;
1036: newNode.appendChild(doc.createTextNode(valStr));
1037: newNodeContext.appendChild(newNode);
1038: valStr = null;
1039: newNode = doc.createElement("param-value");
1040: if (value.getClass().isArray()) {
1041: strings = (String[])value;
1042: valStr = strings[0];
1043: int i = 1;
1044: while (i < strings.length){
1045: valStr = valStr.concat(", ");
1046: valStr = valStr.concat(strings[i]);
1047: i++;
1048: }
1049: newNode.appendChild(doc.createTextNode(valStr));
1050: }
1051: else {
1052: newNode.appendChild(doc.createTextNode(value.toString()));
1053: }
1054: newNodeContext.appendChild(newNode);
1055: if (!comments.get(key).equals("")) {
1056: newNode = doc.createElement("description");
1057: newNode.appendChild(doc.createTextNode((String)comments.get(key)));
1058: newNodeContext.appendChild(newNode);
1059: }
1060: }
1061: catch (Exception ex){
1062: System.out.println("error in createNewContextParam method");
1063: }
1064: return newNodeContext;
1065: }
1066: */
1067:
1068: /**
1069: * Creates new <env-entry> node with all necessary sub-nodes for new entry.
1070: * @param key Name of the parameter for which is new node created.
1071: * @return Created <env-entry> node with its sub-nodes.
1072: * @throws KeywordValueException
1073: */
1074: private Node createNewEnvEntry(String key)
1075: throws KeywordValueException {
1076: // tj 08.02.2004 needed for JNDI with <env-entry> tag
1077: String contextKeyForm = JNDIAdapter.makeContextString(key);
1078: Node newEnvEntry = null;
1079: Node newNode = null;
1080: Object value = null;
1081: String valStr = null;
1082: String nameStr = null;
1083: String[] strings = null;
1084: try {
1085: value = config.get(key);
1086: if (value != null) {
1087: if (value.getClass().isArray()) {
1088: nameStr = contextKeyForm.concat("[]");
1089: } else {
1090: nameStr = contextKeyForm;
1091: }
1092:
1093: valStr = null;
1094: if (value.getClass().isArray()) {
1095: strings = (String[]) value;
1096: valStr = strings[0];
1097: int i = 1;
1098: while (i < strings.length) {
1099: valStr = valStr.concat(", ");
1100: valStr = valStr.concat(strings[i]);
1101: i++;
1102: }
1103: } else {
1104: valStr = value.toString();
1105: }
1106: }
1107: if (nameStr != null && valStr != null) {
1108:
1109: newEnvEntry = doc.createElement("env-entry");
1110: if (!comments.get(key).equals("")) {
1111: newNode = doc.createElement("description");
1112: newNode
1113: .appendChild(doc
1114: .createTextNode((String) comments
1115: .get(key)));
1116: newEnvEntry.appendChild(newNode);
1117: }
1118:
1119: newNode = doc.createElement("env-entry-name");
1120: newNode.appendChild(doc.createTextNode(nameStr));
1121: newEnvEntry.appendChild(newNode);
1122:
1123: newNode = doc.createElement("env-entry-value");
1124: newNode.appendChild(doc.createTextNode(valStr));
1125: newEnvEntry.appendChild(newNode);
1126:
1127: newNode = doc.createElement("env-entry-type");
1128: newNode.appendChild(doc
1129: .createTextNode("java.lang.String"));
1130: newEnvEntry.appendChild(newNode);
1131: }
1132: } catch (Exception ex) {
1133: System.out.println("error in createNewEnvEntry method");
1134: }
1135: return newEnvEntry;
1136: }
1137:
1138: // tj 08.02.2004 needed for JNDI with <context-param> tag
1139: /**
1140: * Finds the node before which will be <context-param> nodes added. This is
1141: * used only if there had not been any after parsing the document doc.
1142: * @return Node before which will new parameters added or null, if the new
1143: * parameters will be added at the end.
1144: * @throws KeywordValueException
1145: */
1146: /*
1147: private Node findPlaceInTree () throws KeywordValueException {
1148: Node web = xmlDoc;
1149: Node child = null;
1150:
1151: if ((web != null) && (web.getNodeName().equals("web-app"))) {
1152: child = web.getFirstChild();
1153: while ( (child != null) && ! (child.getNodeName().equals("filter") ||
1154: child.getNodeName().equals("filter-mapping") ||
1155: child.getNodeName().equals("listener") ||
1156: child.getNodeName().equals("servlet") ||
1157: child.getNodeName().equals("servlet-mapping") ||
1158: child.getNodeName().equals("session-config") ||
1159: child.getNodeName().equals("mime-mapping") ||
1160: child.getNodeName().equals("welcome-file-list") ||
1161: child.getNodeName().equals("error-page") ||
1162: child.getNodeName().equals("taglib") ||
1163: child.getNodeName().equals("resourceenv-ref") ||
1164: child.getNodeName().equals("resource-ref") ||
1165: child.getNodeName().equals("security-constraint") ||
1166: child.getNodeName().equals("login-config") ||
1167: child.getNodeName().equals("security-role") ||
1168: child.getNodeName().equals("env-entry") ||
1169: child.getNodeName().equals("ejb-ref") ||
1170: child.getNodeName().equals("ejb-local-ref"))) {
1171: child = child.getNextSibling();
1172: } // while
1173: if (child != null) {
1174: Node prev = child.getPreviousSibling();
1175: while ( (prev != null) && (prev.getNodeType() != Node.ELEMENT_NODE)) {
1176: child = prev;
1177: prev = prev.getPreviousSibling();
1178: }
1179: }
1180: }
1181: return child;
1182: }
1183: */
1184:
1185: /**
1186: * Finds the node before which will be <env-entry> nodes added.
1187: * @return Node before which will new parameters added or null, if the new
1188: * parameters will be added at the end.
1189: * @throws KeywordValueException
1190: */
1191:
1192: private Node findPlaceInTree() throws KeywordValueException {
1193: // tj 08.02.2004 needed for JNDI with <env-entry> tag
1194: Node web = xmlDoc;
1195: Node child = null;
1196:
1197: if ((web != null) && (web.getNodeName().equals("web-app"))) {
1198: child = web.getFirstChild();
1199: while ((child != null)
1200: && !(child.getNodeName().equals("ejb-ref") || child
1201: .getNodeName().equals("ejb-local-ref"))) {
1202: child = child.getNextSibling();
1203: } // while
1204: if (child != null) {
1205: Node prev = child.getPreviousSibling();
1206: while ((prev != null)
1207: && (prev.getNodeType() != Node.ELEMENT_NODE)) {
1208: child = prev;
1209: prev = prev.getPreviousSibling();
1210: }
1211: }
1212: }
1213: return child;
1214: }
1215:
1216: public static class PublicIdResolver implements EntityResolver {
1217: public PublicIdResolver() {
1218: super ();
1219: }
1220:
1221: public InputSource resolveEntity(String publicId,
1222: String systemId) {
1223: String Resource23 = "/org/enhydra/util/dtd/webXml2_3.dtd";
1224: String Resource24 = "/org/enhydra/util/dtd/webXml2_4.dtd";
1225: try {
1226: if (publicId != null) {
1227: // return a special input source
1228: if (publicId
1229: .equals("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN")) {
1230: return new InputSource(this .getClass()
1231: .getResource(Resource23).toString());
1232: } else {
1233: if (publicId
1234: .equals("-//Sun Microsystems, Inc.//DTD Web Application 2.4//EN")) {
1235: return new InputSource(this .getClass()
1236: .getResource(Resource24).toString());
1237: } else {
1238: return new InputSource(this .getClass()
1239: .getResource(Resource23).toString());
1240: }
1241:
1242: }
1243: }
1244: } catch (Exception e) {
1245: System.out.println("Resolver Entity Error.");
1246: //e.printStackTrace();
1247: }
1248: // use the default behaviour
1249: return null;
1250: }
1251: }
1252:
1253: }
|