|
|
|
|
|
*/ |
|
/* |
|
* 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.xs; |
|
|
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class XSGrammarBucket { |
|
|
|
// Data |
|
|
|
|
|
|
|
*/ |
|
Map<String, SchemaGrammar> fGrammarRegistry = new HashMap<>(); |
|
SchemaGrammar fNoNSGrammar = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SchemaGrammar getGrammar(String namespace) { |
|
if (namespace == null) |
|
return fNoNSGrammar; |
|
return fGrammarRegistry.get(namespace); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void putGrammar(SchemaGrammar grammar) { |
|
if (grammar.getTargetNamespace() == null) |
|
fNoNSGrammar = grammar; |
|
else |
|
fGrammarRegistry.put(grammar.getTargetNamespace(), grammar); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean putGrammar(SchemaGrammar grammar, boolean deep) { |
|
|
|
SchemaGrammar sg = getGrammar(grammar.fTargetNamespace); |
|
if (sg != null) { |
|
|
|
return sg == grammar; |
|
} |
|
|
|
if (!deep) { |
|
putGrammar(grammar); |
|
return true; |
|
} |
|
|
|
// get all imported grammars, and make a copy of the Vector, so that |
|
// we can recursively process the grammars, and add distinct ones |
|
|
|
ArrayList<SchemaGrammar> currGrammars = (ArrayList<SchemaGrammar>)grammar.getImportedGrammars(); |
|
if (currGrammars == null) { |
|
putGrammar(grammar); |
|
return true; |
|
} |
|
|
|
@SuppressWarnings("unchecked") |
|
List<SchemaGrammar> grammars = ((ArrayList<SchemaGrammar>)currGrammars.clone()); |
|
SchemaGrammar sg1, sg2; |
|
List<SchemaGrammar> gs; |
|
|
|
for (int i = 0; i < grammars.size(); i++) { |
|
|
|
sg1 = grammars.get(i); |
|
|
|
sg2 = getGrammar(sg1.fTargetNamespace); |
|
if (sg2 == null) { |
|
|
|
gs = sg1.getImportedGrammars(); |
|
// for all grammars imported by sg2, but not in the vector |
|
|
|
if(gs == null) continue; |
|
for (int j = gs.size() - 1; j >= 0; j--) { |
|
sg2 = gs.get(j); |
|
if (!grammars.contains(sg2)) |
|
grammars.add(sg2); |
|
} |
|
} |
|
// we found one with the same target namespace |
|
|
|
else if (sg2 != sg1) { |
|
return false; |
|
} |
|
} |
|
|
|
|
|
putGrammar(grammar); |
|
for (int i = grammars.size() - 1; i >= 0; i--) |
|
putGrammar(grammars.get(i)); |
|
|
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean putGrammar(SchemaGrammar grammar, boolean deep, boolean ignoreConflict) { |
|
if (!ignoreConflict) { |
|
return putGrammar(grammar, deep); |
|
} |
|
|
|
|
|
SchemaGrammar sg = getGrammar(grammar.fTargetNamespace); |
|
if (sg == null) { |
|
putGrammar(grammar); |
|
} |
|
|
|
|
|
if (!deep) { |
|
return true; |
|
} |
|
|
|
// get all imported grammars, and make a copy of the Vector, so that |
|
// we can recursively process the grammars, and add distinct ones |
|
|
|
ArrayList<SchemaGrammar> currGrammars = (ArrayList<SchemaGrammar>)grammar.getImportedGrammars(); |
|
if (currGrammars == null) { |
|
return true; |
|
} |
|
|
|
@SuppressWarnings("unchecked") |
|
List<SchemaGrammar> grammars = ((ArrayList<SchemaGrammar>)currGrammars.clone()); |
|
SchemaGrammar sg1, sg2; |
|
List<SchemaGrammar> gs; |
|
|
|
for (int i = 0; i < grammars.size(); i++) { |
|
|
|
sg1 = grammars.get(i); |
|
|
|
sg2 = getGrammar(sg1.fTargetNamespace); |
|
if (sg2 == null) { |
|
|
|
gs = sg1.getImportedGrammars(); |
|
// for all grammars imported by sg2, but not in the vector |
|
|
|
if(gs == null) continue; |
|
for (int j = gs.size() - 1; j >= 0; j--) { |
|
sg2 = gs.get(j); |
|
if (!grammars.contains(sg2)) |
|
grammars.add(sg2); |
|
} |
|
} |
|
|
|
else { |
|
grammars.remove(sg1); |
|
} |
|
} |
|
|
|
|
|
for (int i = grammars.size() - 1; i >= 0; i--) { |
|
putGrammar(grammars.get(i)); |
|
} |
|
|
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SchemaGrammar[] getGrammars() { |
|
|
|
int count = fGrammarRegistry.size() + (fNoNSGrammar==null ? 0 : 1); |
|
SchemaGrammar[] grammars = new SchemaGrammar[count]; |
|
|
|
int i = 0; |
|
for(Map.Entry<String, SchemaGrammar> entry : fGrammarRegistry.entrySet()){ |
|
grammars[i++] = entry.getValue(); |
|
} |
|
|
|
|
|
if (fNoNSGrammar != null) |
|
grammars[count-1] = fNoNSGrammar; |
|
return grammars; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void reset() { |
|
fNoNSGrammar = null; |
|
fGrammarRegistry.clear(); |
|
} |
|
|
|
} // class XSGrammarBucket |