View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  /*
13   * This file is protected by the Mozilla Public License
14   * located in euromath2-bin.zip file, downloadable from
15   * http://sourceforge.net/projects/euromath2/.
16   */
17  package sk.uniba.euromath.document.schema;
18  import java.util.ArrayList;
19  import java.util.List;
20  import org.w3c.dom.Element;
21  import sk.uniba.euromath.document.schema.plug.IAttributeListRuleP;
22  import sk.uniba.euromath.document.schema.plug.IElementSequenceRuleP;
23  import sk.uniba.euromath.document.schema.plug.INewElementRuleP;
24  import sk.uniba.euromath.document.schema.plug.IValueRule;
25  /***
26   * <p>
27   * Represents rule for one element (the context element), that is being created.
28   * This rule may consist of more than one expression, but all expressions must
29   * generate exactly one element with required name. These expressions are
30   * alternatives to each other; only one expression can be selected to generate
31   * new contents of element.
32   * </p>
33   * <p>
34   * Returned values depends on context element of this instance. This context
35   * element doesn't exist yet, it is being created.
36   * </p>
37   * <p>
38   * Representing class must properly implement <code>equals()</code> and
39   * <code>hashValue()</code> to ensure, that it can be properly placed into
40   * <code>HashMap</code>. These values should be computed only from enclosed
41   * expressions.
42   * </p>
43   * <p>
44   * When creating element with this rule, choose between the following:
45   * </p>
46   * <ol>
47   * <li>Creating elements: when creating elements, then text is always validated
48   * with AnyString, therefore there can be no text at all. So, when creating
49   * elements, no text is needed to be created.</li>
50   * <li>When creating text, just rely on the value returned by
51   * <code>getNewTextRule</code>, there is no need to creating elements.</li>
52   * </ol>
53   * <p>
54   * So, creation algorithm is as follows:
55   * </p>
56   * <ul>
57   * <li>If <code>getNewTextRule()</code> is not <code>null</code>, then
58   * text is required and must be created.</li>
59   * <li>If <code>getNewContent</code> doesn't return <code>null</code>,
60   * then some elements are required.</li>
61   * <li>When text AND elements must be created, then they must be alternatives
62   * to each other: you must create some text OR you must create some elements.
63   * </li>
64   * <li>Otherwise, no content must be created.</li>
65   * </ul>
66   * @author Martin Vysny
67   */
68  public class NewElementRule extends BaseRule {
69  	/***
70  	 * Instance of the original rule.
71  	 */
72  	private final INewElementRuleP rule;
73  	/***
74  	 * Constructs wrapper for the rule.
75  	 * @param rule the rule.
76  	 */
77  	NewElementRule(INewElementRuleP rule) {
78  		super();
79  		this.rule = rule;
80  	}
81  	/***
82  	 * <p>
83  	 * For each expression returns one list of attributes, that can be created
84  	 * by this rule. Only required attributes must be included in result set.
85  	 * Special care must be taken: there must not be two equal list rules (with
86  	 * equal rules). If all lists being returned are zero-length then a
87  	 * zero-length sequence must be returned instead.
88  	 * </p>
89  	 * <p>
90  	 * Should be computed only once, when this function is called for first
91  	 * time.
92  	 * </p>
93  	 * @return list of creatable attributes.
94  	 */
95  	public List<AttributeListRule> getLists() {
96  		List<IAttributeListRuleP> list = rule.getLists();
97  		List<AttributeListRule> result = new ArrayList<AttributeListRule>(list.size());
98  		for (IAttributeListRuleP ruleP:list)
99  			result.add(new AttributeListRule(ruleP));
100 		return result;
101 	}
102 	/***
103 	 * <p>
104 	 * Computes all possibilities of content, that can be generated by this rule
105 	 * and inserted into context element. There must be only such insertlists,
106 	 * that doesn't contain optional elements. All InsertLists with length equal
107 	 * to 1 should be grouped together in one InsertList.
108 	 * </p>
109 	 * <p>
110 	 * This content must be computed with existing attributes in mind.
111 	 * Expressions, whose contents violates given attribute set, must not be
112 	 * used.
113 	 * </p>
114 	 * <p>
115 	 * It must NOT be computed when instance of this object is created.
116 	 * </p>
117 	 * @param e element with already created attributes. It can be assumed that
118 	 * element has no content.
119 	 * @return array of Insertlists, that can generate sequence of element. If
120 	 * no content is required, return <code>null</code>.
121 	 */
122 	public ElementSequenceRule getNewContent(Element e) {
123 		IElementSequenceRuleP seqRule = rule.getNewContent(e);
124 		return new ElementSequenceRule(seqRule);
125 	}
126 	/***
127 	 * Computes and returns value rule, that can be inserted into context
128 	 * element.
129 	 * @param e element with already created attributes. It can be assumed that
130 	 * element has no content. It may not be inserted in the document.
131 	 * @return ValueRule for validating element's text context. If
132 	 * <code>null</code>, then no text content is required.
133 	 */
134 	public IValueRule getNewTextRule(Element e) {
135 		return rule.getNewTextRule(e);
136 	}
137 }