schemaparserutils.java

来自「一个完整的XACML工程,学习XACML技术的好例子!」· Java 代码 · 共 89 行

JAVA
89 行
字号
/*
 * SchemaParserUtils.java
 *
 * Created on 10 December 2002, 14:26
 */

package ke.defaultimpl.utils.xml.parsing;

import org.apache.commons.digester.*;

import org.xml.sax.*;

import java.util.*;
import java.io.*;

/**
 *
 * @author  James Cunningham
 */
public class SchemaParserUtils
{
    /**
     * @param schemaFile
     * @return
     * @throws IOException
     * @throws SAXException
     * @throws IncorrectSchemaException
     */
    
    public static SchemaBean getAttributesFromSchema(String schemaFile) throws IOException, SAXException, IncorrectSchemaException
    {
        File file = null;
        if(!(file = new File(schemaFile)).exists())
            throw new IOException("The specified schema file [" + schemaFile + "] could not be found");
        if(dg == null) init();
        HashMap nameTypeMapping = new HashMap(15);
        dg.push(nameTypeMapping);
        dg.parse(file);
        SchemaBean schemaBean = new SchemaBean();
        if(nameTypeMapping.get("Schema_Name") != null)
        {
            schemaBean.setSchemaName((String)nameTypeMapping.get("Schema_Name"));
            Set names = nameTypeMapping.keySet();
            String[] nameArray = new String[names.size()-1]; // We don't want to include the schemaName mapping
            String[] typeArray  = new String[names.size()-1];
            int index = 0;
            for(Iterator i = names.iterator(); i.hasNext();)
            {
                String name = (String)i.next();
                if(!name.equals("Schema_Name"))
                {
                    nameArray[index] = name;
                    typeArray[index] = (String)nameTypeMapping.get(name);
                    index++;
                }
            }
            schemaBean.setAttributeName(nameArray);
            schemaBean.setAttributeType(typeArray);
        }
        else
        {
            //clean up as usual
            dg.clear();
            nameTypeMapping = null;
            throw new IncorrectSchemaException("Error: Schema type not valid");
        }
        dg.clear();
        nameTypeMapping = null;
        return schemaBean;
    }

    /**
     * 
     */
    
    private static void init() {
        dg = new Digester();
        dg.setValidating(false);
        dg.addRule("xsd:schema/xsd:element", new MapSchemaNameRule());
        dg.addRule("xsd:schema/xsd:element/xsd:complexType/xsd:sequence/xsd:element", new MapNameTypeRule());
        dg.addRule("xsd:schema/xsd:element/xsd:simpleType", new CreatePlaceHolderRule("simpleType"));
        dg.addRule("xsd:schema/xsd:element/xsd:simpleType/xsd:list", new MapArrayTypeRule());
    }

//*********************************************************************

    private static Digester dg = null;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?