File tree Expand file tree Collapse file tree 3 files changed +14
-73
lines changed
metafacture-biblio/src/main/java/org/metafacture/biblio/marc21
metafacture-commons/src/main/java/org/metafacture/commons
metafacture-xml/src/main/java/org/metafacture/xml Expand file tree Collapse file tree 3 files changed +14
-73
lines changed Original file line number Diff line number Diff line change 1717
1818import java .util .Collections ;
1919
20+ import org .metafacture .commons .XmlUtil ;
2021import org .metafacture .framework .FluxCommand ;
2122import org .metafacture .framework .MetafactureException ;
2223import org .metafacture .framework .ObjectReceiver ;
@@ -235,39 +236,7 @@ private void writeRaw(final String str) {
235236
236237 /** Writes a escaped sequence */
237238 private void writeEscaped (final String str ) {
238- final int len = str .length ();
239- for (int i = 0 ; i < len ; ++i ) {
240- final char c = str .charAt (i );
241- final String entityName ;
242- switch (c ) {
243- case '&' :
244- entityName = "amp" ;
245- break ;
246- case '<' :
247- entityName = "lt" ;
248- break ;
249- case '>' :
250- entityName = "gt" ;
251- break ;
252- case '\'' :
253- entityName = "apos" ;
254- break ;
255- case '"' :
256- entityName = "quot" ;
257- break ;
258- default :
259- entityName = null ;
260- break ;
261- }
262-
263- if (entityName == null ) {
264- builder .append (c );
265- } else {
266- builder .append ('&' );
267- builder .append (entityName );
268- builder .append (';' );
269- }
270- }
239+ builder .append (XmlUtil .escape (str , false ));
271240 }
272241
273242 private void prettyPrintIndentation () {
Original file line number Diff line number Diff line change @@ -95,24 +95,28 @@ public static boolean isXmlMimeType(final String mimeType) {
9595 mimeType .endsWith (XML_BASE_MIME_TYPE );
9696 }
9797
98- public static String escape (String unescaped ) {
98+ public static String escape (final String unescaped ) {
99+ return escape (unescaped , true );
100+ }
101+
102+ public static String escape (final String unescaped , final boolean escapeUnicode ) {
99103 return unescaped .codePoints ()
100- .mapToObj (XmlUtil :: escapeCodePoint )
104+ .mapToObj (value -> escapeCodePoint ( value , escapeUnicode ) )
101105 .collect (joining ());
102106 }
103-
104- private static String escapeCodePoint (int codePoint ) {
107+
108+ private static String escapeCodePoint (final int codePoint , final boolean escapeUnicode ) {
105109 final String entity = entityFor (codePoint );
106110 if (entity != null ) {
107111 return entity ;
108112 }
109- if (codePoint > 0x7f ) {
113+ if (escapeUnicode && codePoint > 0x7f ) {
110114 return "&#" + Integer .toString (codePoint ) + ";" ;
111115 }
112116 return Character .toString ((char ) codePoint );
113117 }
114118
115- private static String entityFor (int ch ) {
119+ private static String entityFor (final int ch ) {
116120 switch (ch ) {
117121 case '<' : return "<" ;
118122 case '>' : return ">" ;
Original file line number Diff line number Diff line change 2626import java .util .Properties ;
2727
2828import org .metafacture .commons .ResourceUtil ;
29+ import org .metafacture .commons .XmlUtil ;
2930import org .metafacture .framework .FluxCommand ;
3031import org .metafacture .framework .MetafactureException ;
3132import org .metafacture .framework .ObjectReceiver ;
@@ -244,40 +245,7 @@ private void writeFooter() {
244245 }
245246
246247 protected static void writeEscaped (final StringBuilder builder , final String str ) {
247-
248- final int len = str .length ();
249- for (int i = 0 ; i < len ; ++i ) {
250- final char c = str .charAt (i );
251- final String entityName ;
252- switch (c ) {
253- case '&' :
254- entityName = "amp" ;
255- break ;
256- case '<' :
257- entityName = "lt" ;
258- break ;
259- case '>' :
260- entityName = "gt" ;
261- break ;
262- case '\'' :
263- entityName = "apos" ;
264- break ;
265- case '"' :
266- entityName = "quot" ;
267- break ;
268- default :
269- entityName = null ;
270- break ;
271- }
272-
273- if (entityName == null ) {
274- builder .append (c );
275- } else {
276- builder .append ('&' );
277- builder .append (entityName );
278- builder .append (';' );
279- }
280- }
248+ builder .append (XmlUtil .escape (str , false ));
281249 }
282250
283251 /**
You can’t perform that action at this time.
0 commit comments