001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.util;
016:
017: import java.util.Collections;
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: /** Utilities used to ease some XML related programming tasks */
022: public class XMLUtils {
023: /** Converts normal string to string able to be used in XML comments. */
024: public static String stringToXmlComment(String pString) {
025: if (pString != null) {
026: pString = StringUtils.replace(pString, "©", "(C)");
027: }
028: return pString;
029: }
030:
031: /** Converts normal string to XML string by replacing characters restricted in XML
032: * by their codes. The characters it looks for are :
033: * <table>
034: * <tr><td>&lt;</td><td><</td><td>less than</td></tr>
035: * <tr><td>&gt;</td><td>></td><td>greater than</td></tr>
036: * <tr><td>&amp;</td><td>&</td><td>ampersand </td></tr>
037: * <tr><td>&apos;</td><td>'</td><td>apostrophe</td></tr>
038: * <tr><td>&quot;</td><td>"</td><td>quotation mark</td></tr>
039: * </table>
040: */
041: public static String stringToXml(String pString) {
042: if (pString != null) {
043: pString = StringUtils.replace(pString, "&", "&");
044: pString = StringUtils.replace(pString, "\"", """);
045: pString = StringUtils.replace(pString, "<", "<");
046: pString = StringUtils.replace(pString, ">", ">");
047: pString = StringUtils.replace(pString, "'", "'");
048: }
049: return pString;
050: }
051:
052: /** Converts XML string to normal string by replacing special character combinations
053: * XML by their ASCII counterparts. The characters it looks for are :
054: * <table>
055: * <tr><td>&lt;</td><td><</td><td>less than</td></tr>
056: * <tr><td>&gt;</td><td>></td><td>greater than</td></tr>
057: * <tr><td>&amp;</td><td>&</td><td>ampersand </td></tr>
058: * <tr><td>&apos;</td><td>'</td><td>apostrophe</td></tr>
059: * <tr><td>&quot;</td><td>"</td><td>quotation mark</td></tr>
060: * </table>
061: */
062: public static String xmlToString(String pString) {
063: if (pString != null) {
064: pString = StringUtils.replace(pString, "&", "&");
065: pString = StringUtils.replace(pString, """, "\"");
066: pString = StringUtils.replace(pString, "<", "<");
067: pString = StringUtils.replace(pString, ">", ">");
068: pString = StringUtils.replace(pString, "'", "'");
069: }
070: return pString;
071: }
072:
073: /** Removes start and end of the CDATA section from the string */
074: public static String unwrapCDATA(String pString) {
075: if (pString != null) {
076: pString = StringUtils.replace(pString, "<![CDATA[", "");
077: pString = StringUtils.replace(pString, "]]>", "");
078: }
079: return pString;
080: }
081:
082: /** Wraps supplied string in the CDATA section */
083: public static String wrapCDATA(String pString) {
084: if (pString != null) {
085: StringBuffer lStringBuffer = new StringBuffer("<![CDATA[");
086: lStringBuffer.append(pString);
087: lStringBuffer.append("]]>");
088: }
089: return pString;
090: }
091:
092: private static Map sNamespaces = Collections
093: .synchronizedMap(new HashMap());
094:
095: /** This utility method will generate the unique prefix for the given namespace.
096: * It guarantees that for any given namespace - the generated prefix will always be the unique and the
097: * same within single JVM run. The generator is thread safe */
098: public static String suggestNamespacePrefix(String pNamespaceName) {
099: String lPrefix = (String) sNamespaces.get(pNamespaceName);
100: if (lPrefix == null) {
101: synchronized (sNamespaces) {
102: lPrefix = (String) sNamespaces.get(pNamespaceName);
103: if (lPrefix == null) {
104: lPrefix = "nmsp" + sNamespaces.size();
105: sNamespaces.put(pNamespaceName, lPrefix);
106: }
107: }
108: }
109: return lPrefix;
110: }
111: }
|