Java源码示例:com.sun.xml.xsom.XSSchemaSet
示例1
private XSSchemaSet getSchemaSet()
throws IOException, SAXException {
if( schemaSet == null ) {
XSOMParser schemaParser = new XSOMParser();
ValueVector vec = getParameterVector( "schema" );
if( vec.size() > 0 ) {
for( Value v : vec ) {
schemaParser.parse( new File( v.strValue() ) );
}
}
parseWSDLTypes( schemaParser );
schemaSet = schemaParser.getResult();
String nsPrefix = "jolie";
int i = 1;
for( XSSchema schema : schemaSet.getSchemas() ) {
if( !schema.getTargetNamespace().equals( XMLConstants.W3C_XML_SCHEMA_NS_URI ) ) {
namespacePrefixMap.put( schema.getTargetNamespace(), nsPrefix + i++ );
}
}
}
return schemaSet;
}
示例2
private void groupProcessing(
Value value,
XSElementDecl xsDecl,
SOAPElement element,
SOAPEnvelope envelope,
boolean first,
XSModelGroup modelGroup,
XSSchemaSet sSet,
String messageNamespace )
throws SOAPException {
XSParticle[] children = modelGroup.getChildren();
XSTerm currTerm;
for( XSParticle child : children ) {
currTerm = child.getTerm();
if( currTerm.isModelGroup() ) {
groupProcessing( value, xsDecl, element, envelope, first, currTerm.asModelGroup(), sSet,
messageNamespace );
} else {
termProcessing( value, element, envelope, first, currTerm, child.getMaxOccurs(), sSet,
messageNamespace );
}
}
}
示例3
public Schema getSchema(String namespaceURI) {
Schema schema = schemas.get(namespaceURI);
if (schema != null)
return schema;
// CityGML 0.4.0
if ("http://www.citygml.org/citygml/1/0/0".equals(namespaceURI)) {
try {
parse(schemaLocations.get(namespaceURI));
} catch (SAXException e) {
//
}
}
XSSchemaSet schemaSet = getXSSchemaSet(namespaceURI);
if (schemaSet != null) {
schema = new Schema(schemaSet, namespaceURI, this);
schemas.put(namespaceURI, schema);
}
return schema;
}
示例4
private void termProcessing( Value value, SOAPElement element, SOAPEnvelope envelope, boolean first,
XSTerm currTerm, int getMaxOccur,
XSSchemaSet sSet, String messageNamespace )
throws SOAPException {
Value currValue = value.clone();
if( currTerm.isElementDecl() ) {
ValueVector vec;
XSElementDecl currElementDecl = currTerm.asElementDecl();
String name = currElementDecl.getName();
String prefix = (first) ? getPrefix( currElementDecl ) : getPrefixOrNull( currElementDecl );
SOAPElement childElement;
if( (vec = currValue.children().get( name )) != null ) {
int k = 0;
while( vec.size() > 0 && (getMaxOccur > k || getMaxOccur == XSParticle.UNBOUNDED) ) {
if( prefix == null ) {
childElement = element.addChildElement( name );
} else {
childElement = element.addChildElement( name, prefix );
}
Value v = vec.remove( 0 );
valueToTypedSOAP(
v,
currElementDecl,
childElement,
envelope,
false,
sSet,
messageNamespace );
k++;
}
}
}
}
示例5
private void convertTypes()
throws IOException {
Types types = definition.getTypes();
if( types != null ) {
List< ExtensibilityElement > list = types.getExtensibilityElements();
for( ExtensibilityElement element : list ) {
if( element instanceof SchemaImpl ) {
Element schemaElement = ((SchemaImpl) element).getElement();
// We need to inject the namespaces declared in parent nodes into the schema element
Map< String, String > namespaces = definition.getNamespaces();
for( Entry< String, String > entry : namespaces.entrySet() ) {
if( schemaElement.getAttribute( "xmlns:" + entry.getKey() ).isEmpty() ) {
schemaElement.setAttribute( "xmlns:" + entry.getKey(), entry.getValue() );
}
}
parseSchemaElement( schemaElement );
}
}
try {
XSSchemaSet schemaSet = schemaParser.getResult();
if( schemaSet == null ) {
throw new IOException( "An error occurred while parsing the WSDL types section" );
}
XsdToJolieConverter schemaConverter = new XsdToJolieConverterImpl( schemaSet, false, null );
typeDefinitions.addAll( schemaConverter.convert() );
} catch( SAXException | XsdToJolieConverter.ConversionException e ) {
throw new IOException( e );
}
}
}
示例6
public XSSchemaSet getResult() throws SAXException {
// run all the patchers
for (Patch patcher : patchers)
patcher.run();
patchers.clear();
// build the element substitutability map
Iterator itr = schemaSet.iterateElementDecls();
while(itr.hasNext())
((ElementDecl)itr.next()).updateSubstitutabilityMap();
if(hadError) return null;
else return schemaSet;
}
示例7
public void visit( XSSchemaSet s ) {
Iterator itr = s.iterateSchema();
while(itr.hasNext()) {
schema((XSSchema)itr.next());
println();
}
}
示例8
public XSSchemaSet parse(String resource) throws Exception {
final XSOMParser parser = new XSOMParser();
parser.setErrorHandler(null);
parser.setEntityResolver(null);
final URL resourceUrl = getClass().getClassLoader().getResource(
resource);
// parser.parseSchema(
//
// new File("myschema.xsd"));
// parser.parseSchema( new File("XHTML.xsd"));
parser.parse(resourceUrl);
XSSchemaSet sset = parser.getResult();
return sset;
}
示例9
public void visit( XSSchemaSet s ) {
Iterator<XSSchema> itr = s.iterateSchema();
while(itr.hasNext()) {
schema(itr.next());
println();
}
}
示例10
private XSSchemaSet getXSSchemaSet(String namespaceURI) {
for (XSSchemaSet schemaSet : schemaSets)
for (XSSchema schema : schemaSet.getSchemas())
if (schema.getTargetNamespace().equals(namespaceURI))
return schemaSet;
return null;
}
示例11
public Schema(XSSchemaSet schemaSet, String namespaceURI, SchemaHandler handler) {
this.schemaSet = schemaSet;
this.handler = handler;
this.namespaceURI = namespaceURI;
schema = schemaSet.getSchema(namespaceURI);
if (schema == null)
throw new IllegalStateException("no XSSchema associated with the namespace '" + namespaceURI + "'.");
uniqueElements = new HashMap<String, ElementDecl>();
multipleElements = new HashMap<String, List<ElementDecl>>();
localElements = new HashMap<ElementDecl, HashMap<String,ElementDecl>>();
}
示例12
public void visit( XSSchemaSet s ) {
Iterator<XSSchema> itr = s.iterateSchema();
while(itr.hasNext()) {
schema((XSSchema)itr.next());
println();
}
}
示例13
public static XmlForm build(XSSchemaSet set, String element) {
try {
log.debug("\n\n\n-------- BUILDING XML FORM --------");
XmlFormBuilder builder = new XmlFormBuilder();
Map<String, XmlForm> xmlForms = builder.parse(set);
XmlForm result = element == null ? getMainForm(xmlForms) : xmlForms.get(element);
log.debug("\n-----------------------------------\n\n");
return result;
} catch (Exception e) {
throw new RuntimeException("Failed to convert schema '" + set + "' into an XML form", e);
}
}
示例14
private Map<String, XmlForm> parse(XSSchemaSet set) {
Map<String, XmlForm> result = new HashMap<String, XmlForm>();
/*
* Accepts all element declarations in the Schema set
*/
for (Iterator<XSElementDecl> elements = set.iterateElementDecls(); elements.hasNext();) {
XSElementDecl decl = elements.next();
XmlForm xmlForm = decl.apply(this);
if (xmlForm != null)
result.put(decl.getName(), xmlForm);
}
return result;
}
示例15
public static void valueToDocument(
Value value,
Document document,
String schemaFilename ) throws IOException {
String rootName = value.children().keySet().iterator().next();
Value root = value.children().get( rootName ).get( 0 );
String rootNameSpace = "";
if( root.hasChildren( jolie.xml.XmlUtils.NAMESPACE_ATTRIBUTE_NAME ) ) {
rootNameSpace = root.getFirstChild( jolie.xml.XmlUtils.NAMESPACE_ATTRIBUTE_NAME ).strValue();
}
XSType type = null;
if( schemaFilename != null ) {
try {
XSOMParser parser = new XSOMParser();
parser.parse( schemaFilename );
XSSchemaSet schemaSet = parser.getResult();
if( schemaSet != null && schemaSet.getElementDecl( rootNameSpace, rootName ) != null ) {
type = schemaSet.getElementDecl( rootNameSpace, rootName ).getType();
} else if( schemaSet != null && schemaSet.getElementDecl( rootNameSpace, rootName ) == null ) {
Interpreter.getInstance().logWarning( "Root element " + rootName + " with namespace "
+ rootNameSpace + " not found in the schema " + schemaFilename );
}
} catch( SAXException e ) {
throw new IOException( e );
}
}
if( type == null ) {
valueToDocument(
value.getFirstChild( rootName ),
rootName,
document );
} else {
valueToDocument(
value.getFirstChild( rootName ),
rootName,
document,
type );
}
}
示例16
public XsdToJolieConverterImpl( XSSchemaSet schemaSet, boolean strict, Logger logger ) {
this.strict = strict;
this.schemaSet = schemaSet;
this.logger = logger;
}
示例17
public XSSchemaSet getRoot() {
if(ownerDocument==null)
return null;
else
return getOwnerSchema().getRoot();
}
示例18
/**
* Visits the root schema set.
*
* @param s Root schema set.
*/
public void visit(XSSchemaSet s) {
for (XSSchema schema : s.getSchemas()) {
schema(schema);
}
}
示例19
private XSSimpleType parse ( SchemaSimpleType schemaSimpleType ) {
ClassLoader cldr = this.getClass().getClassLoader();
InputStream resourceXMLSchema = cldr.getResourceAsStream( schemaSimpleType.getSchemaFilePath() );
File localXMLSchema = new File( schemaSimpleType.getTypeName() + ".xsd" );
try {
InputStream in = resourceXMLSchema;
// Overwrite the file.
OutputStream out = new FileOutputStream( localXMLSchema );
while (in.available() > 0) {
byte[] buf = new byte[1024];
int len;
while ((len = in.read( buf )) > 0) {
out.write( buf, 0, len );
}
}
in.close();
out.close();
} catch (IOException iOException) {
}
XSSimpleType st = null;
try {
XSOMParser parser = new XSOMParser();
parser.parse( localXMLSchema );
XSSchemaSet schemaSet = parser.getResult();
XSSchema xsSchema = schemaSet.getSchema( 1 );
st = xsSchema.getSimpleType( schemaSimpleType.getTypeName() );
} catch (Exception exp) {
exp.printStackTrace( System.out );
}
localXMLSchema.delete();
return st;
}
示例20
public XSSchemaSet getSchemaComponent() {
return getSource().schemaComponent;
}
示例21
public XSSchemaSet getSchemaSet() {
return schemaSet;
}
示例22
public void visit(XSSchemaSet schemas) {
for (XSSchema schema : schemas.getSchemas())
if (shouldWalk && visited.add(schema))
schema(schema);
}
示例23
public XSSchemaSet getXSSchemaSet() {
return schemaSet;
}
示例24
public JAXBModelImpl bind() {
// this has been problematic. turn it off.
// if(!forest.checkSchemaCorrectness(this))
// return null;
// parse all the binding files given via XJC -b options.
// this also takes care of the binding files given in the -episode option.
for (InputSource is : opts.getBindFiles())
parseSchema(is);
// internalization
SCDBasedBindingSet scdBasedBindingSet = forest.transform(opts.isExtensionMode());
if(!NO_CORRECTNESS_CHECK) {
// correctness check
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
sf.setErrorHandler(new DowngradingErrorHandler(this));
forest.weakSchemaCorrectnessCheck(sf);
if(hadError)
return null; // error in the correctness check. abort now
}
JCodeModel codeModel = new JCodeModel();
ModelLoader gl = new ModelLoader(opts,codeModel,this);
try {
XSSchemaSet result = gl.createXSOM(forest, scdBasedBindingSet);
if(result==null)
return null;
// we need info about each field, so we go ahead and generate the
// skeleton at this point.
// REVISIT: we should separate FieldRenderer and FieldAccessor
// so that accessors can be used before we build the code.
Model model = gl.annotateXMLSchema(result);
if(model==null) return null;
if(hadError) return null; // if we have any error by now, abort
context = model.generateCode(opts,this);
if(context==null) return null;
if(hadError) return null;
return new JAXBModelImpl(context);
} catch( SAXException e ) {
// since XSOM uses our parser that scans DOM,
// no parser error is possible.
// all the other errors will be directed to ErrorReceiver
// before it's thrown, so when the exception is thrown
// the error should have already been reported.
// thus ignore.
return null;
}
}
示例25
public static XmlForm build(XSSchemaSet set) {
return build(set, null);
}
示例26
public boolean updateSchema(String schemaPath, Map<String, XmlElementBean> elementMap) throws SAXException, ParserConfigurationException{
File file = new File(schemaPath);
try {
Map<String,String> uriMapping = new HashMap<String,String>();
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader xr = spf.newSAXParser().getXMLReader();
xr.setContentHandler(new LocalContentHandler(uriMapping));
xr.parse(file.getPath());
XSOMParser parser = new XSOMParser();
parser.parse(file);
XSSchemaSet schemaSet = parser.getResult();
XSSchema xsSchema = schemaSet.getSchema(1);
StringWriter string = new StringWriter();
//TODO - list to map
JumbuneSchemaWriter schemaWriter = new JumbuneSchemaWriter(string,elementMap,uriMapping);
schemaWriter.schema(xsSchema);
FileWriter fw = new FileWriter(schemaPath);
fw.write(string.toString());
fw.close();
return true ;
}
catch (FactoryConfigurationError | IOException e) {
LOGGER.error(e.getMessage());
return false;
}
}
示例27
/**
* Gets the parsed result. Don't call this method until
* you parse all the schemas.
*
* @return
* If there was any parse error, this method returns null.
* To receive error information, specify your error handler
* through the setErrorHandler method.
* @exception SAXException
* This exception will never be thrown unless it is thrown
* by an error handler.
*/
public XSSchemaSet getResult() throws SAXException {
return context.getResult();
}
示例28
/**
* Converts an XML Schema into an abstract form with XML data.
*
* @param set XML Schema representation.
* @return an abstract form with XML data.
*/
public static XmlForm toXmlForm(XSSchemaSet set) {
return XmlFormBuilder.build(set);
}
示例29
/**
* Converts an XML Schema into an abstract form with XML data.
*
* @param set XML Schema representation.
* @return an abstract form with XML data.
*/
public static XmlForm toXmlForm(XSSchemaSet set, String element) {
return XmlFormBuilder.build(set, element);
}