19 package org.turro.zkoss.text;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import org.turro.string.Strings;
25 import org.turro.elephant.context.ElephantContext;
26 import org.zkoss.zul.impl.InputElement;
34 public static final String[]
37 private int startElement, endElement, selectionStart, selectionEnd;
38 private String text, tag, content, avoid[];
39 private InputElement input;
40 private boolean creating;
41 private HashMap<String, String> attributes =
new HashMap<>();
43 public WikiElement(
int selectionStart, InputElement input, String tag) {
44 this.selectionStart = selectionStart;
45 this.selectionEnd = selectionStart;
48 this.text = input.getText();
52 public WikiElement(
int selectionStart,
int selectionEnd, InputElement input) {
53 this(selectionStart, selectionEnd, input,
null);
56 public WikiElement(
int selectionStart,
int selectionEnd, InputElement input, String avoid[]) {
57 this.selectionStart = selectionStart;
58 this.selectionEnd = selectionEnd;
61 this.text = input.getText();
72 this.content = content;
76 return !Strings.isBlank(tag);
79 public boolean isTag(String tag) {
80 return this.tag ==
null ? false : this.tag.equals(tag);
92 attributes.put(value,
"no-value");
96 for(String s : attributes.keySet()) {
97 if(attributes.get(s).equals(
"no-value")) {
105 String classes = attributes.get(
"&class");
106 if(classes !=
null) {
107 if(!classes.contains(sclass)) {
108 classes +=
" " + sclass;
113 attributes.put(
"&class", classes);
117 Iterator<String> it = attributes.keySet().iterator();
118 while(it.hasNext()) {
119 String v = attributes.get(it.next());
120 if(!
"no-value".equals(v)) {
126 private void initialize() {
127 startElement = avoided();
128 if(startElement > -1 && text.charAt(startElement + 1) !=
'/') {
129 endElement = text.substring(startElement).indexOf(
"]") + startElement;
130 int p = text.substring(startElement, endElement).indexOf(
"(");
131 tag = Strings.extract(text.substring(startElement + 1),
"([a-z0-9]+)");
133 tag = text.substring(startElement + 1, startElement + p);
134 String attr = text.substring(startElement + p + 1, endElement - 1);
136 for(String s : attr.split(
",")) {
137 int pa = s.indexOf(
":");
139 if(s.charAt(pa - 1) ==
'\\') {
140 int pa2 = s.indexOf(
":", pa + 1);
149 attributes.put(s,
"no-value");
151 attributes.put(s.substring(0, pa), s.substring(pa + 1).replaceAll(
"\\:",
"\\\\\\:"));
159 private void initializeTag(String tag) {
160 startElement = text.substring(0, selectionStart).lastIndexOf(
"[" + tag);
161 endElement = text.indexOf(
"]", startElement);
163 if(startElement > -1 && startElement < selectionStart && selectionStart < endElement) {
171 int pointer = creating ? selectionStart : startElement;
172 if(!Strings.isBlank(tag)) {
174 input.setSelectedText(pointer, endElement + 1,
"",
false);
176 String tagString = elementString();
177 input.setSelectedText(pointer, pointer, tagString,
false);
179 if(!Strings.isBlank(content)) {
180 pointer += tagString.length();
181 input.setSelectedText(pointer, pointer, content,
false);
182 pointer += content.length();
184 input.setSelectedText(pointer, pointer,
"[/" + tag +
"]",
false);
191 private String elementString() {
192 StringBuilder sb =
new StringBuilder();
193 sb.append(
"[").append(tag);
194 boolean firstDone =
false;
195 if(!attributes.isEmpty()) {
198 for(String k : attributes.keySet()) {
199 String v = attributes.get(k);
200 if(
"no-value".equals(v)) {
208 for(String k : attributes.keySet()) {
209 String v = attributes.get(k);
210 if(!
"no-value".equals(v)) {
215 sb.append(
":").append(v.replaceAll(
"\\:",
"\\\\\\:"));
222 return sb.toString();
227 int pointer = creating ? selectionStart : startElement;
228 if(!Strings.isBlank(tag)) {
230 input.setSelectedText(pointer, endElement + 1,
"",
false);
232 String tagString = imageString(image);
233 input.setSelectedText(pointer, pointer, tagString,
false);
240 private String imageString(File image) {
241 StringBuilder sb =
new StringBuilder();
242 sb.append(
"[").append(tag).append(
" ")
245 return sb.toString();
248 private int avoided() {
249 int start = text.substring(0, selectionStart).lastIndexOf(
"[");
252 boolean accept =
true;
253 for(String s : avoid) {
254 if(text.substring(start + 1).startsWith(s)) {
259 if(accept || start == 0) {
262 start = text.substring(0, start - 1).lastIndexOf(
"[");
static String getRelativePath(String path)
static String getRootWebPath()
void addNoValuedAttribute(String value)
boolean renderImage(File image)
void forgetAndCreate(String newTag)
HashMap< String, String > getAttributes()
void addClass(String sclass)
void removeValuedAttributes()
String getFistNoneValued()
WikiElement(int selectionStart, InputElement input, String tag)
boolean isTag(String tag)
WikiElement(int selectionStart, int selectionEnd, InputElement input, String avoid[])
void setContent(String content)
static final String[] NON_BLOCK_ELEMENTS
WikiElement(int selectionStart, int selectionEnd, InputElement input)