cstructwriter.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,100 行 · 第 1/3 页
JAVA
1,100 行
/*
* 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.writer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.axis2.schema.typemap.JavaTypeMap;
import org.apache.axis2.schema.CompilerOptions;
import org.apache.axis2.schema.SchemaCompilationException;
import org.apache.axis2.schema.BeanWriterMetaInfoHolder;
import org.apache.axis2.schema.SchemaCompiler;
import org.apache.axis2.schema.util.SchemaPropertyLoader;
import org.apache.axis2.schema.util.PrimitiveTypeFinder;
import org.apache.axis2.schema.util.PrimitiveTypeWrapper;
import org.apache.axis2.schema.i18n.SchemaCompilerMessages;
import org.apache.axis2.util.XSLTUtils;
import org.apache.axis2.util.JavaUtils;
import org.apache.axis2.util.XSLTTemplateProcessor;
import org.apache.axis2.wsdl.databinding.CUtils;
import org.apache.ws.commons.schema.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaSimpleType;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.namespace.QName;
import java.util.*;
import java.io.*;
import com.ibm.wsdl.util.xml.DOM2Writer;
/**
* Java Bean writer for the schema compiler.
*/
public class CStructWriter implements BeanWriter {
public static final String WRAPPED_DATABINDING_CLASS_NAME = "WrappedDatabinder";
public static final String AXIS2_PREFIX = "adb_";
private String javaBeanTemplateName = null;
private boolean templateLoaded = false;
private Templates sourceTemplateCache;
private Templates headerTemplateCache;
private List namesList;
private static int count = 0;
private boolean wrapClasses = false;
private boolean writeClasses = false;
protected File rootDir;
private Document globalWrappedSourceDocument;
private Document globalWrappedHeaderDocument;
private Map modelMap = new HashMap();
private static final String DEFAULT_PACKAGE = "adb";
private static final String DEFAULT_C_CLASS_NAME = "axiom_node_t*";
private Map baseTypeMap = new JavaTypeMap().getTypeMap();
// 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 DEFAULT_CLASS_NAME = "axiom_node_t*";
public static final String DEFAULT_CLASS_ARRAY_NAME = "axis2_array_list_t";
public static final String DEFAULT_ATTRIB_CLASS_NAME = "axiom_attribute_t*";
public static final String DEFAULT_ATTRIB_ARRAY_CLASS_NAME = "axis2_array_list_t";
/**
* Default constructor
*/
public CStructWriter() {
}
/**
* This returns a map of Qnames vs DOMDocument models. One can use this method to
* obtain the raw DOMmodels used to write the classes.
* This has no meaning when the classes are supposed to be wrapped so the
*
* @return Returns Map.
* @see BeanWriter#getModelMap()
*/
public Map getModelMap() {
return modelMap;
}
public String getDefaultClassName() {
return DEFAULT_CLASS_NAME;
}
public String getDefaultClassArrayName() {
return DEFAULT_CLASS_ARRAY_NAME;
}
public String getDefaultAttribClassName() {
return DEFAULT_ATTRIB_CLASS_NAME;
}
public String getDefaultAttribArrayClassName() {
return DEFAULT_ATTRIB_ARRAY_CLASS_NAME;
}
public void init(CompilerOptions options) throws SchemaCompilationException {
try {
initWithFile(options.getOutputLocation());
writeClasses = options.isWriteOutput();
if (!writeClasses) {
wrapClasses = false;
} else {
wrapClasses = options.isWrapClasses();
}
//if the wrap mode is set then create a global document to keep the wrapped
//element models
if (options.isWrapClasses()) {
globalWrappedSourceDocument = XSLTUtils.getDocument();
Element rootElement = XSLTUtils.getElement(globalWrappedSourceDocument, "beans");
globalWrappedSourceDocument.appendChild(rootElement);
XSLTUtils.addAttribute(globalWrappedSourceDocument, "name", CStructWriter.WRAPPED_DATABINDING_CLASS_NAME, rootElement);
globalWrappedHeaderDocument = XSLTUtils.getDocument();
rootElement = XSLTUtils.getElement(globalWrappedHeaderDocument, "beans");
globalWrappedHeaderDocument.appendChild(rootElement);
XSLTUtils.addAttribute(globalWrappedHeaderDocument, "name", CStructWriter.WRAPPED_DATABINDING_CLASS_NAME, rootElement);
}
} catch (IOException e) {
throw new SchemaCompilationException(e);
} catch (ParserConfigurationException e) {
throw new SchemaCompilationException(e); //todo need to put correct error messages
}
}
/**
* @param element
* @param typeMap
* @param metainf
* @return Returns String.
* @throws org.apache.axis2.schema.SchemaCompilationException
*
*/
public String write(XmlSchemaElement element, Map typeMap, BeanWriterMetaInfoHolder metainf) throws SchemaCompilationException {
try {
QName qName = element.getQName();
return process(qName, metainf, typeMap, true);
} catch (Exception e) {
throw new SchemaCompilationException(e);
}
}
/**
* @param qName
* @param typeMap
* @param metainf
* @param isAbstract
* @throws org.apache.axis2.schema.SchemaCompilationException
*
* @see org.apache.axis2.schema.writer.BeanWriter
*/
public String write(QName qName,
Map typeMap,
BeanWriterMetaInfoHolder metainf,
boolean isAbstract)
throws SchemaCompilationException {
try {
//determine the package for this type.
return process(qName, metainf, typeMap, false);
} catch (SchemaCompilationException e) {
throw e;
} catch (Exception e) {
throw new SchemaCompilationException(e);
}
}
/**
* @throws SchemaCompilationException
* @see org.apache.axis2.schema.writer.BeanWriter#writeBatch()
*/
public void writeBatch() throws SchemaCompilationException {
try {
if (wrapClasses) {
File outSource = createOutFile(CStructWriter.WRAPPED_DATABINDING_CLASS_NAME, ".c");
File outHeader = createOutFile(CStructWriter.WRAPPED_DATABINDING_CLASS_NAME, ".h");
//parse with the template and create the files
parseSource(globalWrappedSourceDocument, outSource);
parseHeader(globalWrappedHeaderDocument, outHeader);
}
} catch (Exception e) {
throw new SchemaCompilationException(e);
}
}
/**
* @param simpleType
* @param typeMap
* @param metainf
* @return Returns String.
* @throws org.apache.axis2.schema.SchemaCompilationException
*
* @see BeanWriter#write(org.apache.ws.commons.schema.XmlSchemaSimpleType, java.util.Map, org.apache.axis2.schema.BeanWriterMetaInfoHolder)
*/
public String write(XmlSchemaSimpleType simpleType, Map typeMap, BeanWriterMetaInfoHolder metainf) throws SchemaCompilationException {
try {
//determine the package for this type.
QName qName = simpleType.getQName();
return process(qName, metainf, typeMap, false);
} catch (SchemaCompilationException e) {
throw e;
} catch (Exception e) {
throw new SchemaCompilationException(e);
}
}
/**
* @param rootDir
* @throws java.io.IOException
* @see org.apache.axis2.schema.writer.BeanWriter
*/
private void initWithFile(File rootDir) throws IOException {
if (rootDir == null) {
this.rootDir = new File(".");
} else if (!rootDir.isDirectory()) {
throw new IOException(SchemaCompilerMessages.getMessage("schema.rootnotfolderexception"));
} else {
this.rootDir = rootDir;
}
namesList = new ArrayList();
javaBeanTemplateName = SchemaPropertyLoader.getBeanTemplate();
}
/**
* Make the fully qualified class name for an element or named type
*
* @param qName the qualified Name for this element or type in the schema
* @return the appropriate fully qualified class name to use in generated code
*/
public String makeFullyQualifiedClassName(QName qName) {
String originalName = qName.getLocalPart();
return makeUniqueCStructName(this.namesList, originalName);
}
/**
* A util method that holds common code
* for the complete schema that the generated XML complies to
* look under other/beanGenerationSchema.xsd
*
* @param qName
* @param metainf
* @param typeMap
* @param isElement
* @return Returns String.
* @throws Exception
*/
private String process(QName qName, BeanWriterMetaInfoHolder metainf, Map typeMap, boolean isElement) throws Exception {
String fullyQualifiedClassName = metainf.getOwnClassName();
if (fullyQualifiedClassName == null)
fullyQualifiedClassName = makeFullyQualifiedClassName(qName);
String className = fullyQualifiedClassName;
String originalName = qName.getLocalPart();
ArrayList propertyNames = new ArrayList();
if (!templateLoaded) {
loadTemplate();
}
//if wrapped then do not write the classes now but add the models to a global document. However in order to write the
//global class that is generated, one needs to call the writeBatch() method
if (wrapClasses) {
globalWrappedSourceDocument.getDocumentElement().appendChild(
getBeanElement(globalWrappedSourceDocument, className, originalName, qName, isElement, metainf, propertyNames, typeMap)
);
globalWrappedHeaderDocument.getDocumentElement().appendChild(
getBeanElement(globalWrappedHeaderDocument, className, originalName, qName, isElement, metainf, propertyNames, typeMap)
);
} else {
//create the model
Document modelSource = XSLTUtils.getDocument();
Document modelHeader = XSLTUtils.getDocument();
//make the XML
modelSource.appendChild(getBeanElement(modelSource, className, originalName, qName, isElement, metainf, propertyNames, typeMap));
modelHeader.appendChild(getBeanElement(modelHeader, className, originalName, qName, isElement, metainf, propertyNames, typeMap));
if (writeClasses) {
//create the file
File outSource = createOutFile(className, ".c");
File outHeader = createOutFile(className, ".h");
//parse with the template and create the files
parseSource(modelSource, outSource);
parseHeader(modelHeader, outHeader);
}
//add the model to the model map
modelMap.put(
new QName(qName.getNamespaceURI(), className)
, modelSource);
modelMap.put(
new QName(qName.getNamespaceURI(), className)
, modelHeader);
}
//return the fully qualified class name
return fullyQualifiedClassName;
}
/**
* @param model
* @param className
* @param originalName
* @param qName
* @param isElement
* @param metainf
* @param propertyNames
* @param typeMap
* @return Returns Element.
* @throws org.apache.axis2.schema.SchemaCompilationException
*
*/
private Element getBeanElement(
Document model, String className, String originalName,
QName qName, boolean isElement,
BeanWriterMetaInfoHolder metainf, ArrayList propertyNames, Map typeMap
) throws SchemaCompilationException {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?