schemacompiler.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,423 行 · 第 1/5 页
JAVA
1,423 行
/*
* 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 org.apache.axis2.schema;
import org.apache.axis2.namespace.Constants;
import org.apache.axis2.schema.i18n.SchemaCompilerMessages;
import org.apache.axis2.schema.util.SchemaPropertyLoader;
import org.apache.axis2.schema.writer.BeanWriter;
import org.apache.axis2.util.URLProcessor;
import org.apache.axis2.util.SchemaUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.commons.schema.*;
import javax.xml.namespace.QName;
import java.util.*;
/**
* Schema compiler for ADB. Based on WS-Commons schema object model.
*/
public class SchemaCompiler {
private static final Log log = LogFactory.getLog(SchemaCompiler.class);
private CompilerOptions options;
private HashMap processedTypemap;
//the list of processedElements for the outer elements
private HashMap processedElementMap;
private HashMap processedAnonymousComplexTypesMap;
//we need this map to keep the referenced elements. these elements need to be kept seperate
//to avoid conflicts
private HashMap processedElementRefMap;
private HashMap simpleTypesMap;
private HashMap changedTypeMap;
private HashSet changedSimpleTypeSet;
private HashSet changedComplexTypeSet;
private HashSet changedElementSet;
// this map is necessary to retain the metainformation of types. The reason why these
// meta info 'bags' would be useful later is to cater for the extensions and restrictions
// of types
private HashMap processedTypeMetaInfoMap;
//
private ArrayList processedElementList;
//a list of nillable elements - used to generate code
//for nillable elements
private List nillableElementList;
// writee reference
private BeanWriter writer = null;
private Map baseSchemaTypeMap = null;
//a map for keeping the already loaded schemas
//the key is the targetnamespace and the value is the schema object
private Map loadedSchemaMap = new HashMap();
// A map keeping the available schemas
//the key is the targetnamespace and the value is the schema object
//this map will be populated when multiple schemas
//are fed to the schema compiler!
private Map availableSchemaMap = new HashMap();
private Map loadedSourceURI = new HashMap();
// a list of externally identified QNames to be processed. This becomes
// useful when only a list of external elements need to be processed
public static final String ANY_ELEMENT_FIELD_NAME = "extraElement";
public static final String EXTRA_ATTRIBUTE_FIELD_NAME = "extraAttributes";
public static final String USE_OPTIONAL = "optional";
public static final String USE_REQUIRED = "required";
public static final String USE_NONE = "none";
/**
* @return the processes element map
* includes the Qname of the element as the key and a
* String representing the fully qualified class name
*/
public HashMap getProcessedElementMap() {
return processedElementMap;
}
/**
* @return a map of Qname vs models. A model can be anything,
* ranging from a DOM document to a stream. This is taken from the
* writer and the schema compiler has no control over it
*/
public Map getProcessedModelMap() {
return writer.getModelMap();
}
/**
* Constructor - Accepts a options bean
*
* @param options
*/
public SchemaCompiler(CompilerOptions options) throws SchemaCompilationException {
if (options == null) {
//create an empty options object
this.options = new CompilerOptions();
} else {
this.options = options;
}
//instantiate the maps
processedTypemap = new HashMap();
processedElementMap = new HashMap();
simpleTypesMap = new HashMap();
processedElementList = new ArrayList();
processedAnonymousComplexTypesMap = new HashMap();
changedTypeMap = new HashMap();
processedTypeMetaInfoMap = new HashMap();
processedElementRefMap = new HashMap();
nillableElementList = new ArrayList();
changedComplexTypeSet = new HashSet();
changedSimpleTypeSet = new HashSet();
changedElementSet = new HashSet();
//load the writer and initiliaze the base types
writer = SchemaPropertyLoader.getBeanWriterInstance();
writer.init(this.options);
//load the base types
baseSchemaTypeMap = SchemaPropertyLoader.getTypeMapperInstance().getTypeMap();
}
/**
* Compile a list of schemas
* This actually calls the compile (XmlSchema s) method repeatedly
*
* @param schemalist
* @throws SchemaCompilationException
* @see #compile(org.apache.ws.commons.schema.XmlSchema)
*/
public void compile(List schemalist) throws SchemaCompilationException {
try {
if (schemalist.isEmpty()) {
return;
}
//clear the loaded and available maps
loadedSchemaMap.clear();
availableSchemaMap.clear();
XmlSchema schema;
// first round - populate the avaialble map
for (int i = 0; i < schemalist.size(); i++) {
schema = (XmlSchema) schemalist.get(i);
availableSchemaMap.put(
schema.getTargetNamespace(),
schema);
}
//set a mapper package if not avaialable
if (writer.getExtensionMapperPackageName() == null) {
String nsp = null;
//get the first schema from the list and take that namespace as the
//mapper namespace
for (int i = 0; nsp == null && i < schemalist.size(); i++) {
nsp = ((XmlSchema) schemalist.get(i)).getTargetNamespace();
if (nsp != null)
break;
XmlSchema[] schemas = SchemaUtil.getAllSchemas((XmlSchema) schemalist.get(i));
for (int j = 0; schemas != null && j < schemas.length; j++) {
nsp = schemas[j].getTargetNamespace();
if (nsp != null)
break;
}
}
if (nsp == null) {
nsp = URLProcessor.DEFAULT_PACKAGE;
}
// if this name space exists in the ns2p list then we use it.
if ((options.getNs2PackageMap() != null)
&& (options.getNs2PackageMap().containsKey(nsp))) {
writer.registerExtensionMapperPackageName((String) options.getNs2PackageMap().get(nsp));
} else {
writer.registerExtensionMapperPackageName(URLProcessor.makePackageName(nsp));
}
}
// second round - call the schema compiler one by one
for (int i = 0; i < schemalist.size(); i++) {
compile((XmlSchema) schemalist.get(i), true);
}
//finish up
finalizeSchemaCompilation();
} catch (SchemaCompilationException e) {
throw e;
} catch (Exception e) {
throw new SchemaCompilationException(e);
}
}
/**
* Compile (rather codegen) a single schema element
*
* @param schema
* @throws SchemaCompilationException
*/
public void compile(XmlSchema schema) throws SchemaCompilationException {
compile(schema, false);
}
/**
* Compile (rather codegen) a single schema element
*
* @param schema
* @param isPartofGroup
* @throws SchemaCompilationException
*/
private void compile(XmlSchema schema, boolean isPartofGroup) throws SchemaCompilationException {
// some documents explicitly imports the schema of built in types. We don't actually need to compile
// the built-in types. So check the target namespace here and ignore it.
if (Constants.URI_2001_SCHEMA_XSD.equals(schema.getTargetNamespace())) {
return;
}
//register the package from this namespace as the mapper classes package
if (!isPartofGroup) {
//set a mapper package if not avaialable
if (writer.getExtensionMapperPackageName() == null) {
writer.registerExtensionMapperPackageName(
URLProcessor.makePackageName(schema.getTargetNamespace()));
}
}
//First look for the schemas that are imported and process them
//Note that these are processed recursively!
//add the schema to the loaded schema list
if (!loadedSchemaMap.containsKey(schema.getTargetNamespace())) {
loadedSchemaMap.put(schema.getTargetNamespace(), schema);
}
// If we have/are loading a schema with a specific targetnamespace from a certain URI,
// then just return back to the caller to avoid recursion.
if (schema.getSourceURI() != null) {
String key = schema.getTargetNamespace() + ":" + schema.getSourceURI();
if (loadedSourceURI.containsKey(key)) {
return;
}
loadedSourceURI.put(key, key);
}
XmlSchemaObjectCollection includes = schema.getIncludes();
if (includes != null) {
Iterator tempIterator = includes.getIterator();
while (tempIterator.hasNext()) {
Object o = tempIterator.next();
if (o instanceof XmlSchemaImport) {
XmlSchema schema1 = ((XmlSchemaImport) o).getSchema();
if (schema1 != null) compile(schema1, isPartofGroup);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?