KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > dtd > models > MixedContentModel


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.dtd.models;
59
60 import com.sun.org.apache.xerces.internal.xni.QName;
61
62 import com.sun.org.apache.xerces.internal.impl.dtd.XMLContentSpec;
63
64 /**
65  * @version $Id: MixedContentModel.java,v 1.5 2002/05/29 17:59:37 neilg Exp $
66  *
67  * MixedContentModel is a derivative of the abstract content model base
68  * class that handles the special case of mixed model elements. If an element
69  * is mixed model, it has PCDATA as its first possible content, followed
70  * by an alternation of the possible children. The children cannot have any
71  * numeration or order, so it must look like this:
72  * <pre>
73  * &lt;!ELEMENT Foo ((#PCDATA|a|b|c|)*)&gt;
74  * </pre>
75  * So, all we have to do is to keep an array of the possible children and
76  * validate by just looking up each child being validated by looking it up
77  * in the list.
78  *
79  */

80 public class MixedContentModel
81     implements ContentModelValidator {
82
83     //
84
// Data
85
//
86

87     /** The count of possible children that we have to deal with. */
88     private int fCount;
89
90     /** The list of possible children that we have to accept. */
91     private QName fChildren[];
92
93     /** The type of the children to support ANY. */
94     private int fChildrenType[];
95
96     /* this is the EquivClassComparator object */
97     //private EquivClassComparator comparator = null;
98

99     /**
100      * True if mixed content model is ordered. DTD mixed content models
101      * are <em>always</em> unordered.
102      */

103     private boolean fOrdered;
104
105     //
106
// Constructors
107
//
108

109     /**
110      * Constructs a mixed content model.
111      *
112      * @param children The list of allowed children.
113      * @param type The list of the types of the children.
114      * @param offset The start offset position in the children.
115      * @param length The child count.
116      * @param ordered True if content must be ordered.
117      * @param dtd True if it is for a DTDGrammar.
118      *
119      */

120     public MixedContentModel(QName[] children, int[] type, int offset, int length , boolean ordered) {
121         // Make our own copy now, which is exactly the right size
122
fCount = length;
123         fChildren = new QName[fCount];
124         fChildrenType = new int[fCount];
125         for (int i = 0; i < fCount; i++) {
126             fChildren[i] = new QName(children[offset + i]);
127             fChildrenType[i] = type[offset + i];
128         }
129         fOrdered = ordered;
130
131     }
132
133     //
134
// ContentModelValidator methods
135
//
136

137     
138     /**
139      * Check that the specified content is valid according to this
140      * content model. This method can also be called to do 'what if'
141      * testing of content models just to see if they would be valid.
142      * <p>
143      * A value of -1 in the children array indicates a PCDATA node. All other
144      * indexes will be positive and represent child elements. The count can be
145      * zero, since some elements have the EMPTY content model and that must be
146      * confirmed.
147      *
148      * @param children The children of this element. Each integer is an index within
149      * the <code>StringPool</code> of the child element name. An index
150      * of -1 is used to indicate an occurrence of non-whitespace character
151      * data.
152      * @param offset Offset into the array where the children starts.
153      * @param length The number of entries in the <code>children</code> array.
154      *
155      * @return The value -1 if fully valid, else the 0 based index of the child
156      * that first failed. If the value returned is equal to the number
157      * of children, then the specified children are valid but additional
158      * content is required to reach a valid ending state.
159      *
160      */

161     public int validate(QName[] children, int offset, int length) {
162         
163         // must match order
164
if (fOrdered) {
165             int inIndex = 0;
166             for (int outIndex = 0; outIndex < length; outIndex++) {
167
168                 // ignore mixed text
169
final QName curChild = children[offset + outIndex];
170                 if (curChild.localpart == null) {
171                     continue;
172                 }
173
174                 // element must match
175
int type = fChildrenType[inIndex];
176                 if (type == XMLContentSpec.CONTENTSPECNODE_LEAF) {
177                     if (fChildren[inIndex].rawname != children[offset + outIndex].rawname) {
178                         return outIndex;
179                     }
180                 }
181                 else if (type == XMLContentSpec.CONTENTSPECNODE_ANY) {
182                     String JavaDoc uri = fChildren[inIndex].uri;
183                     if (uri != null && uri != children[outIndex].uri) {
184                         return outIndex;
185                     }
186                 }
187                 else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_LOCAL) {
188                     if (children[outIndex].uri != null) {
189                         return outIndex;
190                     }
191                 }
192                 else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER) {
193                     if (fChildren[inIndex].uri == children[outIndex].uri) {
194                         return outIndex;
195                     }
196                 }
197                 
198                 // advance index
199
inIndex++;
200             }
201         }
202
203         // can appear in any order
204
else {
205             for (int outIndex = 0; outIndex < length; outIndex++)
206             {
207                 // Get the current child out of the source index
208
final QName curChild = children[offset + outIndex];
209     
210                 // If its PCDATA, then we just accept that
211
if (curChild.localpart == null)
212                     continue;
213     
214                 // And try to find it in our list
215
int inIndex = 0;
216                 for (; inIndex < fCount; inIndex++)
217                 {
218                     int type = fChildrenType[inIndex];
219                     if (type == XMLContentSpec.CONTENTSPECNODE_LEAF) {
220                         if (curChild.rawname == fChildren[inIndex].rawname) {
221                             break;
222                         }
223                     }
224                     else if (type == XMLContentSpec.CONTENTSPECNODE_ANY) {
225                         String JavaDoc uri = fChildren[inIndex].uri;
226                         if (uri == null || uri == children[outIndex].uri) {
227                             break;
228                         }
229                     }
230                     else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_LOCAL) {
231                         if (children[outIndex].uri == null) {
232                             break;
233                         }
234                     }
235                     else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER) {
236                         if (fChildren[inIndex].uri != children[outIndex].uri) {
237                             break;
238                         }
239                     }
240                     // REVISIT: What about checking for multiple ANY matches?
241
// The content model ambiguity *could* be checked
242
// by the caller before constructing the mixed
243
// content model.
244
}
245
246                 // We did not find this one, so the validation failed
247
if (inIndex == fCount)
248                     return outIndex;
249             }
250         }
251
252         // Everything seems to be in order, so return success
253
return -1;
254     } // validate
255

256 } // class MixedContentModel
257
Popular Tags