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.pattern;
10:
11: import net.sourceforge.chaperon.model.Violations;
12:
13: /**
14: * This class represents a concatenation of pattern.
15: *
16: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
17: * @version CVS $Id: Concatenation.java,v 1.3 2003/12/09 19:55:52 benedikta Exp $
18: */
19: public class Concatenation extends PatternList {
20: /**
21: * Create a pattern for a concatenation of pattern.
22: */
23: public Concatenation() {
24: }
25:
26: /**
27: * Return a string representation of this pattern
28: *
29: * @return String representation of the pattern.
30: */
31: public String toString() {
32: StringBuffer buffer = new StringBuffer();
33:
34: for (int i = 0; i < getPatternCount(); i++)
35: buffer.append(getPattern(i).toString());
36:
37: if ((getMinOccurs() == 1) && (getMaxOccurs() == 1)) {
38: // nothing
39: } else if ((getMinOccurs() == 0) && (getMaxOccurs() == 1))
40: buffer.append("?");
41: else if ((getMinOccurs() == 0)
42: && (getMaxOccurs() == Integer.MAX_VALUE))
43: buffer.append("*");
44: else if ((getMinOccurs() == 1)
45: && (getMaxOccurs() == Integer.MAX_VALUE))
46: buffer.append("+");
47: else {
48: buffer.append("{");
49: buffer.append(String.valueOf(getMinOccurs()));
50: buffer.append(",");
51: buffer.append(String.valueOf(getMaxOccurs()));
52: buffer.append("}");
53: }
54:
55: return buffer.toString();
56: }
57:
58: /**
59: * Create a clone of this pattern.
60: *
61: * @return Clone of this pattern.
62: *
63: * @throws CloneNotSupportedException If an exception occurs during the cloning.
64: */
65: public Object clone() throws CloneNotSupportedException {
66: Concatenation clone = new Concatenation();
67:
68: clone.setMinOccurs(getMinOccurs());
69: clone.setMaxOccurs(getMaxOccurs());
70:
71: for (int i = 0; i < getPatternCount(); i++)
72: clone.addPattern((Pattern) getPattern(i).clone());
73:
74: return clone;
75: }
76:
77: /**
78: * Validates this pattern.
79: *
80: * @return Return a list of violations, if this pattern isn't valid.
81: */
82: public Violations validate() {
83: Violations violations = new Violations();
84:
85: if (getPatternCount() == 0)
86: violations.addViolation(
87: "Concatenation doesn't contain any elements",
88: getLocation());
89:
90: for (int i = 0; i < getPatternCount(); i++)
91: violations.addViolations(getPattern(i).validate());
92:
93: return violations;
94: }
95: }
|