comparison java/src/java/nonstandard/XmlTranscode.d @ 21:9b96950f2c3c

the 'java' tree compiles on both D1-Tango and D2-Phobos
author Frank Benoit <benoit@tionex.de>
date Thu, 19 Mar 2009 20:38:55 +0100
parents 6bf2837c50fe
children
comparison
equal deleted inserted replaced
20:dccb717aa902 21:9b96950f2c3c
1 module java.nonstandard.XmlTranscode; 1 module java.nonstandard.XmlTranscode;
2 2
3 import java.lang.util; 3 import java.lang.util;
4 import java.lang.exceptions;
4 import java.lang.Math; 5 import java.lang.Math;
5 import tango.core.Exception;
6 6
7 /++ 7 /++
8 + Decode XML entities into UTF8 string. 8 + Decode XML entities into UTF8 string.
9 + Eg. "&amp;" -> "&", "&#38;" -> "&", "&#x26;" -> "&" 9 + Eg. "&amp;" -> "&", "&#38;" -> "&", "&#x26;" -> "&"
10 + Throws TextException on failure 10 + Throws TextException on failure
11 + The given string is modified. 11 + The given string is modified.
12 +/ 12 +/
13 String xmlUnescape( String str ){ 13 String xmlUnescape( String str ){
14 14
15 void error(){ 15 void error(){
16 throw new TextException( "xmlUnescape" ); 16 throw new RuntimeException( "xmlUnescape" );
17 } 17 }
18 // &lt; ... 18 // &lt; ...
19 // &#1234; 19 // &#1234;
20 // &#x12AF; 20 // &#x12AF;
21 String src = str; 21 String src = str;
22 String trg = str; 22 String trg = str;
23 while( src.length ){ 23 while( src.length ){
24 if( src[0] !is '&' ){ 24 if( src[0] !is '&' ){
25 trg[0] = src[0]; 25 //trg[0] = src[0];
26 trg = trg[1..$]; 26 trg = trg[1..$];
27 src = src[1..$]; 27 src = src[1..$];
28 } 28 }
29 else{ 29 else{
30 src = src[1..$]; // go past '&' 30 src = src[1..$]; // go past '&'
31 if( src.length < 2 ) error(); 31 if( src.length < 2 ) error();
32 32
33 // search semi 33 // search semi
34 int len = Math.min( cast(int)src.length, 10 ); // limit semi search to possible longest entityname 34 int len = Math.min( cast(int)src.length, 10 ); // limit semi search to possible longest entityname
35 int semi = tango.text.Util.locate( src[0 .. len ], ';' ); 35 int semi = java.lang.util.indexOf( src[0 .. len ], ';' );
36 if( semi is len ) error(); // no semi found 36 if( semi is -1 ) error(); // no semi found
37 37
38 String entityName = src[ 0 .. semi ]; // name without semi 38 String entityName = src[ 0 .. semi ]; // name without semi
39 dchar entityValue = 0; 39 dchar entityValue = 0;
40 switch( entityName ){ 40 switch( entityName ){
41 case "lt": entityValue = '<'; break; 41 case "lt": entityValue = '<'; break;
75 error(); 75 error();
76 } 76 }
77 } 77 }
78 } 78 }
79 } 79 }
80 dchar[1] arr; 80 String res = String_valueOf( entityValue );
81 arr[0] = entityValue;
82 uint ate = 0;
83 String res = tango.text.convert.Utf.toString( arr, trg, &ate );
84 trg = trg[ res.length .. $ ]; 81 trg = trg[ res.length .. $ ];
85 src = src[ semi +1 .. $ ]; // go past semi 82 src = src[ semi +1 .. $ ]; // go past semi
86 } 83 }
87 } 84 }
88 return str[ 0 .. trg.ptr-str.ptr ]; 85 return str[ 0 .. trg.ptr-str.ptr ];
120 } 117 }
121 return xml; 118 return xml;
122 Lprocess: 119 Lprocess:
123 120
124 // yes, do a new string, start with +20 chars 121 // yes, do a new string, start with +20 chars
125 String res = new char[ xml.length + 20 ]; 122 char[] res = new char[ xml.length + 20 ];
126 res.length = 0; 123 res.length = 0;
127 124
128 foreach( dchar c; xml ){ 125 foreach( dchar c; xml ){
129 126
130 if( !needsReplacement( c )){ 127 if( !needsReplacement( c )){