View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  /*
13   * This file is protected by the Mozilla Public License located in
14   * euromath2-bin.zip file, downloadable from
15   * http://sourceforge.net/projects/euromath2/.
16   */
17  package sk.uniba.euromath.document.schema;
18  import org.w3c.dom.Attr;
19  import org.w3c.dom.Element;
20  import sk.uniba.euromath.document.DomCore;
21  import sk.uniba.euromath.document.schema.plug.SchemaException;
22  /***
23   * Document's content modifier helper. For chosen operation returns all
24   * possibilities, that will result in valid document. Not intended to be
25   * subclassed nor instantiated by clients.
26   * @author Martin Vysny
27   */
28  public class DocumentSchema {
29  	private final SchemaReferences refs;
30  	/***
31  	 * Constructor. Not intended to be used by clients.
32  	 * @param doc the document instance.
33  	 */
34  	public DocumentSchema(DomCore doc) {
35  		super();
36  		refs = new SchemaReferences(doc);
37  	}
38  	/***
39  	 * Returns rule for given element. It can be further queried for various
40  	 * info.
41  	 * @param element the context element, for which rule will be created.
42  	 * @return instance of element rule
43  	 */
44  	public ElementRule getElementRule(Element element) {
45  		return new ElementRule(refs, element);
46  	}
47  	/***
48  	 * Checks, whether given attribute is deletable from its element. It is OK
49  	 * to use this function. However, for subsequent queries to attribute's
50  	 * element please use <code>ElementRule</code> instance, it's more
51  	 * effective.
52  	 * @param attribute attribute to check.
53  	 * @return false if attribute is not deletable.
54  	 */
55  	public boolean isDeletableAttribute(Attr attribute) {
56  		return new ElementRule(refs, attribute.getOwnerElement())
57  				.isDeletableAttribute(attribute);
58  	}
59  	/***
60  	 * Gets attribute rule for given attribute. It can be used to validate
61  	 * textual value of attribute. It is OK to use this function. However, for
62  	 * subsequent queries to attribute's element please use
63  	 * <code>ElementRule</code> instance, it's more effective.
64  	 * @param attribute return rule for this attribute.
65  	 * @return rule for this attribute.
66  	 */
67  	public AttributeRule getAttributeRule(Attr attribute) {
68  		return new ElementRule(refs, attribute.getOwnerElement())
69  				.getAttributeRule(attribute);
70  	}
71  	/***
72  	 * Validates this document.
73  	 * @throws SchemaException if something goes wrong in the process of
74  	 * validation.
75  	 */
76  	public void validate() throws SchemaException {
77  		Validator.getInstance().validate(
78  				refs.doc.getDocument().getDocumentElement(), refs);
79  	}
80  	/***
81  	 * Returns object containing references to loaded schema objects.
82  	 * @return schema references object
83  	 */
84  	public SchemaReferences getRefs() {
85  		return refs;
86  	}
87  }