View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.utils.xml;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  
24  /**
25   * Collection of XML encoding/decoding helpers. <br>
26   * This is all about the special characters &amp; and &lt;, and for attributes
27   * &quot; and &apos;. These must be encoded/decoded from/to XML.
28   */
29  final class XMLEncode {
30  
31      private static final int CDATA_BLOCK_THRESHOLD_LENGTH = 12;
32  
33      private static final char DEFAULT_QUOTE_CHAR = '"';
34  
35      static void xmlEncodeText(String text, Writer writer) throws IOException {
36          if (text == null) {
37              return;
38          }
39  
40          if (!needsEncoding(text)) {
41              writer.write(text);
42              return;
43          } else {
44              // only encode as cdata if is is longer than CDATA block overhead:
45              if (text.length() > CDATA_BLOCK_THRESHOLD_LENGTH) {
46                  String cdata = xmlEncodeTextAsCDATABlock(text);
47                  if (cdata != null) {
48                      writer.write(cdata);
49                      return;
50                  }
51              }
52          }
53  
54          // if every thing else fails, do it the save way...
55          xmlEncodeTextAsPCDATA(text, false, DEFAULT_QUOTE_CHAR, writer);
56      }
57  
58      static void xmlEncodeTextAsPCDATA(String text, boolean forAttribute, char quoteChar, Writer n) throws IOException {
59          if (text == null) {
60              return;
61          }
62  
63          int length = text.length();
64          if (forAttribute) {
65              n.append(quoteChar);
66          }
67  
68          for (int i = 0; i < length; i++) {
69              char c = text.charAt(i);
70              switch (c) {
71                  case '&':
72                      n.append("&amp;");
73                      break;
74                  case '<':
75                      n.append("&lt;");
76                      break;
77                  case '>': // FIX for sourceforge bug #802520 ("]]>" needs encoding)
78                      n.append("&gt;");
79                      break;
80                  case '"':
81                      if (forAttribute) {
82                          n.append("&quot;");
83                      } else {
84                          n.append(c);
85                      }
86                      break;
87                  case '\'':
88                      if (forAttribute) {
89                          n.append("&apos;");
90                      } else {
91                          n.append(c);
92                      }
93                      break;
94                  case '\r':
95                      if (forAttribute) {
96                          if (i == (length - 1) || text.charAt(i + 1) != '\n') {
97                              n.append("&#13;");
98                          }
99                      } else {
100                         n.append(c);
101                     }
102                     // but skip the \r in \r\n
103 
104                     break;
105                 case '\n':
106                     if (forAttribute) {
107                         n.append("&#10;");
108                     }
109                     break;
110 
111                 default:
112                     n.append(c);
113                     break;
114             }
115         }
116 
117         if (forAttribute) {
118             n.append(quoteChar);
119         }
120     }
121 
122     /**
123      * Returns string as CDATA block if possible, otherwise null.
124      */
125     private static String xmlEncodeTextAsCDATABlock(String text) {
126         if (text == null) {
127             return null;
128         }
129         if (!text.contains("]]>")) {
130             return "<![CDATA[" + text + "]]>";
131         } else {
132             return null;
133         }
134     }
135 
136     /**
137      * Checks if this text needs encoding in order to be represented in XML.
138      */
139     private static boolean needsEncoding(String text) {
140         if (text == null) {
141             return false;
142         }
143         for (int i = 0; i < text.length(); i++) {
144             char c = text.charAt(i);
145             if (c == '&' || c == '<') {
146                 return true;
147             }
148         }
149         return false;
150     }
151 }