BrightSide Workbench Full Report + Source Code
DefaultElement.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.BufferedReader;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.servlet.ServletException;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.context.IElement;
29 import org.turro.elephant.impl.abstracts.AbstractElement;
30 import org.turro.elephant.impl.util.FileUtil;
31 import org.turro.elephant.layout.IManageable;
32 import org.turro.elephant.layout.IRenderable;
33 import org.turro.elephant.search.FoundList;
34 import org.turro.elephant.search.ISearchable;
35 import org.turro.elephant.search.SearchFormatter;
36 import org.turro.elephant.security.IDefendable;
37 import org.turro.elephant.web.context.WebResources;
38 import org.turro.i18n.I_;
39 
45 
46  protected String currentResource;
47 
49  public DefaultElement() {
50  super();
51  }
52 
53  @Override
54  public void loadData() throws ServletException, IOException {
55  super.loadData();
57  }
58 
59  @Override
60  public String getStart() {
61  return "<div class='default-element'>";
62  }
63 
64  @Override
65  public String getEnd() {
66  return "</div>";
67  }
68 
69  @Override
70  public void startConstruction() throws ServletException, IOException {
71  String res[] = getLocaleResourceNames(I_.api().usedLang());
72  PrintWriter out = getConstructor().getOut();
73  out.print(getStart());
74  for(int i = res.length -1 ; i >= 0; i--) {
75  currentResource = res[i];
76  out.print(getStartBody());
79  );
80  out.print(getEndBody());
81  }
82  out.print(getEnd());
83  }
84 
85  protected String[] getLocaleResourceNames(String preferredLocale) {
86  //ElContext elContext = ElContextMap.getContext(context.getPath());
87  WebResources resources = context.getResources();
88  List<String> names = resources.resourceNamesForLang(preferredLocale);
89  if(names.isEmpty()) {
90  for(String lang : context.getDefaultLocales()) {
91  if(!lang.equals(preferredLocale)) {
92  names = resources.resourceNamesForLang(lang);
93  }
94  if(!names.isEmpty()) break;
95  }
96  }
97  names.addAll(resources.globalResourceNames());
98  return names.toArray(new String[0]);
99  }
100 
101  /* ISearchable */
102 
103  @Override
104  public FoundList search(String value, boolean ignoreCase) {
105  String res[] = getLocaleResourceNames(I_.api().usedLang());
106  FoundList fl = new FoundList();
107  BufferedReader br;
108  String line;
109  SearchFormatter sf;
110  for(int i = res.length -1 ; i >= 0; i--) {
111  currentResource = res[i];
112  try {
116  ));
117  sf = new SearchFormatter(value, ignoreCase);
118  while((line = br.readLine()) != null) {
119  sf.processLine(line);
120  }
121  br.close();
122  if(sf.wasFound()) {
123  fl.addItem(
124  getId() == null ? "" : getId(),
125  (context.getPath().length() == 0 ? "/" : context.getPath()) + "#" + currentResource,
126  "<img src='" + ElephantContext.getRootResourcePath() + "/_internal/system/images/webpage.gif'/>",
127  context.getName(),
128  sf.getResult(),
129  (double)sf.getFound(),
130  value
131  );
132  }
133  } catch (IOException ex) {
134  Logger.getLogger(DefaultElement.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
135  }
136  }
137  return fl;
138  }
139 
140 }
FoundList search(String value, boolean ignoreCase)
String[] getLocaleResourceNames(String preferredLocale)
static BufferedReader getBufferedFile(String folder, String file)
Definition: FileUtil.java:146
void addItem(String id, String link, String image, String title, String value, double similarity, String query)
Definition: FoundList.java:66
List< String > resourceNamesForLang(String lang)
static I18nApiWrapper api()
Definition: I_.java:65