
jlibs.xml.xsd.XSUtil Maven / Gradle / Ivy
/**
* Copyright 2015 Santhosh Kumar Tekuri
*
* The JLibs authors license 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 jlibs.xml.xsd;
import jlibs.core.graph.*;
import jlibs.core.graph.navigators.FilteredTreeNavigator;
import jlibs.core.graph.walkers.PreorderWalker;
import jlibs.xml.Namespaces;
import jlibs.xml.sax.helpers.MyNamespaceSupport;
import org.apache.xerces.xs.*;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author Santhosh Kumar T
*/
public class XSUtil{
public static MyNamespaceSupport createNamespaceSupport(XSModel model){
MyNamespaceSupport nsSupport = new MyNamespaceSupport();
StringList list = model.getNamespaces();
for(int i=0; i getSubTypes(XSModel xsModel, XSComplexTypeDefinition complexType){
List subTypes = new ArrayList();
XSNamedMap namedMap = xsModel.getComponents(XSConstants.TYPE_DEFINITION);
XSObject anyType = namedMap.itemByName(Namespaces.URI_XSD, "anyType");
for(int i=0; i guessRootElements(final XSModel model){
XSNamedMap components = model.getComponents(XSConstants.ELEMENT_DECLARATION);
if(components.getLength()==0)
return Collections.emptyList();
final List elements = new ArrayList(components.getLength());
for(int i=0; i(){
@Override
public boolean preProcess(Object elem, Path path){
if(path.getRecursionDepth()>0)
return false;
if(elements.size()>1 && path.getLength()>2){
XSElementDeclaration elemDecl = (XSElementDeclaration)elem;
if(elemDecl.getAbstract()){
XSObjectList substitutionGroup = model.getSubstitutionGroup(elemDecl);
for(int i=0; i getEnumeratedValues(XSSimpleTypeDefinition simpleType){
ArrayList enums = new ArrayList();
XSObjectList facets = simpleType.getMultiValueFacets();
if(facets!=null){
for(int i=0; i xpath){
XSNamespaceItem nsItem = XSUtil.getNamespaceItem(schema, xpath.get(0).getNamespaceURI());
if(nsItem==null)
return null;
FilteredTreeNavigator navigator = new FilteredTreeNavigator(new XSNavigator(), new Filter(){
@Override
public boolean select(Object elem){
return elem instanceof XSElementDeclaration;
}
});
final XSElementDeclaration declaration[] = new XSElementDeclaration[1];
Walker walker = new PreorderWalker(nsItem, navigator);
try{
WalkerUtil.walk(walker, new Processor(){
int i = 0;
@Override
public boolean preProcess(Object elem, Path path){
if(path.getParentPath()!=null){
QName qname = XSUtil.getQName((XSElementDeclaration)elem);
if(qname.equals(xpath.get(i))){
i++;
if(i==xpath.size()){
declaration[0] = (XSElementDeclaration)elem;
throw STOP_SEARCHING;
}
return true;
}else
return false;
}
return true;
}
@Override
public void postProcess(Object elem, Path path){}
});
}catch(RuntimeException ex){
if(ex!=STOP_SEARCHING)
throw ex;
}
return declaration[0];
}
@SuppressWarnings("unchecked")
public static XSAttributeDeclaration findAttributeDeclaration(XSModel schema, final List xpath){
XSNamespaceItem nsItem = XSUtil.getNamespaceItem(schema, xpath.get(0).getNamespaceURI());
if(nsItem==null)
return null;
FilteredTreeNavigator navigator = new FilteredTreeNavigator(new XSNavigator(), new Filter(){
@Override
public boolean select(Object elem){
return elem instanceof XSElementDeclaration || elem instanceof XSAttributeUse;
}
});
final XSAttributeDeclaration declaration[] = new XSAttributeDeclaration[1];
Walker walker = new PreorderWalker(nsItem, navigator);
try{
WalkerUtil.walk(walker, new Processor(){
int i = 0;
@SuppressWarnings("ConstantConditions")
@Override
public boolean preProcess(Object elem, Path path){
if(path.getParentPath()!=null){
QName qname = XSUtil.getQName(elem instanceof XSAttributeUse? ((XSAttributeUse)elem).getAttrDeclaration() : (XSObject)elem);
if(qname.equals(xpath.get(i)) && (i!=xpath.size()-1 || elem instanceof XSAttributeUse)){
i++;
if(i==xpath.size()){
declaration[0] = ((XSAttributeUse)elem).getAttrDeclaration();
throw STOP_SEARCHING;
}
return true;
}else
return false;
}
return true;
}
@Override
public void postProcess(Object elem, Path path){}
});
}catch(RuntimeException ex){
if(ex!=STOP_SEARCHING)
throw ex;
}
return declaration[0];
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy