KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > validators > common > MixedContentModel


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999,2000 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 org.enhydra.apache.xerces.validators.common;
59
60 import org.enhydra.apache.xerces.framework.XMLContentSpec;
61 import org.enhydra.apache.xerces.utils.QName;
62 import org.enhydra.apache.xerces.validators.schema.SchemaGrammar;
63 import org.enhydra.apache.xerces.validators.schema.SubstitutionGroupComparator;
64
65 /**
66  * MixedContentModel is a derivative of the abstract content model base
67  * class that handles the special case of mixed model elements. If an element
68  * is mixed model, it has PCDATA as its first possible content, followed
69  * by an alternation of the possible children. The children cannot have any
70  * numeration or order, so it must look like this:
71  * <pre>
72  * &lt;!ELEMENT Foo ((#PCDATA|a|b|c|)*)&gt;
73  * </pre>
74  * So, all we have to do is to keep an array of the possible children and
75  * validate by just looking up each child being validated by looking it up
76  * in the list.
77  *
78  * @version $Id: MixedContentModel.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
79  */

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

88     /** The count of possible children that we have to deal with. */
89     private int fCount;
90
91     /** The list of possible children that we have to accept. */
92     private QName fChildren[];
93
94     /** The type of the children to support ANY. */
95     private int fChildrenType[];
96
97     /* this is the SubstitutionGroupComparator object */
98     private SubstitutionGroupComparator comparator = null;
99
100     /**
101      * True if mixed content model is ordered. DTD mixed content models
102      * are <em>always</em> unordered.
103      */

104     private boolean fOrdered;
105
106     /** Boolean to allow DTDs to validate even with namespace support. */
107     private boolean fDTD;
108
109     //
110
// Constructors
111
//
112

113     /**
114      * Constructs a mixed content model.
115      *
116      * @param count The child count.
117      * @param childList The list of allowed children.
118      *
119      * @exception CMException Thrown if content model can't be built.
120      */

121     public MixedContentModel(QName childList[],
122                              int childListType[],
123                              int offset, int length) throws CMException {
124         this(childList, childListType, offset, length, false, false);
125     }
126
127     /**
128      * Constructs a mixed content model.
129      *
130      * @param count The child count.
131      * @param childList The list of allowed children.
132      * @param ordered True if content must be ordered.
133      *
134      * @exception CMException Thrown if content model can't be built.
135      */

136     public MixedContentModel(QName childList[],
137                              int childListType[],
138                              int offset, int length,
139                              boolean ordered) throws CMException {
140         this(childList, childListType, offset, length, ordered, false);
141     }
142
143     /**
144      * Constructs a mixed content model.
145      *
146      * @param count The child count.
147      * @param childList The list of allowed children.
148      * @param ordered True if content must be ordered.
149      *
150      * @exception CMException Thrown if content model can't be built.
151      */

152     public MixedContentModel(QName childList[],
153                              int childListType[],
154                              int offset, int length,
155                              boolean ordered,
156                              boolean dtd) throws CMException {
157
158         // Make our own copy now, which is exactly the right size
159
fCount = length;
160         fChildren = new QName[fCount];
161         fChildrenType = new int[fCount];
162         for (int i = 0; i < fCount; i++) {
163             fChildren[i] = new QName(childList[offset + i]);
164             fChildrenType[i] = childListType[offset + i];
165         }
166         fOrdered = ordered;
167
168         fDTD = dtd;
169
170     } // <init>(QName[],int[],int,int,boolean,boolean)
171

172     // Unique Particle Attribution
173
public void checkUniqueParticleAttribution(SchemaGrammar gram) {
174         // rename back
175
for (int i = 0; i < fCount; i++)
176             fChildren[i].uri = gram.getContentSpecOrgUri(fChildren[i].uri);
177
178         // for mixed content model, it's only a sequence
179
// UPA checking is not necessary
180
}
181     // Unique Particle Attribution
182

183     //
184
// XMLContentModel methods
185
//
186

187     /**
188      * Check that the specified content is valid according to this
189      * content model. This method can also be called to do 'what if'
190      * testing of content models just to see if they would be valid.
191      * <p>
192      * A value of -1 in the children array indicates a PCDATA node. All other
193      * indexes will be positive and represent child elements. The count can be
194      * zero, since some elements have the EMPTY content model and that must be
195      * confirmed.
196      *
197      * @param children The children of this element. Each integer is an index within
198      * the <code>StringPool</code> of the child element name. An index
199      * of -1 is used to indicate an occurrence of non-whitespace character
200      * data.
201      * @param offset Offset into the array where the children starts.
202      * @param length The number of entries in the <code>children</code> array.
203      *
204      * @return The value -1 if fully valid, else the 0 based index of the child
205      * that first failed. If the value returned is equal to the number
206      * of children, then the specified children are valid but additional
207      * content is required to reach a valid ending state.
208      *
209      * @exception Exception Thrown on error.
210      */

211     public int validateContent(QName children[], int offset, int length)
212         throws Exception JavaDoc {
213
214         // must match order
215
if (fOrdered) {
216             int inIndex = 0;
217             for (int outIndex = 0; outIndex < length; outIndex++) {
218
219                 // ignore mixed text
220
final QName curChild = children[offset + outIndex];
221                 if (curChild.localpart == -1) {
222                     continue;
223                 }
224
225                 // element must match
226
int type = fChildrenType[inIndex];
227                 if (type == XMLContentSpec.CONTENTSPECNODE_LEAF) {
228                     if (fDTD) {
229                         if (fChildren[inIndex].rawname != children[offset + outIndex].rawname) {
230                             return outIndex;
231                         }
232                     }
233                     else {
234                         if (fChildren[inIndex].uri != children[offset + outIndex].uri &&
235                             fChildren[inIndex].localpart != children[offset + outIndex].localpart) {
236                             return outIndex;
237                         }
238                     }
239                 }
240
241                 // advance index
242
inIndex++;
243             }
244         }
245
246         // can appear in any order
247
else {
248             for (int outIndex = 0; outIndex < length; outIndex++)
249             {
250                 // Get the current child out of the source index
251
final QName curChild = children[offset + outIndex];
252
253                 // If its PCDATA, then we just accept that
254
if (curChild.localpart == -1)
255                     continue;
256
257                 // And try to find it in our list
258
int inIndex = 0;
259                 for (; inIndex < fCount; inIndex++)
260                 {
261                     int type = fChildrenType[inIndex];
262                     if (type == XMLContentSpec.CONTENTSPECNODE_LEAF) {
263                         if (fDTD) {
264                             if (curChild.rawname == fChildren[inIndex].rawname) {
265                                 break;
266                             }
267                         }
268                         else {
269                             if (curChild.uri == fChildren[inIndex].uri &&
270                                 curChild.localpart == fChildren[inIndex].localpart)
271                                 break;
272                         }
273                     }
274                 }
275
276                 // We did not find this one, so the validation failed
277
if (inIndex == fCount)
278                     return outIndex;
279             }
280         }
281
282         // Everything seems to be in order, so return success
283
return -1;
284
285     }
286
287     public int validateContentSpecial(QName children[], int offset, int length) throws Exception JavaDoc{
288          //TO DO here. cause Mixed Content is only for DTD, Schema is kind of different.
289
return validateContent(children,offset, length);
290     }
291
292     public void setSubstitutionGroupComparator(SubstitutionGroupComparator comparator) {
293         this.comparator = comparator;
294     }
295
296     /**
297      * Returns information about which elements can be placed at a particular point
298      * in the passed element's content model.
299      * <p>
300      * Note that the incoming content model to test must be valid at least up to
301      * the insertion point. If not, then -1 will be returned and the info object
302      * will not have been filled in.
303      * <p>
304      * If, on return, the info.isValidEOC flag is set, then the 'insert after'
305      * element is a valid end of content. In other words, nothing needs to be
306      * inserted after it to make the parent element's content model valid.
307      *
308      * @param fullyValid Only return elements that can be inserted and still
309      * maintain the validity of subsequent elements past the
310      * insertion point (if any). If the insertion point is at
311      * the end, and this is true, then only elements that can
312      * be legal final states will be returned.
313      * @param info An object that contains the required input data for the method,
314      * and which will contain the output information if successful.
315      *
316      * @return The value -1 if fully valid, else the 0 based index of the child
317      * that first failed before the insertion point. If the value
318      * returned is equal to the number of children, then the specified
319      * children are valid but additional content is required to reach a
320      * valid ending state.
321      *
322      * @see InsertableElementsInfo
323      */

324     public int whatCanGoHere(boolean fullyValid
325                             , InsertableElementsInfo info) throws Exception JavaDoc
326     {
327         //
328
// For this one, having the empty slot at the insertion point is
329
// a problem. So lets compress the array down. We know that it has
330
// to have at least the empty slot at the insertion point.
331
//
332
for (int index = info.insertAt; index < info.childCount-1; index++)
333             info.curChildren[index] = info.curChildren[index+1];
334         info.childCount--;
335
336         //
337
// Check the validity of the existing contents. If this is less than
338
// the insert at point, then return failure index right now
339
//
340
final int failedIndex = validateContent(info.curChildren, 0, info.childCount);
341         if ((failedIndex != -1) && (failedIndex < info.insertAt))
342             return failedIndex;
343
344         //
345
// Set any stuff we can know right off the bat for all cases. Mixed
346
// models can always hold PCData. And, since its always a repetition
347
// of a bunch of choice nodes, its always valid EOC.
348
//
349
info.canHoldPCData = true;
350         info.isValidEOC = true;
351
352         //
353
// Set the results count member and then see if we need to reallocate
354
// the outgoing arrays.
355
//
356
info.resultsCount = fCount;
357
358         if ((info.results == null) || (info.results.length < info.resultsCount))
359             info.results = new boolean[info.resultsCount];
360
361         if ((info.possibleChildren == null)
362         || (info.possibleChildren.length < info.resultsCount))
363         {
364             info.possibleChildren = new QName[info.resultsCount];
365             for (int i = 0; i < info.possibleChildren.length; i++) {
366                 info.possibleChildren[i] = new QName();
367             }
368         }
369
370         //
371
// If the fully valid parameter is set, then whether any child can
372
// go here is dependent upon the content model having been valid all
373
// the way to the end. If its not, nothing we put here is going to
374
// make it happy. If it was ok, then nothing we put here is ever going
375
// make it bad.
376
//
377
// So set up a boolean that can be used to set every possible child's
378
// insertable status below.
379
//
380
boolean bStatus = true;
381         if (fullyValid && (failedIndex < info.childCount))
382             bStatus = false;
383
384         //
385
// Fill in the possible children array, from our array. And set the
386
// boolean flag for each one to true because any of them can go
387
// anywhere.
388
//
389
for (int index = 0; index < fCount; index++)
390         {
391             info.possibleChildren[index].setValues(fChildren[index]);
392             info.results[index] = bStatus;
393         }
394
395         return -1;
396     }
397
398
399     public ContentLeafNameTypeVector getContentLeafNameTypeVector() {
400         return null;
401     }
402
403
404 } // class MixedContentModel
405
Popular Tags