javax.xml.bind.SchemaOutputResolver Java Examples

The following examples show how to use javax.xml.bind.SchemaOutputResolver. 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: WebServiceConfiguration.java    From cukes with Apache License 2.0 6 votes vote down vote up
@Bean
public XsdSchemaCollection schemaCollection() throws JAXBException, IOException {
    JAXBContext context = JAXBContext.newInstance(CalculatorRequest.class, CalculatorResponse.class);
    List<ByteArrayOutputStream> outs = new ArrayList<>();
    context.generateSchema(new SchemaOutputResolver() {
        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            outs.add(out);
            StreamResult result = new StreamResult(out);
            result.setSystemId(suggestedFileName);
            return result;
        }
    });
    Resource[] resources = outs.stream().
        map(ByteArrayOutputStream::toByteArray).
        map(InMemoryResource::new).
        collect(Collectors.toList()).
        toArray(new Resource[]{});
    return new CommonsXsdSchemaCollection(resources);
}
 
Example #2
Source File: JAXBUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static List<DOMResult> generateJaxbSchemas(
    JAXBContext context, final Map<String, DOMResult> builtIns) throws IOException {
    final List<DOMResult> results = new ArrayList<>();

    context.generateSchema(new SchemaOutputResolver() {
        @Override
        public Result createOutput(String ns, String file) throws IOException {
            DOMResult result = new DOMResult();

            if (builtIns.containsKey(ns)) {
                DOMResult dr = builtIns.get(ns);
                result.setSystemId(dr.getSystemId());
                results.add(dr);
                return result;
            }
            result.setSystemId(file);
            results.add(result);
            return result;
        }
    });
    return results;
}
 
Example #3
Source File: QuerySchemaWriter.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	Path configFile = Paths.get("src/main/resources/org/citydb/config/schema/query.xsd");
	System.out.print("Generting XML schema in " + configFile.toAbsolutePath() + "... ");
	
	JAXBContext context = JAXBContext.newInstance(QueryWrapper.class);
	context.generateSchema(new SchemaOutputResolver() {
		@Override
		public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
			if (ConfigUtil.CITYDB_CONFIG_NAMESPACE_URI.equals(namespaceUri)) {
				Files.createDirectories(configFile.getParent());
				StreamResult res = new StreamResult();
				res.setSystemId(configFile.toUri().toString());

				return res;
			} else
				return null;
		}
		
	});
	
	System.out.println("finished.");
}
 
Example #4
Source File: DefaultToolingXMLService.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void generateSchema(Path outputPath)
{
    try
    {
        JAXBContext jaxbContext = getJAXBContext();
        SchemaOutputResolver sor = new SchemaOutputResolver()
        {
            @Override
            public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException
            {
                StreamResult result = new StreamResult();
                result.setSystemId(outputPath.toUri().toString());
                return result;
            }
        };
        jaxbContext.generateSchema(sor);
    }
    catch (JAXBException | IOException e)
    {
        throw new WindupException("Error generating Windup schema due to: " + e.getMessage(), e);
    }
}
 
Example #5
Source File: DevelopmentGenerateJaxbSchema.java    From settlers-remake with MIT License 5 votes vote down vote up
public static void main(String[] args) throws JAXBException, IOException {
	JAXBContext jaxbContext = JAXBContext.newInstance(Presets.class);
	SchemaOutputResolver sor = new SchemaOutputResolver() {

		@Override
		public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
			File file = new File("src/jsettlers/mapcreator/presetloader/preset.xsd");
			StreamResult result = new StreamResult(file);
			result.setSystemId(file.toURI().toURL().toString());
			return result;
		}
	};
	jaxbContext.generateSchema(sor);
	System.out.println("Finished!");
}
 
Example #6
Source File: SingleType.java    From wadl-tools with Apache License 2.0 5 votes vote down vote up
private String tryBuildSchemaFromJaxbAnnotatedClass() throws JAXBException, IOException {
    final StringWriter stringWriter = new StringWriter();
    JAXBContext.newInstance(clazz).generateSchema(new SchemaOutputResolver() {
        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            final StreamResult result = new StreamResult(stringWriter);
            result.setSystemId("schema" + clazz.getSimpleName());
            return result;
        }
    });

    return stringWriter.toString();
}
 
Example #7
Source File: ConfigReader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) {
    File baseDir = new File(xmlpath, config.getBaseDir().getPath());
    SchemaOutputResolverImpl outResolver = new SchemaOutputResolverImpl (baseDir);

    for( Schema schema : (List<Schema>)config.getSchema() ) {
        String namespace = schema.getNamespace();
        File location = schema.getLocation();
        outResolver.addSchemaInfo(namespace,location);
    }
    return outResolver;
}
 
Example #8
Source File: ConfigReader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) {
    File baseDir = new File(xmlpath, config.getBaseDir().getPath());
    SchemaOutputResolverImpl outResolver = new SchemaOutputResolverImpl (baseDir);

    for( Schema schema : (List<Schema>)config.getSchema() ) {
        String namespace = schema.getNamespace();
        File location = schema.getLocation();
        outResolver.addSchemaInfo(namespace,location);
    }
    return outResolver;
}
 
Example #9
Source File: ApprovalsUtil.java    From recheck with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String generateSchema( final Class<?> type ) throws JAXBException, IOException {
	final StringWriter stringWriter = new StringWriter();

	final JAXBContext jaxbContext =
			JAXBContextFactory.createContext( new Class<?>[] { type }, Collections.emptyMap() );
	jaxbContext.generateSchema( new SchemaOutputResolver() {
		@Override
		public final Result createOutput( final String namespaceURI, final String suggestedFileName )
				throws IOException {
			final StreamResult result = new StreamResult( stringWriter );
			result.setSystemId( suggestedFileName );
			return result;
		}
	} );

	return stringWriter.toString();
}
 
Example #10
Source File: SchemaGen.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	final File baseDir = new File("./data/static_data");

	class MySchemaOutputResolver extends SchemaOutputResolver {

		@Override
		public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
			return new StreamResult(new File(baseDir, "static_data1.xsd"));
		}
	}
	JAXBContext context = JAXBContext.newInstance(StaticData.class);
	context.generateSchema(new MySchemaOutputResolver());
}
 
Example #11
Source File: ConfigReader.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) {
    File baseDir = new File(xmlpath, config.getBaseDir().getPath());
    SchemaOutputResolverImpl outResolver = new SchemaOutputResolverImpl (baseDir);

    for( Schema schema : (List<Schema>)config.getSchema() ) {
        String namespace = schema.getNamespace();
        File location = schema.getLocation();
        outResolver.addSchemaInfo(namespace,location);
    }
    return outResolver;
}
 
Example #12
Source File: ConfigReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) {
    File baseDir = new File(xmlpath, config.getBaseDir().getPath());
    SchemaOutputResolverImpl outResolver = new SchemaOutputResolverImpl (baseDir);

    for( Schema schema : (List<Schema>)config.getSchema() ) {
        String namespace = schema.getNamespace();
        File location = schema.getLocation();
        outResolver.addSchemaInfo(namespace,location);
    }
    return outResolver;
}
 
Example #13
Source File: ConfigReader.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) {
    File baseDir = new File(xmlpath, config.getBaseDir().getPath());
    SchemaOutputResolverImpl outResolver = new SchemaOutputResolverImpl (baseDir);

    for( Schema schema : (List<Schema>)config.getSchema() ) {
        String namespace = schema.getNamespace();
        File location = schema.getLocation();
        outResolver.addSchemaInfo(namespace,location);
    }
    return outResolver;
}
 
Example #14
Source File: ConfigReader.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) {
    File baseDir = new File(xmlpath, config.getBaseDir().getPath());
    SchemaOutputResolverImpl outResolver = new SchemaOutputResolverImpl (baseDir);

    for( Schema schema : (List<Schema>)config.getSchema() ) {
        String namespace = schema.getNamespace();
        File location = schema.getLocation();
        outResolver.addSchemaInfo(namespace,location);
    }
    return outResolver;
}
 
Example #15
Source File: ConfigReader.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) {
    File baseDir = new File(xmlpath, config.getBaseDir().getPath());
    SchemaOutputResolverImpl outResolver = new SchemaOutputResolverImpl (baseDir);

    for( Schema schema : (List<Schema>)config.getSchema() ) {
        String namespace = schema.getNamespace();
        File location = schema.getLocation();
        outResolver.addSchemaInfo(namespace,location);
    }
    return outResolver;
}
 
Example #16
Source File: WorkflowProfilesTest.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
public void testCreateSchema() throws Exception {
    JAXBContext jctx = JAXBContext.newInstance(WorkflowDefinition.class);
    final Map<String, StreamResult> schemas = new HashMap<String, StreamResult>();
    jctx.generateSchema(new SchemaOutputResolver() {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult sr = new StreamResult(new StringWriter());
            sr.setSystemId(namespaceUri);
            schemas.put(namespaceUri, sr);
            return sr;
        }
    });
    System.out.println(schemas.get(WorkflowProfileConsts.NS_WORKFLOW_V1).getWriter().toString());
}
 
Example #17
Source File: DynamicSchemaTest.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test(expected = MarshalException.class)
public void generatesAndUsesSchema() throws JAXBException, IOException,
		SAXException {
	final JAXBContext context = JAXBContext.newInstance(A.class);
	final DOMResult result = new DOMResult();
	result.setSystemId("schema.xsd");
	context.generateSchema(new SchemaOutputResolver() {
		@Override
		public Result createOutput(String namespaceUri,
				String suggestedFileName) {
			return result;
		}
	});

	@SuppressWarnings("deprecation")
	final SchemaFactory schemaFactory = SchemaFactory
			.newInstance(WellKnownNamespace.XML_SCHEMA);
	final Schema schema = schemaFactory.newSchema(new DOMSource(result
			.getNode()));

	final Marshaller marshaller = context.createMarshaller();
	marshaller.setSchema(schema);
	// Works
	marshaller.marshal(new A("works"), System.out);
	// Fails
	marshaller.marshal(new A(null), System.out);
}
 
Example #18
Source File: JAXBRIContextWrapper.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void generateSchema(SchemaOutputResolver outputResolver)
        throws IOException {
    context.generateSchema(outputResolver);
}
 
Example #19
Source File: JAXBModelImpl.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void generateSchema(SchemaOutputResolver outputResolver, ErrorListener errorListener) throws IOException {
    getSchemaGenerator().write(outputResolver,errorListener);
}
 
Example #20
Source File: JAXBModelImpl.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void generateSchema(SchemaOutputResolver outputResolver, ErrorListener errorListener) throws IOException {
    getSchemaGenerator().write(outputResolver,errorListener);
}
 
Example #21
Source File: XmlSchemaGenerator.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Write out the schema documents.
 */
public void write(SchemaOutputResolver resolver, ErrorListener errorListener) throws IOException {
    if(resolver==null)
        throw new IllegalArgumentException();

    if(logger.isLoggable(Level.FINE)) {
        // debug logging to see what's going on.
        logger.log(Level.FINE,"Writing XML Schema for "+toString(),new StackRecorder());
    }

    // make it fool-proof
    resolver = new FoolProofResolver(resolver);
    this.errorListener = errorListener;

    Map<String, String> schemaLocations = types.getSchemaLocations();

    Map<Namespace,Result> out = new HashMap<Namespace,Result>();
    Map<Namespace,String> systemIds = new HashMap<Namespace,String>();

    // we create a Namespace object for the XML Schema namespace
    // as a side-effect, but we don't want to generate it.
    namespaces.remove(WellKnownNamespace.XML_SCHEMA);

    // first create the outputs for all so that we can resolve references among
    // schema files when we write
    for( Namespace n : namespaces.values() ) {
        String schemaLocation = schemaLocations.get(n.uri);
        if(schemaLocation!=null) {
            systemIds.put(n,schemaLocation);
        } else {
            Result output = resolver.createOutput(n.uri,"schema"+(out.size()+1)+".xsd");
            if(output!=null) {  // null result means no schema for that namespace
                out.put(n,output);
                systemIds.put(n,output.getSystemId());
            }
        }
        //Clear the namespace specific set with already written classes
        n.resetWritten();
    }

    // then write'em all
    for( Map.Entry<Namespace,Result> e : out.entrySet() ) {
        Result result = e.getValue();
        e.getKey().writeTo( result, systemIds );
        if(result instanceof StreamResult) {
            OutputStream outputStream = ((StreamResult)result).getOutputStream();
            if(outputStream != null) {
                outputStream.close(); // fix for bugid: 6291301
            } else {
                final Writer writer = ((StreamResult)result).getWriter();
                if(writer != null) writer.close();
            }
        }
    }
}
 
Example #22
Source File: ConfigReader.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This returns the SchemaOutputResolver to generate the schemas
 */
public SchemaOutputResolver getSchemaOutputResolver(){
    return schemaOutputResolver;
}
 
Example #23
Source File: FoolProofResolver.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public FoolProofResolver(SchemaOutputResolver resolver) {
    assert resolver!=null;
    this.resolver = resolver;
}
 
Example #24
Source File: XmlSchemaGenerator.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Write out the schema documents.
 */
public void write(SchemaOutputResolver resolver, ErrorListener errorListener) throws IOException {
    if(resolver==null)
        throw new IllegalArgumentException();

    if(logger.isLoggable(Level.FINE)) {
        // debug logging to see what's going on.
        logger.log(Level.FINE,"Writing XML Schema for "+toString(),new StackRecorder());
    }

    // make it fool-proof
    resolver = new FoolProofResolver(resolver);
    this.errorListener = errorListener;

    Map<String, String> schemaLocations = types.getSchemaLocations();

    Map<Namespace,Result> out = new HashMap<Namespace,Result>();
    Map<Namespace,String> systemIds = new HashMap<Namespace,String>();

    // we create a Namespace object for the XML Schema namespace
    // as a side-effect, but we don't want to generate it.
    namespaces.remove(WellKnownNamespace.XML_SCHEMA);

    // first create the outputs for all so that we can resolve references among
    // schema files when we write
    for( Namespace n : namespaces.values() ) {
        String schemaLocation = schemaLocations.get(n.uri);
        if(schemaLocation!=null) {
            systemIds.put(n,schemaLocation);
        } else {
            Result output = resolver.createOutput(n.uri,"schema"+(out.size()+1)+".xsd");
            if(output!=null) {  // null result means no schema for that namespace
                out.put(n,output);
                systemIds.put(n,output.getSystemId());
            }
        }
        //Clear the namespace specific set with already written classes
        n.resetWritten();
    }

    // then write'em all
    for( Map.Entry<Namespace,Result> e : out.entrySet() ) {
        Result result = e.getValue();
        e.getKey().writeTo( result, systemIds );
        if(result instanceof StreamResult) {
            OutputStream outputStream = ((StreamResult)result).getOutputStream();
            if(outputStream != null) {
                outputStream.close(); // fix for bugid: 6291301
            } else {
                final Writer writer = ((StreamResult)result).getWriter();
                if(writer != null) writer.close();
            }
        }
    }
}
 
Example #25
Source File: JAXBRIContextWrapper.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void generateSchema(SchemaOutputResolver outputResolver)
        throws IOException {
    context.generateSchema(outputResolver);
}
 
Example #26
Source File: FoolProofResolver.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public FoolProofResolver(SchemaOutputResolver resolver) {
    assert resolver!=null;
    this.resolver = resolver;
}
 
Example #27
Source File: XmlSchemaGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Write out the schema documents.
 */
public void write(SchemaOutputResolver resolver, ErrorListener errorListener) throws IOException {
    if(resolver==null)
        throw new IllegalArgumentException();

    if(logger.isLoggable(Level.FINE)) {
        // debug logging to see what's going on.
        logger.log(Level.FINE,"Writing XML Schema for "+toString(),new StackRecorder());
    }

    // make it fool-proof
    resolver = new FoolProofResolver(resolver);
    this.errorListener = errorListener;

    Map<String, String> schemaLocations = types.getSchemaLocations();

    Map<Namespace,Result> out = new HashMap<Namespace,Result>();
    Map<Namespace,String> systemIds = new HashMap<Namespace,String>();

    // we create a Namespace object for the XML Schema namespace
    // as a side-effect, but we don't want to generate it.
    namespaces.remove(WellKnownNamespace.XML_SCHEMA);

    // first create the outputs for all so that we can resolve references among
    // schema files when we write
    for( Namespace n : namespaces.values() ) {
        String schemaLocation = schemaLocations.get(n.uri);
        if(schemaLocation!=null) {
            systemIds.put(n,schemaLocation);
        } else {
            Result output = resolver.createOutput(n.uri,"schema"+(out.size()+1)+".xsd");
            if(output!=null) {  // null result means no schema for that namespace
                out.put(n,output);
                systemIds.put(n,output.getSystemId());
            }
        }
        //Clear the namespace specific set with already written classes
        n.resetWritten();
    }

    // then write'em all
    for( Map.Entry<Namespace,Result> e : out.entrySet() ) {
        Result result = e.getValue();
        e.getKey().writeTo( result, systemIds );
        if(result instanceof StreamResult) {
            OutputStream outputStream = ((StreamResult)result).getOutputStream();
            if(outputStream != null) {
                outputStream.close(); // fix for bugid: 6291301
            } else {
                final Writer writer = ((StreamResult)result).getWriter();
                if(writer != null) writer.close();
            }
        }
    }
}
 
Example #28
Source File: JAXBRIContextWrapper.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void generateSchema(SchemaOutputResolver outputResolver)
        throws IOException {
    context.generateSchema(outputResolver);
}
 
Example #29
Source File: JAXBModelImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void generateSchema(SchemaOutputResolver outputResolver, ErrorListener errorListener) throws IOException {
    getSchemaGenerator().write(outputResolver,errorListener);
}
 
Example #30
Source File: FoolProofResolver.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public FoolProofResolver(SchemaOutputResolver resolver) {
    assert resolver!=null;
    this.resolver = resolver;
}