Source code: joelib/io/types/cml/XMLSpecialCharacter.java
1 ///////////////////////////////////////////////////////////////////////////////
2 //Filename: $RCSfile: XMLSpecialCharacter.java,v $
3 //Purpose: Chemical Markup Language.
4 //Language: Java
5 //Compiler: JDK 1.4
6 //Authors: Joerg K. Wegner
7 //Version: $Revision: 1.6 $
8 // $Date: 2003/08/19 13:11:26 $
9 // $Author: wegner $
10 //
11 //Copyright (C) 1997-2003 The Chemistry Development Kit (CDK) project
12 //Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
13 //
14 //This program is free software; you can redistribute it and/or modify
15 //it under the terms of the GNU General Public License as published by
16 //the Free Software Foundation version 2 of the License.
17 //
18 //This program is distributed in the hope that it will be useful,
19 //but WITHOUT ANY WARRANTY; without even the implied warranty of
20 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 //GNU General Public License for more details.
22 ///////////////////////////////////////////////////////////////////////////////
23 package joelib.io.types.cml;
24
25
26 /*==========================================================================*
27 * IMPORTS
28 *========================================================================== */
29 /*==========================================================================*
30 * CLASS DECLARATION
31 *========================================================================== */
32
33 /**
34 * Checks and converts XML special characters.
35 *
36 * @author wegnerj
37 * @license GPL
38 * @cvsversion $Revision: 1.6 $, $Date: 2003/08/19 13:11:26 $
39 * @cite rr99b
40 * @cite mr01
41 * @cite gmrw01
42 * @cite wil01
43 */
44 public class XMLSpecialCharacter
45 {
46 //~ Methods ////////////////////////////////////////////////////////////////
47
48 /*-------------------------------------------------------------------------*
49 * private static member variables
50 *------------------------------------------------------------------------- */
51 /*-------------------------------------------------------------------------*
52 * public methods
53 *------------------------------------------------------------------------- */
54
55 /**
56 * Converts plain strings to XML formatted string entries.
57 *
58 * Converts special characters to XML special characters for single line entries <b>or</b>
59 * changes multiple line entries to a XML core data element.
60 *
61 * @param the plain string
62 * @return the XML compatible string
63 */
64 public static String convertPlain2XML(String xmlEntry)
65 {
66 char amp = '&';
67 int q;
68 StringBuffer sb = new StringBuffer(xmlEntry);
69
70 // store multiple line string entries as core data elements
71 if (sb.toString().indexOf("\n") != -1)
72 {
73 sb.insert(0, "<![CDATA[");
74 sb.append("]]>");
75
76 return sb.toString();
77 }
78
79 //conversion of '&'
80 q = -1;
81
82 while ((q = sb.toString().indexOf("&", (q + 1))) != -1)
83 {
84 sb.insert((q + 1), "amp;");
85 }
86
87 //conversion of '<'
88 while ((q = sb.toString().indexOf("<")) != -1)
89 {
90 sb.setCharAt(q, amp);
91 sb.insert((q + 1), "lt;");
92 }
93
94 //conversion of '>'
95 while ((q = sb.toString().indexOf(">")) != -1)
96 {
97 sb.setCharAt(q, amp);
98 sb.insert((q + 1), "gt;");
99 }
100
101 return sb.toString();
102 }
103 }
104 ///////////////////////////////////////////////////////////////////////////////
105 // END OF FILE.
106 ///////////////////////////////////////////////////////////////////////////////