MATSIM
XmlUtils.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * XmlUtils.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2019 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
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; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 
22  package org.matsim.core.utils.io;
23 
27 public final class XmlUtils {
28 
29  private XmlUtils() {
30  // static helper class
31  }
32 
41  public static String encodeAttributeValue(final String attributeValue) {
42  if (attributeValue == null) {
43  return null;
44  }
45  int len = attributeValue.length();
46  boolean encode = false;
47  for (int pos = 0; pos < len; pos++) {
48  char ch = attributeValue.charAt(pos);
49  if (ch == '<') {
50  encode = true;
51  break;
52  } else if (ch == '>') {
53  encode = true;
54  break;
55  } else if (ch == '\"') {
56  encode = true;
57  break;
58  } else if (ch == '&') {
59  encode = true;
60  break;
61  }
62  }
63  if (encode) {
64  StringBuilder bf = new StringBuilder(attributeValue.length() + 30);
65  for (int pos = 0; pos < len; pos++) {
66  char ch = attributeValue.charAt(pos);
67  if (ch == '<') {
68  bf.append("&lt;");
69  } else if (ch == '>') {
70  bf.append("&gt;");
71  } else if (ch == '\"') {
72  bf.append("&quot;");
73  } else if (ch == '&') {
74  bf.append("&amp;");
75  } else {
76  bf.append(ch);
77  }
78  }
79 
80  return bf.toString();
81  }
82  return attributeValue;
83  }
84 
89  public static StringBuilder writeEncodedAttributeValue(StringBuilder out, String attributeValue) {
90 
91  if (attributeValue == null) {
92  // By convention, null values are written as "null" in the xml output.
93  out.append("null");
94  return out;
95  }
96 
97  int len = attributeValue.length();
98 
99  for (int pos = 0; pos < len; pos++) {
100  char ch = attributeValue.charAt(pos);
101  switch (ch) {
102  case '<' -> out.append("&lt;");
103  case '>' -> out.append("&gt;");
104  case '\"' -> out.append("&quot;");
105  case '&' -> out.append("&amp;");
106  default -> out.append(ch);
107  };
108  }
109 
110  return out;
111  }
112 
117  public static StringBuilder writeEncodedAttributeKeyValue(StringBuilder out, String key, String value) {
118  out.append(key).append("=\"");
119  writeEncodedAttributeValue(out, value);
120  out.append("\" ");
121  return out;
122  }
123 
124  public static String encodeContent(final String content) {
125  if (content.contains("&") || content.contains("<") || content.contains(">")) {
126  return content.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
127  }
128  return content;
129  }
130 
131 }
static String encodeAttributeValue(final String attributeValue)
Definition: XmlUtils.java:41
static String encodeContent(final String content)
Definition: XmlUtils.java:124
static StringBuilder writeEncodedAttributeValue(StringBuilder out, String attributeValue)
Definition: XmlUtils.java:89
static StringBuilder writeEncodedAttributeKeyValue(StringBuilder out, String key, String value)
Definition: XmlUtils.java:117