19 package org.turro.elephant.web;
22 import java.io.IOException;
23 import java.nio.file.Path;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.Optional;
28 import java.util.TreeSet;
29 import java.util.function.Function;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import javax.servlet.ServletException;
33 import org.turro.string.Strings;
34 import org.jdom.Document;
35 import org.jdom.Element;
36 import org.jdom.JDOMException;
37 import org.jdom.input.SAXBuilder;
38 import org.turro.collections.Item;
39 import org.turro.collections.Tree;
40 import org.turro.elephant.context.Application;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.elephant.context.IConstructor;
43 import org.turro.elephant.context.IContext;
44 import org.turro.elephant.context.IElement;
45 import org.turro.elephant.impl.abstracts.AbstractElement;
46 import org.turro.elephant.impl.context.ContextFactory;
47 import org.turro.elephant.impl.repository.Repository;
48 import org.turro.elephant.impl.util.Files;
49 import org.turro.elephant.search.FoundList;
50 import org.turro.elephant.search.SearchFormatter;
51 import org.turro.elephant.user.menu.UserMenus;
52 import org.turro.elephant.web.context.WebContext;
53 import org.turro.elephant.web.context.WebResources;
54 import org.turro.plugin.contacts.IContact;
55 import org.turro.util.CompareUtil;
61 public class ElContext implements Comparable<ElContext> {
63 public static final String
69 protected String
title, path, params;
103 return (Strings.isBlank(tmp) ?
"/" : tmp + (Strings.isBlank(params) ?
"" : params));
127 return path ==
null ? 0 : path.split(
"\\/").length - 1;
141 while(ctx !=
null && !ctx.
equals(root)) {
142 result = ctx.
getName() +
" " + result;
189 !
getPath().startsWith(
"/user");
202 TreeSet<ElContext> statics =
new TreeSet<>();
204 if(!ctx.getWebContext().isDynamic()) statics.add(ctx);
210 TreeSet<ElContext> visible =
new TreeSet<>();
212 if(ctx.canDisplay()) visible.add(ctx);
219 TreeSet<ElContext>
set =
new TreeSet<>();
228 TreeSet<ElContext> visible =
new TreeSet<>();
230 if(ctx.canDisplay()) visible.add(ctx);
236 TreeSet<ElContext> ancestors =
new TreeSet<>();
247 boolean found =
false;
249 if(found && sibling.canDisplay())
return getLastChildren(sibling);
250 if(sibling.equals(
this)) found =
true;
259 if(!ctxs.isEmpty()) {
260 return getLastChildren(ctxs.last());
268 if(!
set.isEmpty())
return set.first();
271 boolean found =
false;
273 if(found && sibling.canDisplay())
return sibling;
274 if(sibling.equals(
this)) found =
true;
298 level = path ==
null ? 0 : path.split(
"\\/").length - 1;
302 if(
folder.isDirectory()) {
303 File[] subfolders =
folder.listFiles();
304 for(File subfolder: subfolders) {
305 if(subfolder.isDirectory() && !subfolder.isHidden()) {
307 if(
Files.
exists(path +
"/" + subfolder.getName() + CONTEXT_FILE) ||
334 if(file ==
null)
return true;
335 String fileName = file.isFile() ? file.getName() : file.getAbsolutePath();
337 if(internals !=
null) {
338 String tmp[] = internals.split(
",");
339 for(String s : tmp) {
340 if(fileName.matches(Strings.convertToRegEx(s)))
return true;
343 return (fileName.matches(
"google.*\\.html")) ||
344 (fileName.matches(
"site.*\\.xml")) ||
345 (fileName.matches(
"robots\\.txt")) ||
346 (fileName.contains(
"_internal")) ||
347 (fileName.contains(
"_zul")) ||
348 (fileName.contains(
"resources")) ||
349 (fileName.contains(
"WEB-INF")) ||
350 (fileName.contains(
"META-INF"));
369 private void readConfiguration() {
375 Element conf = readConfFile(confFile);
377 Element i18n = conf.getChild(
"i18n");
379 Element name = i18n.getChild(
"name");
381 for(Element language : (List<Element>) name.getChildren()) {
399 for(Element el : (List<Element>) conf.getChildren(
"element")) {
402 IElement iel = ContextFactory.getContextElement(ictx, el);
403 if(iel instanceof AbstractElement) {
404 ((AbstractElement) iel).setConfiguration(el,
null);
405 el.getChildren(
"attrib").forEach(attrib -> {
407 ((Element) attrib).getAttributeValue(
"name"),
408 ((Element) attrib).getAttributeValue(
"value"));
415 Element layout = conf.getChild(
"layout");
417 Element
template = layout.getChild(
"template");
418 if(
template !=
null) {
421 Element localTemplate = layout.getChild(
"local-template");
422 if(localTemplate !=
null) {
431 private Element readConfFile(File confFile) {
432 if(confFile.exists()) {
433 SAXBuilder builder =
new SAXBuilder();
436 doc = builder.build(confFile);
437 return doc.getRootElement();
438 }
catch (IOException | JDOMException ex) {
439 Logger.getLogger(
ElContext.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
477 private boolean dynamicDone =
false;
479 private void generateDynamic() {
492 for(Item item : (List<Item>) tree.getItems()) {
493 addCategoriesChildren(
getPath(), item, onParam);
497 private void addCategoriesChildren(String root, Item current, Function<Item, String> onParam) {
498 String tmpPath = root + current.getPath();
500 if(!Strings.isBlank(realPath)) {
501 File subfolder =
new File(realPath);
502 ElContext child =
new ElContext(Strings.unpunctuateKeepPath(tmpPath), onParam.apply(current), subfolder,
this);
509 if(!current.isLeaf()) {
510 for(Item item : (List<Item>) current.getChildren()) {
511 child.addCategoriesChildren(root, item, onParam);
526 return findRoot(
this);
534 return findRoot(ctx.
parent);
541 return findTemplate(
this);
544 private String findTemplate(
ElContext ctx) {
545 if(ctx ==
null)
return "default-web";
549 return findTemplate(ctx.
parent);
578 int p = path.indexOf(
"?");
580 path = path.substring(0, p);
603 result = CompareUtil.compare(
getId(), o.
getId());
606 result = CompareUtil.compare(path, o.path);
616 hash = 89 * hash + Objects.hashCode(this.path);
628 if (getClass() != obj.getClass()) {
632 if (!Objects.equals(
this.path, other.path)) {
IConstructor getConstructor()
boolean isInRole(String role)
static Application getApplication()
static String getRealPath(String path)
static String getRootResourcePath()
static String getRootWebPath()
static String getTemplatesRoot()
static String getSiteInternalFiles()
static IContext getContext(IConstructor constructor)
static boolean exists(String path)
void addItem(String id, String link, String image, String title, String value, double similarity, String query)
static ElContext addContext(ElContext context)
WebContext getWebContext()
TreeSet< ElContext > children
TreeSet< ElContext > getChildren()
boolean equals(Object obj)
Repository getRepository()
TreeSet< ElContext > getSiblings()
Set< String > getDefaultLocales()
TreeSet< ElContext > getAncestors()
TreeSet< ElContext > getStaticChildren()
boolean inPath(ElContext context)
FoundList search(String value, boolean ignoreCase)
void setTitle(String title)
static boolean isInternal(File file)
TreeSet< ElContext > getVisibleSiblings()
ElContext getNext(boolean doChildren)
ElContext(String path, String params, File folder, ElContext parent)
boolean canShowTo(IContact contact)
void generateCategoriesChildren(Tree tree, Function< Item, String > onParam)
int compareTo(ElContext o)
boolean hasRestrictions()
static String extractPath(String path)
void construct(IConstructor constructor)
void resetDefaultLocales()
ElContext(String path, File folder, ElContext parent)
String getRepositoryPath()
static final String CONTEXT_FOLDER
String getVerbosePath(ElContext root)
Set< String > defaultLocales
SocialImage getSocialImage()
WebResources getResources()
TreeSet< ElContext > getVisibleChildren()
static SocialImage getImage(String url)
void setPrintContext(boolean printContext)
static boolean existsConf(Path path)
void setNoIndex(boolean noIndex)
void setTraversalNavigation(boolean traversalNavigation)
void setPrintVersion(boolean printVersion)
void setNoFollow(boolean noFollow)
Map< String, String > getNames()
IElement getElementInstance()
void setShowAlways(boolean showAlways)
void setRedirect(String redirect)
void setDynamic(boolean dynamic)
static WebContext loadFrom(Path path)
static Path repository(Path path)
void setOrder(String order)
void setTarget(String target)
void setControlVersion(boolean controlVersion)
boolean isShowInNavigators()
void setRole(String role)
void setShowInNavigators(boolean showInNavigators)
Map< String, String > getAttributes()
void setElementClass(String elementClass)
void setTemplate(String template)
void setLocal(String local)
boolean isInRole(String role)
void setRootCategories(ElContext root)
void setConfiguration(IConstructor constructor, ElContext context)