com.sun.xml.xsom.parser.XSOMParser Java Examples

The following examples show how to use com.sun.xml.xsom.parser.XSOMParser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SoapProtocol.java    From jolie with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void parseSchemaElement( Definition definition, Element element, XSOMParser schemaParser )
	throws IOException {
	try {
		Transformer transformer = transformerFactory.newTransformer();
		transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
		StringWriter sw = new StringWriter();
		StreamResult result = new StreamResult( sw );
		DOMSource source = new DOMSource( element );
		transformer.transform( source, result );
		InputSource schemaSource = new InputSource( new StringReader( sw.toString() ) );
		schemaSource.setSystemId( definition.getDocumentBaseURI() );
		schemaParser.parse( schemaSource );
	} catch( SAXException | TransformerException e ) {
		throw new IOException( e );
	}
}
 
Example #2
Source File: SoapProtocol.java    From jolie with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void parseWSDLTypes( XSOMParser schemaParser )
	throws IOException {
	Definition definition = getWSDLDefinition();
	if( definition != null ) {
		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();
					Map< String, String > namespaces = definition.getNamespaces();
					for( Entry< String, String > entry : namespaces.entrySet() ) {
						if( entry.getKey().equals( "xmlns" ) || entry.getKey().trim().isEmpty() ) {
							continue;
						}
						if( schemaElement.getAttribute( "xmlns:" + entry.getKey() ).isEmpty() ) {
							schemaElement.setAttribute( "xmlns:" + entry.getKey(), entry.getValue() );
						}
					}
					parseSchemaElement( definition, schemaElement, schemaParser );
				}
			}
		}
	}
}
 
Example #3
Source File: SoapProtocol.java    From jolie with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
}
 
Example #4
Source File: ParserContext.java    From jolie with GNU Lesser General Public License v2.1 6 votes vote down vote up
public ParserContext( XSOMParser owner, XMLParser parser ) {
    this.owner = owner;
    this.parser = parser;

    try {
        parse(new InputSource(ParserContext.class.getResource("../../../../../../META-INF/datatypes.xsd").toExternalForm()));
        SchemaImpl xs = (SchemaImpl)schemaSet.getSchema("http://www.w3.org/2001/XMLSchema");
        xs.addSimpleType(schemaSet.anySimpleType,true);
        xs.addComplexType(schemaSet.anyType,true);
    } catch( SAXException e ) {
        // this must be a bug of XSOM
        if(e.getException()!=null)
            e.getException().printStackTrace();
        else
            e.printStackTrace();
        throw new InternalError();
    }
}
 
Example #5
Source File: SimpleTypesAnalyzerTest.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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;
}
 
Example #6
Source File: XmlUtils.java    From jolie with GNU Lesser General Public License v2.1 4 votes vote down vote up
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 );
	}

}
 
Example #7
Source File: XMLSchemaProcessor.java    From ET_Redux with Apache License 2.0 4 votes vote down vote up
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;
    }
 
Example #8
Source File: SchemaGenerator.java    From jumbune with GNU Lesser General Public License v3.0 3 votes vote down vote up
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;
 }

}