BrightSide Workbench Full Report + Source Code
AbstractParser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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 
19 package org.turro.parser;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.turro.elephant.context.IConstructor;
27 import org.turro.elephant.impl.util.FileUtil;
28 import org.turro.elephant.impl.util.Files;
29 import org.turro.marker.ElephantMarker;
30 
35 public abstract class AbstractParser implements IParser {
36 
38  protected String[] tokens;
39  protected PrintWriter out;
40  protected String parserName;
42  //protected KeyValueMap values = new KeyValueMap(Collections.EMPTY_MAP);
43 
44  public AbstractParser() {
45  initParser();
46  }
47 
49  return parserMacros;
50  }
51 
52  @Override
53  public boolean execute(IConstructor constructor, PrintWriter out, String[] tokens) {
54  this.constructor = constructor;
55  this.out = out;
56  this.tokens = tokens;
57  // escaped double dots
58  for(int i = 1; i < tokens.length; i++) {
59  tokens[i] = tokens[i].replaceAll("/:", ":");
60  }
61  if(tokens.length > 0) {
62  return doExecute();
63  }
64  return false;
65  }
66 
67  public abstract String getParserName();
68  protected abstract boolean doExecute();
69  protected abstract void explainMacros(ParserMacroSet macros);
70 
71  protected String getMacroName() {
72  return getToken(0);
73  }
74 
75  protected String getToken(String name) {
76  return getToken(name, true);
77  }
78 
79  protected String getToken(String name, boolean defParameter) {
80  ParserParameter pp = getParameter(name);
81  return pp != null ? tokens[pp.getOrder()] : pp.getDefaultValue();
82  }
83 
84  protected String getToken(String name, String def) {
85  ParserParameter pp = getParameter(name);
86  return pp != null ? tokens[pp.getOrder()] : def;
87  }
88 
89  protected String getToken(int index) {
90  return getToken(index, true);
91  }
92 
93  protected String getToken(int index, boolean defParameter) {
94  if(tokens.length > index) {
95  return tokens[index];
96  }
97  return defParameter ? getDefaultFor(index) : null;
98  }
99 
100  protected String getToken(int index, String def) {
101  if(tokens.length > index) {
102  return tokens[index];
103  }
104  return def;
105  }
106 
107  protected int getTokenSize() {
108  return tokens.length;
109  }
110 
111  protected String getDefaultFor(int index) {
112  ParserParameter pp = getParameter(index);
113  return pp != null ? pp.getDefaultValue() : null;
114  }
115 
116  protected void prepareProperties(ElephantMarker marker, String properties) {
117  try {
118  File file = Files.file(marker.getConstructor(), properties + ".properties");
119  if(file.exists()) {
120  marker.put("props", FileUtil.getOrderedProperties(file));
121  }
122  } catch (IOException ex) {
123  Logger.getLogger(DocumentationParser.class.getName()).log(Level.SEVERE, null, ex);
124  }
125  }
126 
127  private void initParser() {
129  }
130 
131  private ParserParameter getParameter(String name) {
132  for(ParserMacro pm : parserMacros) {
133  if(pm.getMacroName().equals(tokens[0])) {
134  for(ParserParameter pp : pm.getParameters()) {
135  if(pp.getName().equals(name)) {
136  return pp;
137  }
138  }
139  }
140  }
141  return null;
142  }
143 
144  private ParserParameter getParameter(int index) {
145  for(ParserMacro pm : parserMacros) {
146  if(pm.getMacroName().equals(tokens[0])) {
147  for(ParserParameter pp : pm.getParameters()) {
148  if(pp.getOrder() == index) {
149  return pp;
150  }
151  }
152  }
153  }
154  return null;
155  }
156 
157 }
static Properties getOrderedProperties(File file)
Definition: FileUtil.java:180
static File file(IConstructor constructor, String file)
Definition: Files.java:71
Object put(Object key, Object value)
String getToken(int index, String def)
void prepareProperties(ElephantMarker marker, String properties)
abstract String getParserName()
String getToken(String name, boolean defParameter)
String getToken(String name, String def)
abstract boolean doExecute()
String getToken(int index, boolean defParameter)
abstract void explainMacros(ParserMacroSet macros)
boolean execute(IConstructor constructor, PrintWriter out, String[] tokens)