|
| 1 | +package net.sf.j2s.core.adapters; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +public class ExtendedAdapter extends AbstractPluginAdapter { |
| 7 | + |
| 8 | + public static String buildXSource(String fullClassName, String tagName, String firstLine, String sources) { |
| 9 | + if (firstLine != null && sources.length() > 0) { |
| 10 | + Set<String> xTags = null; |
| 11 | + StringBuilder builder = new StringBuilder(); |
| 12 | + if ("@j2sXHTML".equals(tagName)) { |
| 13 | + boolean shouldMergeFirstLine = false; |
| 14 | + if (firstLine.startsWith("{$") || firstLine.contains("<") || firstLine.contains(">")) { |
| 15 | + shouldMergeFirstLine = true; |
| 16 | + } |
| 17 | + if (shouldMergeFirstLine) { |
| 18 | + sources = firstLine + "\r\n" + sources; |
| 19 | + firstLine = ""; |
| 20 | + } |
| 21 | + xTags = parseXTag(sources); |
| 22 | + sources = "\"" + sources.replaceAll("\t", "\\\\t").replaceAll("\"", "\\\\\"").replaceAll("\r\n", "\\\\r\\\\n\" +\r\n\"") + "\""; |
| 23 | + String[] parts = firstLine.split("(,| |\t)+"); |
| 24 | + if (firstLine.length() == 0) { |
| 25 | + builder.append("Clazz.parseHTML("); |
| 26 | + } else if (parts == null || parts.length == 1) { |
| 27 | + if ("return".equals(firstLine)) { |
| 28 | + builder.append("return Clazz.parseHTML("); |
| 29 | + } else { |
| 30 | + builder.append("Clazz.parseHTML(").append(firstLine).append(", "); |
| 31 | + } |
| 32 | + } else { |
| 33 | + String firstVar = parts[0]; |
| 34 | + String leftStr = firstLine.substring(firstVar.length() + 1).replaceAll("^(,| |\t)+", ""); |
| 35 | + if (leftStr.endsWith(",")) { |
| 36 | + leftStr = leftStr.substring(0, leftStr.length() - 1); |
| 37 | + } |
| 38 | + if ("return".equals(firstVar)) { |
| 39 | + builder.append("return Clazz.parseHTML(").append(leftStr).append(", "); |
| 40 | + } else { |
| 41 | + builder.append(firstVar).append(" = Clazz.parseHTML(").append(leftStr).append(", "); |
| 42 | + } |
| 43 | + } |
| 44 | + } else { // @j2sXCSS |
| 45 | + boolean shouldMergeFirstLine = false; |
| 46 | + if (firstLine.startsWith(".") || firstLine.contains("#") || firstLine.contains(">") || firstLine.contains("{")) { |
| 47 | + shouldMergeFirstLine = true; |
| 48 | + } else if (sources.startsWith("{")) { |
| 49 | + shouldMergeFirstLine = true; |
| 50 | + } |
| 51 | + if (shouldMergeFirstLine) { |
| 52 | + sources = firstLine + "\r\n" + sources; |
| 53 | + xTags = parseXTag(sources); |
| 54 | + builder.append("Clazz.parseCSS("); |
| 55 | + } else { |
| 56 | + xTags = parseXTag(sources); |
| 57 | + if (firstLine.endsWith(",")) { |
| 58 | + firstLine = firstLine.substring(0, firstLine.length() - 1); |
| 59 | + } |
| 60 | + builder.append("Clazz.parseCSS(").append(firstLine).append(", "); |
| 61 | + } |
| 62 | + sources = "\"" + sources.replaceAll("\t", "\\\\t").replaceAll("\"", "\\\\\"").replaceAll("\r\n", "\\\\r\\\\n\" +\r\n\"") + "\""; |
| 63 | + } |
| 64 | + boolean containsThis = containsBeginning(xTags, "$:") || containsBeginning(xTags, "$."); |
| 65 | + boolean containsClass = containsBeginning(xTags, "$/"); |
| 66 | + if (containsThis) { |
| 67 | + builder.append("this, "); |
| 68 | + } else if (containsClass) { |
| 69 | + builder.append(fullClassName).append(", "); |
| 70 | + } |
| 71 | + boolean localStarted = false; |
| 72 | + for (String s : xTags) { |
| 73 | + if (s.startsWith("$~")) { |
| 74 | + if (!localStarted) { |
| 75 | + builder.append("{"); |
| 76 | + localStarted = true; |
| 77 | + } else { |
| 78 | + builder.append(", "); |
| 79 | + } |
| 80 | + String varName = s.substring(2); |
| 81 | + builder.append(varName).append(": ").append(varName); |
| 82 | + } |
| 83 | + } |
| 84 | + if (localStarted) { |
| 85 | + builder.append("}, "); |
| 86 | + } |
| 87 | + builder.append(sources).append(");\r\n"); |
| 88 | + return builder.toString(); |
| 89 | + } |
| 90 | + return sources; |
| 91 | + } |
| 92 | + |
| 93 | + private static Set<String> parseXTag(String sources) { |
| 94 | + Set<String> vars = new HashSet<String>(); |
| 95 | + String key = "{$"; |
| 96 | + int index = sources.indexOf(key, 0); |
| 97 | + while (index != -1) { |
| 98 | + int idx = sources.indexOf("}", index + key.length()); |
| 99 | + if (idx == -1) { |
| 100 | + break; |
| 101 | + } |
| 102 | + String var = sources.substring(index + key.length() - 1, idx); // with prefix $ |
| 103 | + if (var.indexOf(' ') == -1) { |
| 104 | + vars.add(var); |
| 105 | + } |
| 106 | + index = sources.indexOf(key, idx + 1); |
| 107 | + } |
| 108 | + key = "<!--"; |
| 109 | + index = sources.indexOf(key, 0); |
| 110 | + while (index != -1) { |
| 111 | + int idx = sources.indexOf("-->", index + key.length()); |
| 112 | + if (idx == -1) { |
| 113 | + break; |
| 114 | + } |
| 115 | + String comment = sources.substring(index + key.length(), idx).trim(); |
| 116 | + if (comment.startsWith("$") && comment.indexOf(' ') == -1) { |
| 117 | + vars.add(comment); |
| 118 | + } |
| 119 | + index = sources.indexOf(key, idx + 3); // 3: "-->".length() |
| 120 | + } |
| 121 | + key = "id"; |
| 122 | + index = sources.indexOf(key, 0); |
| 123 | + while (index > 0) { |
| 124 | + char last = sources.charAt(index - 1); |
| 125 | + if (!(last == ' ' || last == '\t' || last == '\n' || last == '\r')) { |
| 126 | + index = sources.indexOf(key, index + key.length()); |
| 127 | + continue; |
| 128 | + } |
| 129 | + int idxEqual = index + key.length(); |
| 130 | + do { |
| 131 | + char c = sources.charAt(idxEqual); |
| 132 | + if (c == '=') { |
| 133 | + break; |
| 134 | + } else if (c == ' ' || c == '\t') { |
| 135 | + idxEqual++; |
| 136 | + if (idxEqual == sources.length() - 1) { |
| 137 | + idxEqual = -1; |
| 138 | + break; |
| 139 | + } |
| 140 | + } else { |
| 141 | + idxEqual = -1; |
| 142 | + break; |
| 143 | + } |
| 144 | + } while (true); |
| 145 | + if (idxEqual == -1 || idxEqual == sources.length() - 1) { |
| 146 | + break; |
| 147 | + } |
| 148 | + char quote = 0; |
| 149 | + int idxQuoteStart = idxEqual + 1; |
| 150 | + do { |
| 151 | + char c = sources.charAt(idxQuoteStart); |
| 152 | + if (c == '\'' || c == '\"') { |
| 153 | + quote = c; |
| 154 | + break; |
| 155 | + } else if (c == ' ' || c == '\t') { |
| 156 | + idxQuoteStart++; |
| 157 | + if (idxQuoteStart == sources.length() - 1) { |
| 158 | + idxQuoteStart = -1; |
| 159 | + break; |
| 160 | + } |
| 161 | + } else { |
| 162 | + idxQuoteStart = -1; |
| 163 | + break; |
| 164 | + } |
| 165 | + } while (true); |
| 166 | + if (idxQuoteStart == -1 || idxQuoteStart == sources.length() - 1) { |
| 167 | + break; |
| 168 | + } |
| 169 | + int idxQuoteEnd = sources.indexOf(quote, idxQuoteStart + 1); |
| 170 | + if (idxQuoteEnd == -1 || idxQuoteEnd == sources.length() - 1) { |
| 171 | + break; |
| 172 | + } |
| 173 | + String idStr = sources.substring(idxQuoteStart + 1, idxQuoteEnd).trim(); |
| 174 | + if (idStr.startsWith("$") && idStr.indexOf(' ') == -1) { |
| 175 | + vars.add(idStr); |
| 176 | + } |
| 177 | + index = sources.indexOf(key, idxQuoteEnd + 1); |
| 178 | + } |
| 179 | + return vars; |
| 180 | + } |
| 181 | + |
| 182 | + private static boolean containsBeginning(Set<String> set, String beginning) { |
| 183 | + for (String s : set) { |
| 184 | + if (s.startsWith(beginning)) { |
| 185 | + return true; |
| 186 | + } |
| 187 | + } |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | +// /* |
| 192 | +// * Read HTML/CSS sources from @j2sXHTML, @J2SXCSS or others |
| 193 | +// */ |
| 194 | +// boolean readXSources(BodyDeclaration node, String tagName, String prefix, String suffix) { |
| 195 | +// boolean existed = false; |
| 196 | +// Javadoc javadoc = node.getJavadoc(); |
| 197 | +// if (javadoc != null) { |
| 198 | +// List<?> tags = javadoc.tags(); |
| 199 | +// if (tags.size() != 0) { |
| 200 | +// for (Iterator<?> iter = tags.iterator(); iter.hasNext();) { |
| 201 | +// TagElement tagEl = (TagElement) iter.next(); |
| 202 | +// if (tagName.equals(tagEl.getTagName())) { |
| 203 | +// List<?> fragments = tagEl.fragments(); |
| 204 | +// StringBuffer buf = new StringBuffer(); |
| 205 | +// boolean isFirstLine = true; |
| 206 | +// String firstLine = null; |
| 207 | +// for (Iterator<?> iterator = fragments.iterator(); iterator.hasNext();) { |
| 208 | +// TextElement commentEl = (TextElement) iterator.next(); |
| 209 | +// String text = commentEl.getText().trim(); |
| 210 | +// if (isFirstLine) { |
| 211 | +// if (text.length() == 0) { |
| 212 | +// continue; |
| 213 | +// } |
| 214 | +// firstLine = text.trim(); |
| 215 | +// isFirstLine = false; |
| 216 | +// continue; |
| 217 | +// } |
| 218 | +// buf.append(text); |
| 219 | +// buf.append("\r\n"); |
| 220 | +// } |
| 221 | +// String sources = buf.toString().trim(); |
| 222 | +// sources = buildXSource(tagName, firstLine, sources); |
| 223 | +// buffer.append(prefix + sources + suffix); |
| 224 | +// existed = true; |
| 225 | +// } |
| 226 | +// } |
| 227 | +// } |
| 228 | +// } |
| 229 | +// return existed; |
| 230 | +// } |
| 231 | + |
| 232 | + |
| 233 | +} |
0 commit comments