|
|
|
|
|
|
|
*/ |
|
/* |
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
|
* contributor license agreements. See the NOTICE file distributed with |
|
* this work for additional information regarding copyright ownership. |
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
|
* (the "License"); you may not use this file except in compliance with |
|
* the License. You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
*/ |
|
|
|
package com.sun.org.apache.xerces.internal.impl.dtd.models; |
|
|
|
import com.sun.org.apache.xerces.internal.xni.QName; |
|
|
|
import com.sun.org.apache.xerces.internal.impl.dtd.XMLContentSpec; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class SimpleContentModel |
|
implements ContentModelValidator { |
|
|
|
// |
|
// Constants |
|
// |
|
|
|
|
|
public static final short CHOICE = -1; |
|
|
|
|
|
public static final short SEQUENCE = -1; |
|
|
|
// |
|
// Data |
|
// |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private QName fFirstChild = new QName(); |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private QName fSecondChild = new QName(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private int fOperator; |
|
|
|
/* this is the EquivClassComparator object */ |
|
//private EquivClassComparator comparator = null; |
|
|
|
|
|
// |
|
// Constructors |
|
// |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SimpleContentModel(short operator, QName firstChild, QName secondChild) { |
|
// |
|
// Store away the children and operation. This is all we need to |
|
// do the content model check. |
|
// |
|
// The operation is one of the ContentSpecNode.NODE_XXX values! |
|
|
|
fFirstChild.setValues(firstChild); |
|
if (secondChild != null) { |
|
fSecondChild.setValues(secondChild); |
|
} |
|
else { |
|
fSecondChild.clear(); |
|
} |
|
fOperator = operator; |
|
} |
|
|
|
// |
|
// ContentModelValidator methods |
|
// |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public int validate(QName[] children, int offset, int length) { |
|
|
|
// |
|
// According to the type of operation, we do the correct type of |
|
// content check. |
|
|
|
switch(fOperator) |
|
{ |
|
case XMLContentSpec.CONTENTSPECNODE_LEAF : |
|
|
|
if (length == 0) |
|
return 0; |
|
|
|
|
|
if (children[offset].rawname != fFirstChild.rawname) { |
|
return 0; |
|
} |
|
|
|
|
|
if (length > 1) |
|
return 1; |
|
break; |
|
|
|
case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE : |
|
// |
|
// If there is one child, make sure its the right type. If not, |
|
// then its an error at index 0. |
|
|
|
if (length == 1) { |
|
if (children[offset].rawname != fFirstChild.rawname) { |
|
return 0; |
|
} |
|
} |
|
|
|
// |
|
// If the child count is greater than one, then obviously |
|
// bad, so report an error at index 1. |
|
|
|
if (length > 1) |
|
return 1; |
|
break; |
|
|
|
case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE : |
|
// |
|
// If the child count is zero, that's fine. If its more than |
|
// zero, then make sure that all children are of the element |
|
// type that we stored. If not, report the index of the first |
|
// failed one. |
|
|
|
if (length > 0) |
|
{ |
|
for (int index = 0; index < length; index++) { |
|
if (children[offset + index].rawname != fFirstChild.rawname) { |
|
return index; |
|
} |
|
} |
|
} |
|
break; |
|
|
|
case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE : |
|
// |
|
// If the child count is zero, that's an error so report |
|
// an error at index 0. |
|
|
|
if (length == 0) |
|
return 0; |
|
|
|
// |
|
// Otherwise we have to check them all to make sure that they |
|
// are of the correct child type. If not, then report the index |
|
// of the first one that is not. |
|
|
|
for (int index = 0; index < length; index++) { |
|
if (children[offset + index].rawname != fFirstChild.rawname) { |
|
return index; |
|
} |
|
} |
|
break; |
|
|
|
case XMLContentSpec.CONTENTSPECNODE_CHOICE : |
|
// |
|
// There must be one and only one child, so if the element count |
|
// is zero, return an error at index 0. |
|
|
|
if (length == 0) |
|
return 0; |
|
|
|
|
|
if ((children[offset].rawname != fFirstChild.rawname) && |
|
(children[offset].rawname != fSecondChild.rawname)) { |
|
return 0; |
|
} |
|
|
|
|
|
if (length > 1) |
|
return 1; |
|
break; |
|
|
|
case XMLContentSpec.CONTENTSPECNODE_SEQ : |
|
// |
|
// There must be two children and they must be the two values |
|
// we stored, in the stored order. |
|
|
|
if (length == 2) { |
|
if (children[offset].rawname != fFirstChild.rawname) { |
|
return 0; |
|
} |
|
if (children[offset + 1].rawname != fSecondChild.rawname) { |
|
return 1; |
|
} |
|
} |
|
else { |
|
if (length > 2) { |
|
return 2; |
|
} |
|
|
|
return length; |
|
} |
|
|
|
break; |
|
|
|
default : |
|
throw new RuntimeException("ImplementationMessages.VAL_CST"); |
|
} |
|
|
|
|
|
return -1; |
|
} // validate |
|
|
|
} // class SimpleContentModel |