1 /*
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3 * @LastModified: Oct 2017
4 */
5 /*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22 package com.sun.org.apache.xerces.internal.impl.xs;
23
148 * check whether simple type derived is valid derived from base,
149 * given a subset of {restriction, extension}.
150 */
151 public static boolean checkSimpleDerivationOk(XSSimpleType derived, XSTypeDefinition base, short block) {
152 // if derived is anySimpleType, then it's valid only if the base
153 // is ur-type
154 if (derived == SchemaGrammar.fAnySimpleType) {
155 return (base == SchemaGrammar.fAnyType ||
156 base == SchemaGrammar.fAnySimpleType);
157 }
158
159 // if base is complex type
160 if (base.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
161 // if base is anyType, change base to anySimpleType,
162 // otherwise, not valid
163 if (base == SchemaGrammar.fAnyType)
164 base = SchemaGrammar.fAnySimpleType;
165 else
166 return false;
167 }
168 return checkSimpleDerivation((XSSimpleType)derived,
169 (XSSimpleType)base, block);
170 }
171
172 /**
173 * check whether complex type derived is valid derived from base,
174 * given a subset of {restriction, extension}.
175 */
176 public static boolean checkComplexDerivationOk(XSComplexTypeDecl derived, XSTypeDefinition base, short block) {
177 // if derived is anyType, then it's valid only if base is anyType too
178 if (derived == SchemaGrammar.fAnyType)
179 return derived == base;
180 return checkComplexDerivation((XSComplexTypeDecl)derived, base, block);
181 }
182
183 /**
184 * Note: this will be a private method, and it assumes that derived is not
185 * anySimpleType, and base is not anyType. Another method will be
186 * introduced for public use, which will call this method.
187 */
188 private static boolean checkSimpleDerivation(XSSimpleType derived, XSSimpleType base, short block) {
189 // 1 They are the same type definition.
190 if (derived == base)
191 return true;
192
193 // 2 All of the following must be true:
194 // 2.1 restriction is not in the subset, or in the {final} of its own {base type definition};
195 if ((block & XSConstants.DERIVATION_RESTRICTION) != 0 ||
196 (derived.getBaseType().getFinal() & XSConstants.DERIVATION_RESTRICTION) != 0) {
197 return false;
198 }
199
200 // 2.2 One of the following must be true:
1220 private static void checkNSRecurseCheckCardinality(List<XSParticleDecl> children, int min1, int max1,
1221 SubstitutionGroupHandler dSGHandler,
1222 XSParticleDecl wildcard, int min2, int max2,
1223 boolean checkWCOccurrence)
1224 throws XMLSchemaException {
1225
1226
1227 // check Occurrence ranges
1228 if (checkWCOccurrence && !checkOccurrenceRange(min1,max1,min2,max2)) {
1229 throw new XMLSchemaException("rcase-NSRecurseCheckCardinality.2", new Object[]{
1230 Integer.toString(min1),
1231 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1232 Integer.toString(min2),
1233 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1234 }
1235
1236 // Check that each member of the group is a valid restriction of the wildcard
1237 int count = children.size();
1238 try {
1239 for (int i = 0; i < count; i++) {
1240 XSParticleDecl particle1 = (XSParticleDecl)children.get(i);
1241 particleValidRestriction(particle1, dSGHandler, wildcard, null, false);
1242
1243 }
1244 }
1245 // REVISIT: should we really just ignore original cause of this error?
1246 // how can we report it?
1247 catch (XMLSchemaException e) {
1248 throw new XMLSchemaException("rcase-NSRecurseCheckCardinality.1", null);
1249 }
1250
1251 }
1252
1253 private static void checkRecurse(List<XSParticleDecl> dChildren, int min1, int max1,
1254 SubstitutionGroupHandler dSGHandler,
1255 List<XSParticleDecl> bChildren, int min2, int max2,
1256 SubstitutionGroupHandler bSGHandler)
1257 throws XMLSchemaException {
1258
1259 // check Occurrence ranges
1260 if (!checkOccurrenceRange(min1,max1,min2,max2)) {
1261 throw new XMLSchemaException("rcase-Recurse.1", new Object[]{
1262 Integer.toString(min1),
1263 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1264 Integer.toString(min2),
1265 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1266 }
1267
1268 int count1= dChildren.size();
1269 int count2= bChildren.size();
1270
1271 int current = 0;
1272 label: for (int i = 0; i<count1; i++) {
1273
1274 XSParticleDecl particle1 = (XSParticleDecl)dChildren.get(i);
1275 for (int j = current; j<count2; j++) {
1276 XSParticleDecl particle2 = (XSParticleDecl)bChildren.get(j);
1277 current +=1;
1278 try {
1279 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler);
1280 continue label;
1281 }
1282 catch (XMLSchemaException e) {
1283 if (!particle2.emptiable())
1284 throw new XMLSchemaException("rcase-Recurse.2", null);
1285 }
1286 }
1287 throw new XMLSchemaException("rcase-Recurse.2", null);
1288 }
1289
1290 // Now, see if there are some elements in the base we didn't match up
1291 for (int j=current; j < count2; j++) {
1292 XSParticleDecl particle2 = (XSParticleDecl)bChildren.get(j);
1293 if (!particle2.emptiable()) {
1294 throw new XMLSchemaException("rcase-Recurse.2", null);
1295 }
1296 }
1297
1298 }
1299
1300 private static void checkRecurseUnordered(List<XSParticleDecl> dChildren, int min1, int max1,
1301 SubstitutionGroupHandler dSGHandler,
1302 List<XSParticleDecl> bChildren, int min2, int max2,
1303 SubstitutionGroupHandler bSGHandler)
1304 throws XMLSchemaException {
1305
1306
1307 // check Occurrence ranges
1308 if (!checkOccurrenceRange(min1,max1,min2,max2)) {
1309 throw new XMLSchemaException("rcase-RecurseUnordered.1", new Object[]{
1310 Integer.toString(min1),
1311 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1312 Integer.toString(min2),
1313 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1314 }
1315
1316 int count1= dChildren.size();
1317 int count2 = bChildren.size();
1318
1319 boolean foundIt[] = new boolean[count2];
1320
1321 label: for (int i = 0; i<count1; i++) {
1322 XSParticleDecl particle1 = (XSParticleDecl)dChildren.get(i);
1323
1324 for (int j = 0; j<count2; j++) {
1325 XSParticleDecl particle2 = (XSParticleDecl)bChildren.get(j);
1326 try {
1327 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler);
1328 if (foundIt[j])
1329 throw new XMLSchemaException("rcase-RecurseUnordered.2", null);
1330 else
1331 foundIt[j]=true;
1332
1333 continue label;
1334 }
1335 catch (XMLSchemaException e) {
1336 }
1337 }
1338 // didn't find a match. Detect an error
1339 throw new XMLSchemaException("rcase-RecurseUnordered.2", null);
1340 }
1341
1342 // Now, see if there are some elements in the base we didn't match up
1343 for (int j=0; j < count2; j++) {
1344 XSParticleDecl particle2 = (XSParticleDecl)bChildren.get(j);
1345 if (!foundIt[j] && !particle2.emptiable()) {
1346 throw new XMLSchemaException("rcase-RecurseUnordered.2", null);
1347 }
1348 }
1349
1350 }
1351
1352 private static void checkRecurseLax(List<XSParticleDecl> dChildren, int min1, int max1,
1353 SubstitutionGroupHandler dSGHandler,
1354 List<XSParticleDecl> bChildren, int min2, int max2,
1355 SubstitutionGroupHandler bSGHandler)
1356 throws XMLSchemaException {
1357
1358 // check Occurrence ranges
1359 if (!checkOccurrenceRange(min1,max1,min2,max2)) {
1360 throw new XMLSchemaException("rcase-RecurseLax.1", new Object[]{
1361 Integer.toString(min1),
1362 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1363 Integer.toString(min2),
1364 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1365 }
1366
1367 int count1= dChildren.size();
1368 int count2 = bChildren.size();
1369
1370 int current = 0;
1371 label: for (int i = 0; i<count1; i++) {
1372
1373 XSParticleDecl particle1 = (XSParticleDecl)dChildren.get(i);
1374 for (int j = current; j<count2; j++) {
1375 XSParticleDecl particle2 = (XSParticleDecl)bChildren.get(j);
1376 current +=1;
1377 try {
1378 // IHR: go back one element on b list because the next element may match
1379 // this as well.
1380 if (particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler))
1381 current--;
1382 continue label;
1383 }
1384 catch (XMLSchemaException e) {
1385 }
1386 }
1387 // didn't find a match. Detect an error
1388 throw new XMLSchemaException("rcase-RecurseLax.2", null);
1389
1390 }
1391
1392 }
1393
1394 private static void checkMapAndSum(List<XSParticleDecl> dChildren, int min1, int max1,
1395 SubstitutionGroupHandler dSGHandler,
1408 //
1409 // <sequence>
1410 // <b/>
1411 // <a/>
1412 // </sequence>
1413
1414 // check Occurrence ranges
1415 if (!checkOccurrenceRange(min1,max1,min2,max2)) {
1416 throw new XMLSchemaException("rcase-MapAndSum.2",
1417 new Object[]{Integer.toString(min1),
1418 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1419 Integer.toString(min2),
1420 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1421 }
1422
1423 int count1 = dChildren.size();
1424 int count2 = bChildren.size();
1425
1426 label: for (int i = 0; i<count1; i++) {
1427
1428 XSParticleDecl particle1 = (XSParticleDecl)dChildren.get(i);
1429 for (int j = 0; j<count2; j++) {
1430 XSParticleDecl particle2 = (XSParticleDecl)bChildren.get(j);
1431 try {
1432 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler);
1433 continue label;
1434 }
1435 catch (XMLSchemaException e) {
1436 }
1437 }
1438 // didn't find a match. Detect an error
1439 throw new XMLSchemaException("rcase-MapAndSum.1", null);
1440 }
1441 }
1442 // to check whether two element overlap, as defined in constraint UPA
1443 public static boolean overlapUPA(XSElementDecl element1,
1444 XSElementDecl element2,
1445 SubstitutionGroupHandler sgHandler) {
1446 // if the two element have the same name and namespace,
1447 if (element1.fName == element2.fName &&
1448 element1.fTargetNamespace == element2.fTargetNamespace) {
1449 return true;
1450 }
|
1 /*
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3 * @LastModified: Nov 2017
4 */
5 /*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22 package com.sun.org.apache.xerces.internal.impl.xs;
23
148 * check whether simple type derived is valid derived from base,
149 * given a subset of {restriction, extension}.
150 */
151 public static boolean checkSimpleDerivationOk(XSSimpleType derived, XSTypeDefinition base, short block) {
152 // if derived is anySimpleType, then it's valid only if the base
153 // is ur-type
154 if (derived == SchemaGrammar.fAnySimpleType) {
155 return (base == SchemaGrammar.fAnyType ||
156 base == SchemaGrammar.fAnySimpleType);
157 }
158
159 // if base is complex type
160 if (base.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
161 // if base is anyType, change base to anySimpleType,
162 // otherwise, not valid
163 if (base == SchemaGrammar.fAnyType)
164 base = SchemaGrammar.fAnySimpleType;
165 else
166 return false;
167 }
168 return checkSimpleDerivation(derived, (XSSimpleType)base, block);
169 }
170
171 /**
172 * check whether complex type derived is valid derived from base,
173 * given a subset of {restriction, extension}.
174 */
175 public static boolean checkComplexDerivationOk(XSComplexTypeDecl derived, XSTypeDefinition base, short block) {
176 // if derived is anyType, then it's valid only if base is anyType too
177 if (derived == SchemaGrammar.fAnyType)
178 return derived == base;
179 return checkComplexDerivation(derived, base, block);
180 }
181
182 /**
183 * Note: this will be a private method, and it assumes that derived is not
184 * anySimpleType, and base is not anyType. Another method will be
185 * introduced for public use, which will call this method.
186 */
187 private static boolean checkSimpleDerivation(XSSimpleType derived, XSSimpleType base, short block) {
188 // 1 They are the same type definition.
189 if (derived == base)
190 return true;
191
192 // 2 All of the following must be true:
193 // 2.1 restriction is not in the subset, or in the {final} of its own {base type definition};
194 if ((block & XSConstants.DERIVATION_RESTRICTION) != 0 ||
195 (derived.getBaseType().getFinal() & XSConstants.DERIVATION_RESTRICTION) != 0) {
196 return false;
197 }
198
199 // 2.2 One of the following must be true:
1219 private static void checkNSRecurseCheckCardinality(List<XSParticleDecl> children, int min1, int max1,
1220 SubstitutionGroupHandler dSGHandler,
1221 XSParticleDecl wildcard, int min2, int max2,
1222 boolean checkWCOccurrence)
1223 throws XMLSchemaException {
1224
1225
1226 // check Occurrence ranges
1227 if (checkWCOccurrence && !checkOccurrenceRange(min1,max1,min2,max2)) {
1228 throw new XMLSchemaException("rcase-NSRecurseCheckCardinality.2", new Object[]{
1229 Integer.toString(min1),
1230 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1231 Integer.toString(min2),
1232 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1233 }
1234
1235 // Check that each member of the group is a valid restriction of the wildcard
1236 int count = children.size();
1237 try {
1238 for (int i = 0; i < count; i++) {
1239 XSParticleDecl particle1 = children.get(i);
1240 particleValidRestriction(particle1, dSGHandler, wildcard, null, false);
1241
1242 }
1243 }
1244 // REVISIT: should we really just ignore original cause of this error?
1245 // how can we report it?
1246 catch (XMLSchemaException e) {
1247 throw new XMLSchemaException("rcase-NSRecurseCheckCardinality.1", null);
1248 }
1249
1250 }
1251
1252 private static void checkRecurse(List<XSParticleDecl> dChildren, int min1, int max1,
1253 SubstitutionGroupHandler dSGHandler,
1254 List<XSParticleDecl> bChildren, int min2, int max2,
1255 SubstitutionGroupHandler bSGHandler)
1256 throws XMLSchemaException {
1257
1258 // check Occurrence ranges
1259 if (!checkOccurrenceRange(min1,max1,min2,max2)) {
1260 throw new XMLSchemaException("rcase-Recurse.1", new Object[]{
1261 Integer.toString(min1),
1262 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1263 Integer.toString(min2),
1264 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1265 }
1266
1267 int count1= dChildren.size();
1268 int count2= bChildren.size();
1269
1270 int current = 0;
1271 label: for (int i = 0; i<count1; i++) {
1272
1273 XSParticleDecl particle1 = dChildren.get(i);
1274 for (int j = current; j<count2; j++) {
1275 XSParticleDecl particle2 = bChildren.get(j);
1276 current +=1;
1277 try {
1278 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler);
1279 continue label;
1280 }
1281 catch (XMLSchemaException e) {
1282 if (!particle2.emptiable())
1283 throw new XMLSchemaException("rcase-Recurse.2", null);
1284 }
1285 }
1286 throw new XMLSchemaException("rcase-Recurse.2", null);
1287 }
1288
1289 // Now, see if there are some elements in the base we didn't match up
1290 for (int j=current; j < count2; j++) {
1291 XSParticleDecl particle2 = bChildren.get(j);
1292 if (!particle2.emptiable()) {
1293 throw new XMLSchemaException("rcase-Recurse.2", null);
1294 }
1295 }
1296
1297 }
1298
1299 private static void checkRecurseUnordered(List<XSParticleDecl> dChildren, int min1, int max1,
1300 SubstitutionGroupHandler dSGHandler,
1301 List<XSParticleDecl> bChildren, int min2, int max2,
1302 SubstitutionGroupHandler bSGHandler)
1303 throws XMLSchemaException {
1304
1305
1306 // check Occurrence ranges
1307 if (!checkOccurrenceRange(min1,max1,min2,max2)) {
1308 throw new XMLSchemaException("rcase-RecurseUnordered.1", new Object[]{
1309 Integer.toString(min1),
1310 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1311 Integer.toString(min2),
1312 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1313 }
1314
1315 int count1= dChildren.size();
1316 int count2 = bChildren.size();
1317
1318 boolean foundIt[] = new boolean[count2];
1319
1320 label: for (int i = 0; i<count1; i++) {
1321 XSParticleDecl particle1 = dChildren.get(i);
1322
1323 for (int j = 0; j<count2; j++) {
1324 XSParticleDecl particle2 = bChildren.get(j);
1325 try {
1326 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler);
1327 if (foundIt[j])
1328 throw new XMLSchemaException("rcase-RecurseUnordered.2", null);
1329 else
1330 foundIt[j]=true;
1331
1332 continue label;
1333 }
1334 catch (XMLSchemaException e) {
1335 }
1336 }
1337 // didn't find a match. Detect an error
1338 throw new XMLSchemaException("rcase-RecurseUnordered.2", null);
1339 }
1340
1341 // Now, see if there are some elements in the base we didn't match up
1342 for (int j=0; j < count2; j++) {
1343 XSParticleDecl particle2 = bChildren.get(j);
1344 if (!foundIt[j] && !particle2.emptiable()) {
1345 throw new XMLSchemaException("rcase-RecurseUnordered.2", null);
1346 }
1347 }
1348
1349 }
1350
1351 private static void checkRecurseLax(List<XSParticleDecl> dChildren, int min1, int max1,
1352 SubstitutionGroupHandler dSGHandler,
1353 List<XSParticleDecl> bChildren, int min2, int max2,
1354 SubstitutionGroupHandler bSGHandler)
1355 throws XMLSchemaException {
1356
1357 // check Occurrence ranges
1358 if (!checkOccurrenceRange(min1,max1,min2,max2)) {
1359 throw new XMLSchemaException("rcase-RecurseLax.1", new Object[]{
1360 Integer.toString(min1),
1361 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1362 Integer.toString(min2),
1363 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1364 }
1365
1366 int count1= dChildren.size();
1367 int count2 = bChildren.size();
1368
1369 int current = 0;
1370 label: for (int i = 0; i<count1; i++) {
1371
1372 XSParticleDecl particle1 = dChildren.get(i);
1373 for (int j = current; j<count2; j++) {
1374 XSParticleDecl particle2 = bChildren.get(j);
1375 current +=1;
1376 try {
1377 // IHR: go back one element on b list because the next element may match
1378 // this as well.
1379 if (particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler))
1380 current--;
1381 continue label;
1382 }
1383 catch (XMLSchemaException e) {
1384 }
1385 }
1386 // didn't find a match. Detect an error
1387 throw new XMLSchemaException("rcase-RecurseLax.2", null);
1388
1389 }
1390
1391 }
1392
1393 private static void checkMapAndSum(List<XSParticleDecl> dChildren, int min1, int max1,
1394 SubstitutionGroupHandler dSGHandler,
1407 //
1408 // <sequence>
1409 // <b/>
1410 // <a/>
1411 // </sequence>
1412
1413 // check Occurrence ranges
1414 if (!checkOccurrenceRange(min1,max1,min2,max2)) {
1415 throw new XMLSchemaException("rcase-MapAndSum.2",
1416 new Object[]{Integer.toString(min1),
1417 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1),
1418 Integer.toString(min2),
1419 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)});
1420 }
1421
1422 int count1 = dChildren.size();
1423 int count2 = bChildren.size();
1424
1425 label: for (int i = 0; i<count1; i++) {
1426
1427 XSParticleDecl particle1 = dChildren.get(i);
1428 for (int j = 0; j<count2; j++) {
1429 XSParticleDecl particle2 = bChildren.get(j);
1430 try {
1431 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler);
1432 continue label;
1433 }
1434 catch (XMLSchemaException e) {
1435 }
1436 }
1437 // didn't find a match. Detect an error
1438 throw new XMLSchemaException("rcase-MapAndSum.1", null);
1439 }
1440 }
1441 // to check whether two element overlap, as defined in constraint UPA
1442 public static boolean overlapUPA(XSElementDecl element1,
1443 XSElementDecl element2,
1444 SubstitutionGroupHandler sgHandler) {
1445 // if the two element have the same name and namespace,
1446 if (element1.fName == element2.fName &&
1447 element1.fTargetNamespace == element2.fTargetNamespace) {
1448 return true;
1449 }
|