BrightSide Workbench Full Report + Source Code
DefaultContext.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.context;
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.OutputStreamWriter;
23 import java.nio.file.Path;
24 import java.util.*;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.servlet.ServletException;
28 import org.turro.string.Strings;
29 import org.jdom.Attribute;
30 import org.jdom.Element;
31 import org.jdom.output.Format;
32 import org.jdom.output.XMLOutputter;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.context.IContext;
36 import org.turro.elephant.context.IElement;
37 import org.turro.elephant.impl.context.vc.FileVersionWrapper;
38 import org.turro.elephant.impl.navigation.Navigator;
39 import org.turro.elephant.impl.navigation.WebMap;
40 import org.turro.elephant.impl.repository.Repository;
41 import org.turro.elephant.impl.util.FileUtil;
42 import org.turro.elephant.impl.util.Files;
43 import org.turro.elephant.layout.IManageable;
44 import org.turro.elephant.search.FoundList;
45 import org.turro.elephant.search.ISearchable;
46 import org.turro.elephant.search.SearchFormatter;
47 import org.turro.elephant.security.IDefendable;
48 import org.turro.elephant.user.menu.UserMenus;
49 import org.turro.elephant.web.ElContext;
50 import org.turro.elephant.web.ElContextMap;
51 import org.turro.elephant.web.context.WebContext;
52 import org.turro.i18n.I_;
53 import org.turro.marker.ElephantMarker;
54 import org.turro.util.CompareUtil;
55 
60 public class DefaultContext implements IManageable, IDefendable, ISearchable, IContext, Comparable {
61  public static final String
62  //NO_SPACE_TABLE = "<table border='1' cellpadding='0' cellspacing='0'>",
63  //ANY_EXT_REGEXP = "\\.([a-zA-Z0-9\\_\\-]+)",
64  CONTEXT_FILE = "/_internal/context.xml";
66  protected String path;
67  protected File file;
68  protected IElement usedElement = null;
69  protected Element conf;
70  //protected IElement[] elements;
71  protected IContext[] children, ancestors;
72  protected IContext parent;
73  protected String[] defaultLocales;
75 
77  public DefaultContext() {
79  }
80 
81  @Override
82  public boolean equals(Object obj) {
83  return (
84  (obj instanceof IContext) &&
86  );
87  }
88 
89  @Override
90  public int hashCode() {
91  int hash = 7;
92  hash = 79 * hash + (this.path != null ? this.path.hashCode() : 0);
93  return hash;
94  }
95 
96  @Override
97  public String getId() {
98  return file.getName();
99  }
100 
101  @Override
102  public String getType() {
103  return "context";
104  }
105 
106  @Override
107  public IContext getContext() {
108  return this;
109  }
110 
111  @Override
112  public String getName() {
113  ElContext elContext = getElContext();
114  if(elContext != null) {
115  return getElContext().getWebContext().getName();
116  } else {
117  return getId();
118  }
119  }
120 
121  @Override
122  public String getName(String lang) {
123  ElContext elContext = getElContext();
124  if(elContext != null) {
125  return elContext.getWebContext().getNames().get(lang);
126  } else {
127  return getId();
128  }
129 // if(conf != null) {
130 // Element i18n = conf.getChild("i18n");
131 // if(i18n != null) {
132 // Element name = i18n.getChild("name");
133 // if(name != null) {
134 // String[] pref = getLocaleSufixes(lang);
135 // Element tmp;
136 // for(int i = 0; i < pref.length; i++) {
137 // tmp = name.getChild(pref[i]);
138 // if(tmp != null) {
139 // return tmp.getValue();
140 // }
141 // }
142 // }
143 // }
144 // }
145 // return getId();
146  }
147 
148  @Override
149  public String getExactName(String lang) {
150  return getElContext().getWebContext().getNames().get(lang);
151 // if(conf != null) {
152 // Element i18n = conf.getChild("i18n");
153 // if(i18n != null) {
154 // Element name = i18n.getChild("name");
155 // if(name != null) {
156 // Element elang = name.getChild(lang);
157 // if(elang != null) {
158 // return elang.getValue();
159 // }
160 // }
161 // }
162 // }
163 // return getId();
164  }
165 
166  @Override
167  public void setName(String lang, String newName) {
168  getElContext().getWebContext().getNames().put(lang, newName);
169 // if(conf == null) {
170 // Document doc = new Document(new Element("elephant-context"));
171 // conf = doc.getRootElement();
172 // }
173 // Element i18n = conf.getChild("i18n");
174 // if(i18n == null) {
175 // i18n = new Element("i18n");
176 // conf.addContent(i18n);
177 // }
178 // Element name = i18n.getChild("name");
179 // if(name == null) {
180 // name = new Element("name");
181 // i18n.addContent(name);
182 // }
183 // Element tmp = name.getChild(lang);
184 // if(tmp == null) {
185 // tmp = new Element(lang);
186 // name.addContent(tmp);
187 // }
188 // tmp.setText(newName);
189  }
190 
191  @Override
192  public String getTitle() {
193  String title = null;
194  if(usedElement != null) {
195  title = usedElement.getTitle();
196  }
197  return Strings.isBlank(title) ? getName() : title;
198  }
199 
200  @Override
201  public String getIcon() {
202  return getElContext().getWebContext().getImage();
203  }
204 
205  @Override
206  public void setIcon(String icon) {
207  }
208 
209  @Override
210  public String getRedirection() {
211  return getElContext().getWebContext().getRedirect();
212 // return attrSupport.getAttributeValue("redirect", null);
213  }
214 
215  @Override
216  public String getTarget() {
217  return getElContext().getWebContext().getRedirect();
218 // return attrSupport.getAttributeValue("target", null);
219  }
220 
221  @Override
223  if(usedElement == null) {
224  ElContext elContext = getElContext();
225  if(elContext != null) {
226  usedElement = getElContext().getWebContext().getElementInstance(); //getElements(null)[0];
228  }
229  }
230  return usedElement;
231  }
232 
233  @Override
234  public IElement getDefaultElement(String type) {
235 // IElement[] iels = getElements(type);
236 // if(iels.length > 0) return iels[0];
237  return null;
238  }
239 
240 // private IElement[] getElements(String type) {
241 // ArrayList els = new ArrayList();
242 // String strEl = null;
243 // List elList = null;
244 // IElement ielTmp = null;
245 // if(conf != null) {
246 // elList = conf.getChildren("element");
247 // for(Element el : (List<Element>) elList) {
248 // if(el != null) strEl = el.getAttributeValue("type");
249 // ielTmp = ContextFactory.getElement(this, strEl);
250 // ielTmp.setConfiguration(el, null);
251 // els.add(ielTmp);
252 // }
253 // }
254 // if(els.isEmpty()) {
255 // els.add(ContextFactory.getElement(this, "default-element"));
256 // }
257 // return (IElement[]) els.toArray(new IElement[0]);
258 // }
259 
260  @Override
261  public Set<String> getDefaultLocales() {
262  return getElContext().getDefaultLocales();
263 // if(defaultLocales == null) {
264 // getAncestors();
265 // String value = null;
266 // for(int i = ancestors.length - 1; i >= 0 && value == null; i--) {
267 // value = ((IManageable)ancestors[i]).getAttributes().getAttributeValue("lang", null);
268 // }
269 // if(value != null) {
270 // defaultLocales = value.split(" *, *");
271 // }
272 // else {
273 // defaultLocales = new String[0];
274 // }
275 // }
276 // return defaultLocales;
277  }
278 
279  @Override
281  this.constructor = constructor;
282 // if(path == null) {
283 // if(constructor.getRequest() == null) {
284 // this.path = "/";
285 // } else {
286 // this.path = constructor.getRequest().getServletPath();
287 // if(this.path == null) {
288 // this.path = "/";
289 // } else {
290 // int p = this.path.indexOf("?");
291 // if(p > -1) {
292 // this.path = this.path.substring(0, p);
293 // }
294 // }
295 // }
296 // }
297 // else {
298 // this.path = path.replaceAll("[^\\/a-zA-Z0-9\\-\\_\\.]", "_");
299 // }
300 // if(this.path.endsWith("/")) this.path = this.path.substring(0, this.path.length() - 1);
301  this.path = locateRealContext(constructor, path);
302  file = new File(ElephantContext.getRealPath(this.path));
303  ElContext elContext = getElContext();
304  if(elContext != null) {
306  attrSupport.addAttribute(new AttributeItem("showInNavigators", elContext.getWebContext().isShowInNavigators(), true));
307  attrSupport.addAttribute(new AttributeItem("showAlways", elContext.getWebContext().isShowAlways(), false));
308  attrSupport.addAttribute(new AttributeItem("printContext", elContext.getWebContext().isPrintContext(), true));
309  attrSupport.addAttribute(new AttributeItem("contextRole", elContext.getWebContext().getRole(), AttributeItem.STRING_ATTR, null));
312  attrSupport.addAttribute(new AttributeItem("noindex", elContext.getWebContext().isNoIndex(), false));
313  attrSupport.addAttribute(new AttributeItem("nofollow", elContext.getWebContext().isNoFollow(), false));
314  attrSupport.addAttribute(new AttributeItem("print-version", elContext.getWebContext().isPrintVersion(), true));
315  attrSupport.addAttribute(new AttributeItem("control-version", elContext.getWebContext().isControlVersion(), true));
316  attrSupport.addAttribute(new AttributeItem("traversalNavigation", elContext.getWebContext().isTraversalNavigation(), false));
317  }
318 // readConfiguration();
319 // attrSupport.addAttribute(new AttributeItem("order",
320 // (conf != null ? conf.getAttributeValue("order") : null), AttributeItem.STRING_ATTR, "000"));
321 // attrSupport.addAttribute(new AttributeItem("lang",
322 // (conf != null ? conf.getAttributeValue("lang") : null), AttributeItem.STRING_ATTR, null));
323 // attrSupport.addAttribute(new AttributeItem("showInNavigators",
324 // (conf != null ? conf.getAttributeValue("showInNavigators") : null), AttributeItem.BOOLEAN_ATTR, "true"));
325 // attrSupport.addAttribute(new AttributeItem("showAlways",
326 // (conf != null ? conf.getAttributeValue("showAlways") : null), AttributeItem.BOOLEAN_ATTR, "false"));
327 // attrSupport.addAttribute(new AttributeItem("printContext",
328 // (conf != null ? conf.getAttributeValue("printContext") : null), AttributeItem.BOOLEAN_ATTR, "true"));
329 // attrSupport.addAttribute(new AttributeItem("contextRole",
330 // (conf != null ? conf.getAttributeValue("contextRole") : null), AttributeItem.STRING_ATTR, null));
331 // attrSupport.addAttribute(new AttributeItem("redirect",
332 // (conf != null ? conf.getAttributeValue("redirect") : null), AttributeItem.STRING_ATTR, null));
333 // attrSupport.addAttribute(new AttributeItem("target",
334 // (conf != null ? conf.getAttributeValue("target") : null), AttributeItem.STRING_ATTR, null));
335 // attrSupport.addAttribute(new AttributeItem("noindex",
336 // (conf != null ? conf.getAttributeValue("noindex") : null), AttributeItem.BOOLEAN_ATTR, "false"));
337 // attrSupport.addAttribute(new AttributeItem("nofollow",
338 // (conf != null ? conf.getAttributeValue("nofollow") : null), AttributeItem.BOOLEAN_ATTR, "false"));
339 // attrSupport.addAttribute(new AttributeItem("print-version",
340 // (conf != null ? conf.getAttributeValue("print-version") : null), AttributeItem.BOOLEAN_ATTR, "true"));
341 // attrSupport.addAttribute(new AttributeItem("control-version",
342 // (conf != null ? conf.getAttributeValue("control-version") : null), AttributeItem.BOOLEAN_ATTR, "true"));
343 // attrSupport.addAttribute(new AttributeItem("traversalNavigation",
344 // (conf != null ? conf.getAttributeValue("traversalNavigation") : null), AttributeItem.BOOLEAN_ATTR, "false"));
345 // attrSupport.addAttribute(new AttributeItem("onTop",
346 // (conf != null ? conf.getAttributeValue("onTop") : null), AttributeItem.STRING_ATTR, null));
347 // attrSupport.addAttribute(new AttributeItem("onBottom",
348 // (conf != null ? conf.getAttributeValue("onBottom") : null), AttributeItem.STRING_ATTR, null));
349  }
350 
351  @Override
352  public String getFullPath() {
353  // prepend application context
354  String tmp = ElephantContext.getRootWebPath() + path;
355  return (tmp.length() == 0 ? "/" : tmp);
356  }
357 
358  @Override
359  public String getWebPath() {
360  String redirection = getRedirection();
361  if(Strings.isBlank(redirection)) {
362  return getFullPath();
363  } else if(redirection.startsWith("/")) {
364  return ElephantContext.getRootWebPath() + redirection;
365  } else if(redirection.contains("://")) {
366  return redirection;
367  } else {
368  return getFullPath() + "/" + redirection;
369  }
370  }
371 
372  @Override
373  public String getPath() {
374  return path;
375  }
376 
377  @Override
378  public int getLevel() {
379  return path == null ? 0 : path.split("\\/").length - 1;
380  }
381 
382  @Override
383  public File getFile() {
384  return file;
385  }
386 
387 // @Override
388 // public String getVerbosePath() {
389 // return getVerbosePath(null);
390 // }
391 //
392 // @Override
393 // public String getVerbosePath(IContext root) {
394 // StringBuilder sb = new StringBuilder();
395 // boolean render = root == null;
396 // for(IContext c : getAncestors()) {
397 // if(render) {
398 // sb.append(c.getName());
399 // sb.append(" ");
400 // }
401 // if(!render && c.equals(root)) render = true;
402 // }
403 // return sb.toString();
404 // }
405 
406  @Override
407  public void startConstruction() throws ServletException, IOException {
408  final IElement element = getDefaultElement();
409 
410  ElContext elContext = ElContextMap.getContext(path);
411  String template = elContext.getTemplate();
412  WebContext webContext = elContext.getWebContext();
413 
415  if(webContext.isControlVersion() ||
416  constructor.isInRole("control-version:admin") ||
418  em.put("fileVersion", new FileVersionWrapper(element, 20, 3));
419  }
420  // Desactivat fins no tenir les plantilles carregades
421  if(webContext.isPrintVersion() && canPrint()) {
422  String printPath = ElephantContext.getRootWebPath() + "/print_" + getContext().getPath();
423  em.put("printPath", printPath);
424  em.put("printVersion", "<div id='print-version'><a class='newwindow' target='_blank' rel='nofollow' href='" + printPath + "'>" +
425  I_.get("Printer version") +
426  "</a></div>");
427  }
428  Navigator navigator = new Navigator(elContext);
429  navigator.setDynamicLoad("dynamic".equals(ElephantContext.getContextLoad()));
430  em.put("nav", navigator);
431  em.put("traversal", webContext.isTraversalNavigation());
432  em.put("body", element);
433  em.put("webmap", new WebMap(constructor));
434  element.loadData();
436  em.process("layout", template);
437  }
438 
439  @Override
440  public IContext getParent() {
441  if(parent == null) {
442  ElContext elContext = getElContext();
443  if(elContext != null && elContext.getParent() != null) {
444  parent = elContext.getParent().getContext();
445  }
446 // if(!"/".equals(path) && path.indexOf("/") != -1) {
447 // parent = ContextFactory.getContext(constructor, path.substring(0, path.lastIndexOf("/")));
448 // }
449  }
450  return parent;
451  }
452 
453  @Override
454  public IContext[] getAncestors() {
455  if(ancestors == null) {
456  ArrayList v = new ArrayList();
457  ElContext elContext = getElContext();
458  if(elContext != null && !elContext.getAncestors().isEmpty()) {
459  for(ElContext child : elContext.getAncestors()) {
460  v.add(child.getContext());
461  }
462  }
463 // IContext tmp = this;
464 // v.add(this);
465 // while((tmp = tmp.getParent()) != null) {
466 // v.add(0, tmp);
467 // }
468  ancestors = (IContext[]) v.toArray(new IContext[0]);
469  }
470  return ancestors;
471  }
472 
473  @Override
474  public IContext[] getChildren() {
475  if(children == null) {
476  ArrayList v = new ArrayList();
477  ElContext elContext = getElContext();
478  if(elContext != null && !elContext.getChildren().isEmpty()) {
479  for(ElContext child : elContext.getChildren()) {
480  v.add(child.getContext());
481  }
482  }
483 // IContext ctmp;
484 // if(file.isDirectory()) {
485 // File[] children = file.listFiles();
486 // for(int i = 0; i < children.length; i++)
487 // if(children[i].isDirectory())
488 // if(!constructor.isFileInternal(children[i])) {
489 // ctmp = ContextFactory.getContext(constructor, path + "/" + children[i].getName());
490 // v.add(ctmp);
491 // }
492 // }
493  children = (IContext[]) v.toArray(new IContext[0]);
494  }
495  return children;
496  }
497 
498  @Override
499  public IContext[] getSiblings() {
500  ElContext elContext = getElContext();
501  ArrayList v = new ArrayList();
502  if(elContext != null) {
503  for(ElContext child : elContext.getSiblings()) {
504  v.add(child.getContext());
505  }
506  }
507  return (IContext[]) v.toArray(new IContext[0]);
508 // IContext parent = getParent();
509 // if(parent == null) {
510 // return new IContext[] { this };
511 // }
512 // else {
513 // return parent.getChildren();
514 // }
515  }
516 
517 // @Override
518 // public IElement getElementById(String idel) {
519 // if(conf != null) {
520 // Element element;
521 // try {
522 // element = (Element) XPath.selectSingleNode(conf, "//element[@id='" + idel + "']");
523 // if(element != null) {
524 // IElement el = ContextFactory.getElement(this, element);
525 // if(el != null) {
526 // return el;
527 // }
528 // }
529 // } catch (JDOMException ex) {
530 // Logger.getLogger(DefaultContext.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
531 // }
532 // }
533 // return null;
534 // }
535 
536  public String[] getBaseNames() {
537  TreeSet set = new TreeSet();
538  String name;
539  if(file.isDirectory()) {
540  File[] children = file.listFiles();
541  for(int i = 0; i < children.length; i++)
542  if(children[i].isFile()) {
543  name = FileUtil.baseName(children[i].getName());
544  if(name != null) {
545  set.add(name);
546  }
547  }
548  }
549  return (String[])set.toArray(new String[0]);
550  }
551 
552  public String[] getLocaleResources(String preferredLocale) {
553  String[] baseName = getBaseNames(),
554  prefixes = getLocaleSufixes(preferredLocale);
555  TreeSet set = new TreeSet();
556  String name;
557  File resource;
558  if(file.isDirectory()) {
559  for(int b = 0; b < baseName.length; b++) {
560  for(int p = 0; p < prefixes.length; p++) {
561  resource = new File(
562  file.getAbsolutePath() + "/" +
563  baseName[b].replaceAll(FileUtil.VALID_EXT_REGEXP + "$", prefixes[p] + ".$1")
564  );
565  if(resource.exists()) {
566  set.add(resource.getName());
567  break;
568  }
569  }
570  }
571  }
572  return (String[])set.toArray(new String[0]);
573  }
574 
575 // @Override
576 // public File getAbsoluteLocaleResource(String resource, String preferredLocale) {
577 // String[] prefixes = getLocaleSufixes(preferredLocale);
578 // File resFile;
579 // for(int p = 0; p < prefixes.length; p++) {
580 // resFile = new File(ElephantContext.getRealPath(resource).replaceAll(
581 // ANY_EXT_REGEXP + "$", prefixes[p] + ".$1"
582 // ));
583 // if(resFile.exists()) {
584 // return resFile;
585 // }
586 // }
587 // return null;
588 // }
589 
590  @Override
592  return constructor;
593  }
594 
595  @Override
596  public String getTypeName() {
597  return I_.get("element." + getType());
598  }
599 
600  @Override
602  return attrSupport;
603  }
604 
605  @Override
606  public void applyChanges() {
607  List attrs = conf.getAttributes();
608  Iterator it = attrs.iterator();
609  Attribute a;
610  while(it.hasNext()) {
611  a = (Attribute) it.next();
612  if(attrSupport.getAttribute(a.getName()) == null) {
613  it.remove();
614  }
615  }
616  it = attrSupport.keySet().iterator();
617  AttributeItem ai;
618  while(it.hasNext()) {
619  ai = (AttributeItem) attrSupport.getAttribute((String) it.next());
620  if(ai.getValue() != null) {
621  conf.setAttribute(ai.name, ai.getValue());
622  }
623  }
624  saveConfiguration();
625  WebContext.removeFrom(Path.of(path));
627  }
628 
629  @Override
630  public void populateFromParent() {
631  if(getParent() == null) return;
632 
634 
635  attrSupport.getAttribute("contextRole").setValue(mgb.getAttributes().getAttributeValue("contextRole", null));
637  attrSupport.getAttribute("nofollow").setBooleanValue(mgb.getAttributes().getAttributeBooleanValue("nofollow", false));
638  attrSupport.getAttribute("print-version").setBooleanValue(mgb.getAttributes().getAttributeBooleanValue("print-version", false));
639  attrSupport.getAttribute("control-version").setBooleanValue(mgb.getAttributes().getAttributeBooleanValue("control-version", false));
640  attrSupport.getAttribute("traversalNavigation").setBooleanValue(mgb.getAttributes().getAttributeBooleanValue("traversalNavigation", false));
641  }
642 
643  @Override
644  public String[] getLocaleSufixes(String preferredLocale) {
645  ArrayList v = new ArrayList();
646  String defaultLocale = null;
647  Set<String> defLocale = getDefaultLocales();
648  Locale[] locale = new Locale[] {I_.api().used()}; //getLocales().getPreferred().toArray(new Locale[0]);
649  if(!defLocale.isEmpty()) {
650  defaultLocale = defLocale.iterator().next();
651  }
652  // check preferredLocale
653  if(preferredLocale != null) {
654  v.add(preferredLocale);
655  if(preferredLocale.equals(defaultLocale))
656  if(!v.contains("")) v.add("");
657  }
658  // visitor preferences
659  String language, country, variant, curr;
660  int languageLength, countryLength, variantLength;
661  for(int l = 0; l < locale.length; l++) {
662  language = locale[l].getLanguage();
663  languageLength = language.length();
664  country = locale[l].getCountry();
665  countryLength = country.length();
666  variant = locale[l].getVariant();
667  variantLength = variant.length();
668  if(languageLength > 0 && countryLength > 0 && variantLength > 0) {
669  curr = "_" + language + "_" + country + "_" + variant;
670  if(!v.contains(curr)) v.add(curr);
671  if(curr.equals(defaultLocale))
672  if(!v.contains("")) v.add("");
673  }
674  if(languageLength > 0 && countryLength > 0) {
675  curr = "_" + language + "_" + country;
676  if(!v.contains(curr)) v.add(curr);
677  if(curr.equals(defaultLocale))
678  if(!v.contains("")) v.add("");
679  }
680  if(languageLength > 0) {
681  curr = "_" + language;
682  if(!v.contains(curr)) v.add(curr);
683  if(curr.equals(defaultLocale))
684  if(!v.contains("")) v.add("");
685  }
686  }
687  // and finally context preferences
688 // for(int l = 0; l < defLocale.length; l++) {
689 // if(!v.contains(defLocale[l])) v.add(defLocale[l]);
690 // if(defLocale[l].equals(defaultLocale))
691 // if(!v.contains("")) v.add("");
692 // }
693  for(String dl : defLocale) {
694  if(!v.contains(dl)) v.add(dl);
695  if(dl.equals(defaultLocale))
696  if(!v.contains("")) v.add("");
697  }
698  return (String[]) v.toArray(new String[0]);
699  }
700 
702  return null;// new Repository(this);
703  }
704 
705  private String getOrder() {
706  ElContext elContext = getElContext();
707  return elContext == null ? "000" : elContext.getWebContext().getOrder();
708  }
709 
710 // private void readConfiguration() {
711 // SAXBuilder builder = new SAXBuilder();
712 // Document doc;
713 // try {
714 // File confFile = new File(ElephantContext.getRealPath(path + CONTEXT_FILE));
715 // if(confFile.exists()) {
716 // doc = builder.build(confFile);
717 // conf = doc.getRootElement();
718 // }
719 // } catch (IOException | JDOMException ex) {
720 // Logger.getLogger(DefaultContext.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
721 // }
722 // }
723 
724  private void saveConfiguration() {
725  try (OutputStreamWriter fw = FileUtil.getFileWriter(
727  Format fm = Format.getPrettyFormat();
728  fm.setEncoding(ElephantContext.getEncoding());
729  XMLOutputter xo = new XMLOutputter(fm);
730  xo.output(conf.getDocument(), fw);
731  } catch (IOException ex) {
732  Logger.getLogger(DefaultContext.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
733  }
734  }
735 
736  @Override
737  public boolean inPath(IContext context) {
738  return context.getFullPath().matches(getFullPath() + "(\\/.*)?");
739  }
740 
741  // TODO: if order attr, compare, if no order with order, order first,
742  // otherwise, compare ids.
743  @Override
744  public int compareTo(Object o) {
746  int result = 0;
747 // if(conf != null && dc.conf != null) {
748 // result = CompareUtil.compare(getAttributes().getAttributeValue("order", ""),
749 // dc.getAttributes().getAttributeValue("order", ""));
750 //
751 // }
752  result = CompareUtil.compare(getOrder(), dc.getOrder());
753  if(result == 0) {
754  result = CompareUtil.compare(getName(), dc.getName());
755  }
756  if(result == 0) {
757  result = CompareUtil.compare(getId(), dc.getId());
758  }
759  return result;
760  }
761 
762  @Override
763  public boolean canDisplay() {
764  boolean showAlways = attrSupport.getAttributeBooleanValue("showAlways", false);
765  return isSelectable() && (showAlways || isInRole());
766  }
767 
768  @Override
769  public boolean isSelectable() {
770  return getElContext().getWebContext().isShowInNavigators(); //attrSupport.getAttributeBooleanValue("showInNavigators", false);
771  }
772 
773  @Override
774  public boolean isInRole() {
775  if(!UserMenus.isInRole(path)) return false;
777  if(parent != null && !parent.isInRole()) return false;
778  String contextRole = attrSupport.getAttributeValue("contextRole", null);
779  if(contextRole == null) return true;
780  return constructor.isInRole(contextRole);
781  }
782 
783  @Override
784  public boolean canPrint() {
785  boolean canPrint = attrSupport.getAttributeBooleanValue("printContext", true);
786  return (canPrint && isInRole());
787  }
788 
789  @Override
790  public FoundList search(String value, boolean ignoreCase) {
791  FoundList fl = new FoundList();
792  SearchFormatter sf = new SearchFormatter(value, ignoreCase);
793  sf.processLine(getName());
794  if(sf.wasFound()) {
795  fl.addItem(
796  getId() == null ? "" : getId(),
797  (getPath().length() == 0 ? "/" : getPath()),
798  "<img src='" + ElephantContext.getRootResourcePath() + "/_internal/system/images/context.gif'/>",
799  getName(),
800  sf.getResult(),
801  (double)sf.getFound(),
802  value
803  );
804  }
805  return fl;
806  }
807 
808  private ElContext getElContext() {
809  return ElContextMap.getContext(path);
810  }
811 
812  public static String locateRealContext(IConstructor constructor, String path) {
813  if(path == null) {
814  if(constructor.getRequest() == null) {
815  path = "/";
816  } else {
817  path = extractPath(constructor.getRequest().getServletPath());
818  }
819  } else {
820  path = path.replaceAll("[^\\/a-zA-Z0-9\\-\\_\\.]", "_");
821  }
823  if(path.endsWith("/")) path = path.substring(0, path.length() - 1);
824  return path;
825  }
826 
827  public static String extractPath(String path) {
828  if(path == null) {
829  path = "/";
830  } else {
831  int p = path.indexOf("?");
832  if(p > -1) {
833  path = path.substring(0, p);
834  }
835  }
836  return path;
837  }
838 
839 }
String getAttributeValue(String name, String defaultValue)
boolean getAttributeBooleanValue(String name, boolean defaultValue)
String[] getLocaleSufixes(String preferredLocale)
FoundList search(String value, boolean ignoreCase)
String[] getLocaleResources(String preferredLocale)
void setConstructor(IConstructor constructor, String path)
static String locateRealContext(IConstructor constructor, String path)
void setName(String lang, String newName)
void setDynamicLoad(boolean dynamicLoad)
Definition: Navigator.java:340
static String baseName(String file)
Definition: FileUtil.java:191
static final String VALID_EXT_REGEXP
Definition: FileUtil.java:52
static boolean exists(String path)
Definition: Files.java:80
static String toExisting(String path)
Definition: Files.java:85
void addItem(String id, String link, String image, String title, String value, double similarity, String query)
Definition: FoundList.java:66
static boolean isInRole(String path)
Definition: UserMenus.java:56
static ElContext getContext(IConstructor constructor)
TreeSet< ElContext > getChildren()
Definition: ElContext.java:196
TreeSet< ElContext > getSiblings()
Definition: ElContext.java:217
TreeSet< ElContext > getAncestors()
Definition: ElContext.java:235
static boolean removeFrom(Path path)
static String get(String msg)
Definition: I_.java:41
static I18nApiWrapper api()
Definition: I_.java:65
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
void setConfiguration(IConstructor constructor, ElContext context)