001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.lang.dsl;
018:
019: import java.util.List;
020: import java.util.Map;
021: import java.util.regex.Pattern;
022:
023: /**
024: * A single entry in a DSL mapping file
025: *
026: * @author etirelli
027: */
028: public interface DSLMappingEntry {
029:
030: public static final Section KEYWORD = new KeywordSection();
031: public static final Section CONDITION = new ConditionSection();
032: public static final Section CONSEQUENCE = new ConsequenceSection();
033: public static final Section ANY = new AnySection();
034: public static final MetaData EMPTY_METADATA = new DefaultDSLEntryMetaData(
035: "");
036:
037: /**
038: * Returns the section this mapping entry refers to
039: *
040: * @return
041: */
042: public DSLMappingEntry.Section getSection();
043:
044: /**
045: * Returns the meta data info about this mapping entry
046: *
047: * @return
048: */
049: public DSLMappingEntry.MetaData getMetaData();
050:
051: /**
052: * Returns the key of this mapping, i.e., the source
053: * that needs to be translated
054: *
055: * @return
056: */
057: public String getMappingKey();
058:
059: /**
060: * Returns the result of the translation
061: *
062: * @return
063: */
064: public String getMappingValue();
065:
066: /**
067: * Returns the compiled pattern based on the given MappingKey
068: * @return the keyPattern
069: */
070: public Pattern getKeyPattern();
071:
072: /**
073: * Returns the transformed mapping value using place holders for variables
074: * @return the valuePattern
075: */
076: public String getValuePattern();
077:
078: /**
079: * Returns the list of variables found in the given pattern key
080: * @return the variables
081: */
082: public Map getVariables();
083:
084: /**
085: * @param key the key to set
086: */
087: public void setMappingKey(String key);
088:
089: /**
090: * @param section the section to set
091: */
092: public void setSection(Section section);
093:
094: /**
095: * @param value the value to set
096: */
097: public void setMappingValue(String value);
098:
099: /**
100: * @param metadata the metadata to set
101: */
102: public void setMetaData(MetaData metadata);
103:
104: /**
105: * Returns a list of errors found in this mapping
106: * @return
107: */
108: public List getErrors();
109:
110: /**
111: * An inner interface for DSL mapping sections
112: * @author etirelli
113: *
114: */
115: public static interface Section extends Comparable {
116: public String getSymbol();
117: }
118:
119: /**
120: * An inner interface to represent any metadata
121: * associated with this entry. It is obviously
122: * implementation dependent.
123: *
124: * @author etirelli
125: *
126: */
127: public static interface MetaData extends Comparable {
128: public String toString();
129:
130: public String getMetaData();
131: }
132:
133: /**
134: * The keyword section, to allow mapping of keywords
135: *
136: * @author etirelli
137: */
138: public static class KeywordSection implements Section {
139: private static final String symbol = "[keyword]";
140:
141: private KeywordSection() {
142: }
143:
144: public String getSymbol() {
145: return symbol;
146: }
147:
148: public String toString() {
149: return symbol;
150: }
151:
152: public int hashCode() {
153: final int PRIME = 31;
154: int result = 1;
155: result = PRIME * result
156: + ((symbol == null) ? 0 : symbol.hashCode());
157: return result;
158: }
159:
160: public boolean equals(final Object obj) {
161: if (this == obj) {
162: return true;
163: }
164: if (obj == null) {
165: return false;
166: }
167: if (getClass() != obj.getClass()) {
168: return false;
169: }
170: final KeywordSection other = (KeywordSection) obj;
171: if (symbol == null) {
172: if (other.getSymbol() != null) {
173: return false;
174: }
175: } else if (!symbol.equals(other.getSymbol())) {
176: return false;
177: }
178: return true;
179: }
180:
181: public int compareTo(final Object arg0) {
182: return this .toString().compareTo(arg0.toString());
183: }
184: }
185:
186: /**
187: * The condition section, to allow mapping of the conditions
188: *
189: * @author etirelli
190: */
191: public static class ConditionSection implements Section {
192: private static final String symbol = "[condition]";
193:
194: private ConditionSection() {
195: }
196:
197: public String getSymbol() {
198: return symbol;
199: }
200:
201: public String toString() {
202: return symbol;
203: }
204:
205: public int hashCode() {
206: final int PRIME = 31;
207: int result = 1;
208: result = PRIME * result
209: + ((symbol == null) ? 0 : symbol.hashCode());
210: return result;
211: }
212:
213: public boolean equals(final Object obj) {
214: if (this == obj) {
215: return true;
216: }
217: if (obj == null) {
218: return false;
219: }
220: if (getClass() != obj.getClass()) {
221: return false;
222: }
223: final KeywordSection other = (KeywordSection) obj;
224: if (symbol == null) {
225: if (other.getSymbol() != null) {
226: return false;
227: }
228: } else if (!symbol.equals(other.getSymbol())) {
229: return false;
230: }
231: return true;
232: }
233:
234: public int compareTo(final Object arg0) {
235: return this .toString().compareTo(arg0.toString());
236: }
237: }
238:
239: /**
240: * The consequence section to allow the mapping
241: * of consequence elements
242: *
243: * @author etirelli
244: */
245: public static class ConsequenceSection implements Section {
246: private static final String symbol = "[consequence]";
247:
248: private ConsequenceSection() {
249: }
250:
251: public String getSymbol() {
252: return symbol;
253: }
254:
255: public String toString() {
256: return symbol;
257: }
258:
259: public int hashCode() {
260: final int PRIME = 31;
261: int result = 1;
262: result = PRIME * result
263: + ((symbol == null) ? 0 : symbol.hashCode());
264: return result;
265: }
266:
267: public boolean equals(final Object obj) {
268: if (this == obj) {
269: return true;
270: }
271: if (obj == null) {
272: return false;
273: }
274: if (getClass() != obj.getClass()) {
275: return false;
276: }
277: final KeywordSection other = (KeywordSection) obj;
278: if (symbol == null) {
279: if (other.getSymbol() != null) {
280: return false;
281: }
282: } else if (!symbol.equals(other.getSymbol())) {
283: return false;
284: }
285: return true;
286: }
287:
288: public int compareTo(final Object arg0) {
289: return this .toString().compareTo(arg0.toString());
290: }
291: }
292:
293: /**
294: * An element to indicate the mapping should be applicable
295: * to any section
296: *
297: * @author etirelli
298: */
299: public static class AnySection implements Section {
300:
301: private static final String symbol = "[*]";
302:
303: private AnySection() {
304: }
305:
306: public String getSymbol() {
307: return symbol;
308: }
309:
310: public String toString() {
311: return symbol;
312: }
313:
314: public int hashCode() {
315: final int PRIME = 31;
316: int result = 1;
317: result = PRIME * result
318: + ((symbol == null) ? 0 : symbol.hashCode());
319: return result;
320: }
321:
322: public boolean equals(final Object obj) {
323: if (this == obj) {
324: return true;
325: }
326: if (obj == null) {
327: return false;
328: }
329: if (getClass() != obj.getClass()) {
330: return false;
331: }
332: final KeywordSection other = (KeywordSection) obj;
333: if (symbol == null) {
334: if (other.getSymbol() != null) {
335: return false;
336: }
337: } else if (!symbol.equals(other.getSymbol())) {
338: return false;
339: }
340: return true;
341: }
342:
343: public int compareTo(final Object arg0) {
344: return this .toString().compareTo(arg0.toString());
345: }
346: }
347:
348: public static class DefaultDSLEntryMetaData implements
349: DSLMappingEntry.MetaData {
350:
351: private String metadata;
352:
353: public DefaultDSLEntryMetaData(final String metadata) {
354: this .metadata = metadata;
355: }
356:
357: public String getMetaData() {
358: return this .metadata;
359: }
360:
361: public String toString() {
362: return (this .metadata == null) ? "" : this .metadata;
363: }
364:
365: public int compareTo(final Object arg0) {
366: return this.toString().compareTo(arg0.toString());
367: }
368: }
369:
370: }
|