01: /*
02: * Copyright (C) Chaperon. All rights reserved.
03: * -------------------------------------------------------------------------
04: * This software is published under the terms of the Apache Software License
05: * version 1.1, a copy of which has been included with this distribution in
06: * the LICENSE file.
07: */
08:
09: package net.sourceforge.chaperon.model.extended;
10:
11: import net.sourceforge.chaperon.model.Violations;
12:
13: /**
14: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
15: * @version CVS $Id: Optional.java,v 1.1 2003/12/12 14:11:34 benedikta Exp $
16: */
17: public class Optional extends PatternList {
18: public Optional() {
19: }
20:
21: public boolean isNullable() {
22: return true;
23: }
24:
25: /**
26: * Return a string representation of this pattern
27: *
28: * @return String representation of the pattern.
29: */
30: public String toString() {
31: StringBuffer buffer = new StringBuffer();
32:
33: if (getPatternCount() > 0) {
34: buffer.append(super .toString());
35: buffer.append("?");
36: }
37:
38: return buffer.toString();
39: }
40:
41: public String toString(PatternSet previous, PatternSet next) {
42: StringBuffer buffer = new StringBuffer();
43:
44: if (getPatternCount() > 0) {
45: buffer.append(super .toString(previous, next));
46: buffer.append("?");
47: }
48:
49: return buffer.toString();
50: }
51:
52: /**
53: * Create a clone this pattern.
54: *
55: * @return Clone of this pattern.
56: *
57: * @throws CloneNotSupportedException If an exception occurs during the cloning.
58: */
59: public Object clone() throws CloneNotSupportedException {
60: Optional clone = new Optional();
61:
62: for (int i = 0; i < getPatternCount(); i++)
63: clone.addPattern((Pattern) getPattern(i).clone());
64:
65: return clone;
66: }
67:
68: /**
69: * Validates this pattern.
70: *
71: * @return Return a list of violations, if this pattern isn't valid.
72: */
73: public Violations validate() {
74: Violations violations = new Violations();
75:
76: /*if (pattern==null)
77: violations.addViolation("Multiplier contains no pattern",
78: getLocation());*/
79: for (int i = 0; i < getPatternCount(); i++)
80: violations.addViolations(getPattern(i).validate());
81:
82: return violations;
83: }
84: }
|