Mercurial Hosting > nabble
comparison src/fschmidt/html/HtmlScript.java @ 68:00520880ad02
add fschmidt source
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 05 Oct 2025 17:24:15 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:9d0fefce6985 | 68:00520880ad02 |
---|---|
1 /* | |
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com> | |
3 | |
4 Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 of this software and associated documentation files (the "Software"), to deal | |
6 in the Software without restriction, including without limitation the rights | |
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 copies of the Software, and to permit persons to whom the Software is | |
9 furnished to do so, subject to the following conditions: | |
10 | |
11 The above copyright notice and this permission notice shall be included in | |
12 all copies or substantial portions of the Software. | |
13 | |
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 THE SOFTWARE. | |
21 */ | |
22 | |
23 package fschmidt.html; | |
24 | |
25 import java.util.List; | |
26 import java.util.ArrayList; | |
27 | |
28 | |
29 public final class HtmlScript extends HtmlTextContainer { | |
30 | |
31 public HtmlScript(HtmlTag startTag,String text,HtmlTag endTag) { | |
32 super(startTag,text,endTag); | |
33 } | |
34 | |
35 | |
36 // odd strings are string constants | |
37 public static String[] split(String text) { | |
38 List<String> list = new ArrayList<String>(); | |
39 char[] a = text.toCharArray(); | |
40 int len = a.length; | |
41 StringBuilder buf = new StringBuilder(); | |
42 outer: | |
43 for( int i=0; i<len; i++ ) { | |
44 char c = a[i]; | |
45 switch(c) { | |
46 case '"': | |
47 case '\'': | |
48 StringBuilder bufS = new StringBuilder(); | |
49 bufS.append(c); | |
50 for( int j=i+1; j<len && j!='\n'; j++ ) { | |
51 char c2 = a[j]; | |
52 bufS.append(c2); | |
53 if( c2=='\\' ) { | |
54 bufS.append(a[++j]); | |
55 continue; | |
56 } | |
57 if( c2==c ) { | |
58 list.add(buf.toString()); | |
59 list.add(bufS.toString()); | |
60 i = j; | |
61 buf.setLength(0); | |
62 continue outer; | |
63 } | |
64 } | |
65 default: | |
66 buf.append(c); | |
67 } | |
68 } | |
69 list.add(buf.toString()); | |
70 return (String[])list.toArray(new String[0]); | |
71 } | |
72 | |
73 public static String unquote(String s) { | |
74 char c = s.charAt(0); | |
75 if( c!='"' && c!='\'' || c!=s.charAt(s.length()-1) ) | |
76 throw new RuntimeException(); | |
77 s = s.substring(1,s.length()-1); | |
78 StringBuilder buf = new StringBuilder(); | |
79 int i = 0; | |
80 while(true) { | |
81 int i2 = s.indexOf('\\',i); | |
82 if( i2 == -1 ) | |
83 break; | |
84 buf.append(s.substring(i,i2)); | |
85 c = s.charAt(i2+1); | |
86 switch(c) { | |
87 case 'b': | |
88 buf.append('\b'); | |
89 break; | |
90 case 'f': | |
91 buf.append('\f'); | |
92 break; | |
93 case 'n': | |
94 buf.append('\n'); | |
95 break; | |
96 case 'r': | |
97 buf.append('\r'); | |
98 break; | |
99 case 't': | |
100 buf.append('\t'); | |
101 break; | |
102 default: | |
103 buf.append(c); | |
104 } | |
105 i = i2 + 2; | |
106 } | |
107 buf.append(s.substring(i)); | |
108 return buf.toString(); | |
109 } | |
110 | |
111 public static String quote(String s,char quote) { | |
112 StringBuilder buf = new StringBuilder(); | |
113 char[] a = s.toCharArray(); | |
114 buf.append(quote); | |
115 for( int i=0; i<a.length; i++ ) { | |
116 char c = a[i]; | |
117 switch(c) { | |
118 case '\b': | |
119 buf.append("\\b"); | |
120 break; | |
121 case '\f': | |
122 buf.append("\\f"); | |
123 break; | |
124 case '\n': | |
125 buf.append("\\n"); | |
126 break; | |
127 case '\r': | |
128 buf.append("\\r"); | |
129 break; | |
130 case '\t': | |
131 buf.append("\\t"); | |
132 break; | |
133 case '\\': | |
134 case '"': | |
135 case '\'': | |
136 buf.append('\\'); | |
137 default: | |
138 buf.append(c); | |
139 } | |
140 } | |
141 buf.append(quote); | |
142 return buf.toString(); | |
143 } | |
144 } |