19 package org.turro.parser;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.string.ObjectString;
29 import org.turro.string.Strings;
30 import org.turro.annotation.ExternalParser;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.impl.navigation.Navigator;
33 import org.turro.elephant.impl.navigation.NavigatorType;
34 import org.turro.elephant.impl.navigation.WebMap;
35 import org.turro.elephant.web.ElContext;
36 import org.turro.elephant.web.ElContextMap;
37 import org.turro.html.HTMLHelper;
56 boolean verbosePath =
false;
57 boolean hidden =
false;
58 boolean external =
false;
60 List<ElContext> result =
null;
61 String clickAction =
"", cssClass =
"";
62 while(currToken <
tokens.length && root !=
null) {
63 if(
"popup".equals(
tokens[currToken])) {
67 else if(
"verbose".equals(
tokens[currToken])) {
70 else if(
"hidden".equals(
tokens[currToken])) {
73 else if(
"external".equals(
tokens[currToken])) {
76 else if(
"parent".equals(
tokens[currToken])) {
79 else if(
"root".equals(
tokens[currToken])) {
82 else if(
"locate".equals(
tokens[currToken])) {
83 if(
tokens.length > currToken + 1) {
88 else if(
"match".equals(
tokens[currToken])) {
89 if(
tokens.length > currToken + 1) {
90 root = getSingleContextFrom(root,
tokens[currToken + 1]);
94 else if(
"level".equals(
tokens[currToken])) {
95 if(
tokens.length > currToken + 1) {
96 root = getLevelContextFrom(root,
tokens[currToken + 1]);
100 else if(
"children".equals(
tokens[currToken])) {
104 else if(
"all".equals(
tokens[currToken])) {
105 result = getAllFrom(root,
false);
108 else if(
"leaf".equals(
tokens[currToken])) {
109 result = getAllFrom(root,
true);
112 else if(
"ancestors".equals(
tokens[currToken])) {
116 else if(
"siblings".equals(
tokens[currToken])) {
120 else if(
"search".equals(
tokens[currToken])) {
121 if(
tokens.length > currToken + 1) {
122 result = getContextsFrom(root,
tokens[currToken + 1]);
126 else if(
"self".equals(
tokens[currToken])) {
127 result =
new ArrayList<>();
134 boolean list =
"list".equals(
tokens[0]);
135 if(result !=
null && !result.isEmpty()) {
137 hh.
startTag(
"ul",
"class='m_list'");
138 }
else if(result.size() > 1) {
142 if(hidden || ctx.canDisplay()) {
149 if(Strings.isBlank(ctx.getTarget()) || !Strings.isBlank(clickAction)) {
150 hh.
startAnchor(ctx.getWebPath(),
null, cssClass, clickAction);
156 hh.
write(ctx.getVerbosePath(root));
158 hh.
write(ctx.getName());
163 }
else if(result.lastIndexOf(ctx) < result.size() - 1) {
168 if(!list && result.size() > 1) {
173 hh.
startTag((list ?
"div" :
"span"),
"class='m_error'");
182 if(
"location".equals(
getToken(1))) {
187 }
catch (IOException ex) {
190 }
else if(
"traversal".equals(
getToken(1))) {
193 navigator.
setShowTitle((Boolean) ObjectString.parseNativeString(
getToken(2), Boolean.class,
true));
196 }
catch (IOException ex) {
199 }
else if(
"options".equals(
getToken(1))) {
202 navigator.
setLevel((Integer) ObjectString.parseNativeString(
getToken(3), Integer.class,
true));
203 navigator.
setPreloaded((Integer) ObjectString.parseNativeString(
getToken(4), Integer.class,
true));
208 }
catch (IOException ex) {
211 }
else if(
"tabulator".equals(
getToken(1))) {
214 navigator.
setLevel((Integer) ObjectString.parseNativeString(
getToken(2), Integer.class,
true));
217 }
catch (IOException ex) {
225 wm.
draw((Integer) ObjectString.parseNativeString(
getToken(1), Integer.class,
true));
226 }
catch (IOException ex) {
253 .
addParameter(
"behavioural_keywords",
false,
null,
true)
258 .
addParameter(
"navigational_keywords",
false,
null,
true)
276 private static List<ElContext> getAllFrom(
ElContext root,
boolean leaf) {
277 ArrayList v =
new ArrayList();
278 processContext(root, v, leaf,
false);
282 private static void processContext(ElContext root, Collection v,
boolean leaf,
boolean addRoot) {
283 Set<ElContext> children = root.getChildren();
284 if(addRoot && (!leaf || (leaf && children.isEmpty()))) {
287 for(ElContext c : children) {
288 processContext(c, v, leaf,
true);
292 private static ElContext getLevelContextFrom(ElContext root, String level) {
293 ElContext[] ctxs = root.getAncestors().toArray(
new ElContext[0]);
294 int idx = Integer.valueOf(level);
295 if(idx < ctxs.length) {
297 }
else if(ctxs.length > 0) {
298 return ctxs[ctxs.length - 1];
304 private static List<ElContext> getContextsFrom(ElContext root, String regExp) {
305 ArrayList v =
new ArrayList();
306 processSearchFrom(root, regExp, v);
310 private static ElContext getSingleContextFrom(ElContext root, String regExp) {
311 return processSingleSearchFrom(root, regExp);
314 private static void processSearchFrom(ElContext root, String regExp, Collection v) {
315 for(ElContext ctx : root.getChildren()) {
316 if(ctx.getId().matches(regExp)) {
319 processSearchFrom(ctx, regExp, v);
323 private static ElContext processSingleSearchFrom(ElContext root, String regExp) {
324 for(ElContext ctx : root.getChildren()) {
325 if(ctx.getId().matches(regExp)) {
328 ctx = processSingleSearchFrom(ctx, regExp);
329 if(ctx !=
null)
return ctx;
335 if(sequence ==
null || !sequence.contains(
"$"))
return sequence;
336 StringBuilder sb =
new StringBuilder(sequence);
338 while((p = sb.indexOf(
"$", start)) > -1) {
342 return sb.toString();
static String getContextLoad()
static String getRootWebPath()
static String logMsg(String msg)
void setDrawDecorators(boolean drawDecorators)
void setDynamicLoad(boolean dynamicLoad)
void setShowCurrent(boolean showCurrent)
void setType(NavigatorType type)
void setPreloaded(int preloaded)
void setShowTitle(boolean showTitle)
static ElContext getContext(IConstructor constructor)
TreeSet< ElContext > getChildren()
TreeSet< ElContext > getSiblings()
TreeSet< ElContext > getAncestors()
HTMLGenerator endAllTags()
HTMLGenerator startTag(String tag)
HTMLGenerator startTargetAnchor(String url, String target, String hint)
HTMLGenerator startExtAnchor(String url, String hint)
HTMLGenerator startAnchor(String url, String hint)
HTMLGenerator write(String value)
String getToken(String name)
static String escapeGrouping(String sequence)
void explainMacros(ParserMacroSet macros)
ParserMacro addMacro(String macroName)
ParserMacro addPossibleValue(String value)
void copyParametersFrom(String macroName, ParserMacroSet macros)
ParserMacro addParameter(String name, boolean optional)
ElContext getRenderingContext()