001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: AbsElement.java 4718 2004-05-10 12:06:09Z sauthieg $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib.deployment.xml;
026:
027: /**
028: * This class defines the implementation of the interface Element.
029: * These elements are used by Digester during the xml parsing.
030: * @author Florent Benoit
031: */
032: public abstract class AbsElement implements Element {
033:
034: /**
035: * Represents this element by it's XML description.
036: * @param indent use this indent for prexifing XML representation.
037: * @return the XML description of this object.
038: */
039: public abstract String toXML(int indent);
040:
041: /**
042: * Represents this element by it's XML description.
043: * Use a default indent set to 0.
044: * @return the XML description of this object.
045: */
046: public String toXML() {
047: return toXML(0);
048: }
049:
050: /**
051: * Return the representation of this element.
052: * Use the XML representation of the object for the toString() method.
053: * @return the XML description of this object.
054: */
055: public String toString() {
056: return toXML();
057: }
058:
059: /**
060: * Return indent spaces.
061: * @param indent number of indentation.
062: * @return the indent space string.
063: */
064: protected String indent(int indent) {
065: String txt = "";
066: for (int i = 0; i < indent; i++) {
067: txt += " ";
068: }
069: return txt;
070: }
071:
072: /**
073: * Return the xml representation of the specified value with the root-element xmlTag
074: * @param value String value to represent in XML
075: * @param xmlTag tag of the root-element
076: * @param indent indent to use
077: * @return xml representation of the specified value
078: */
079: protected String xmlElement(String value, String xmlTag, int indent) {
080: if (value == null) {
081: return "";
082: }
083:
084: // else
085:
086: StringBuffer sb = new StringBuffer();
087: sb.append(indent(indent));
088: sb.append("<");
089: sb.append(xmlTag);
090: sb.append(">");
091: sb.append(value);
092: sb.append("</");
093: sb.append(xmlTag);
094: sb.append(">\n");
095: return sb.toString();
096: }
097:
098: /**
099: * Return the xml representation of the specified attribute value
100: * @param value String value to represent in XML
101: * @param xmlTag tag of the attribute
102: * @return xml representation of the specified value
103: */
104: protected String xmlAttribute(String value, String xmlTag) {
105: if (value == null) {
106: return "";
107: }
108:
109: // else
110:
111: StringBuffer sb = new StringBuffer();
112: sb.append(" ");
113: sb.append(xmlTag);
114: sb.append("=\"");
115: sb.append(value);
116: sb.append("\"");
117: return sb.toString();
118: }
119:
120: }
|