use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.
the class BasicNameManager method initialize.
private void initialize(ServiceInfo service, Endpoint endpoint) {
nsPrefixMap = new HashMap<>();
if (endpoint != null) {
JavascriptOptionsFeature options = getOptions(endpoint);
if (options.getNamespacePrefixMap() != null) {
nsPrefixMap.putAll(options.getNamespacePrefixMap());
}
}
Set<String> poorPrefixURIs = new HashSet<>();
for (SchemaInfo schemaInfo : service.getSchemas()) {
NamespacePrefixList schemaPrefixList = schemaInfo.getSchema().getNamespaceContext();
for (String declaredPrefix : schemaPrefixList.getDeclaredPrefixes()) {
String uri = schemaPrefixList.getNamespaceURI(declaredPrefix);
if (!nsPrefixMap.containsKey(uri)) {
// first schema to define a prefix wins.
if (declaredPrefix.startsWith("ns") || "tns".equals(declaredPrefix)) {
poorPrefixURIs.add(uri);
} else {
nsPrefixMap.put(uri, declaredPrefix.toUpperCase());
}
}
}
}
for (String uri : poorPrefixURIs) {
defineFallbackPrefix(uri);
}
}
use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.
the class GenericAegisTest method testGenerateJavascript.
// the claim is that code generation makes this go boom.
@Test
public void testGenerateJavascript() throws Exception {
// Create our service implementation
GenericGenericClass<String> impl = new GenericGenericClass<String>();
// Create our Server
ServerFactoryBean svrFactory = new ServerFactoryBean();
// we sure can't get a .class for the interface, can we?
svrFactory.setServiceClass(impl.getClass());
svrFactory.setAddress("http://localhost:" + PORT + "/aegisgeneric");
svrFactory.setServiceBean(impl);
Server server = svrFactory.create();
ServiceInfo serviceInfo = ((EndpointImpl) server.getEndpoint()).getEndpointInfo().getService();
Collection<SchemaInfo> schemata = serviceInfo.getSchemas();
BasicNameManager nameManager = BasicNameManager.newNameManager(serviceInfo);
NamespacePrefixAccumulator prefixManager = new NamespacePrefixAccumulator(serviceInfo.getXmlSchemaCollection());
for (SchemaInfo schema : schemata) {
SchemaJavascriptBuilder builder = new SchemaJavascriptBuilder(serviceInfo.getXmlSchemaCollection(), prefixManager, nameManager);
String allThatJavascript = builder.generateCodeForSchema(schema.getSchema());
assertNotNull(allThatJavascript);
}
ServiceJavascriptBuilder serviceBuilder = new ServiceJavascriptBuilder(serviceInfo, null, prefixManager, nameManager);
serviceBuilder.walk();
String serviceJavascript = serviceBuilder.getCode();
assertNotNull(serviceJavascript);
}
use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.
the class SerializationTest method setupClientAndRhino.
private void setupClientAndRhino(String clientProxyFactoryBeanId) throws IOException {
testUtilities.setBus(getBean(Bus.class, "cxf"));
testUtilities.initializeRhino();
testUtilities.readResourceIntoRhino("/org/apache/cxf/javascript/cxf-utils.js");
clientProxyFactory = getBean(JaxWsProxyFactoryBean.class, clientProxyFactoryBeanId);
client = clientProxyFactory.getClientFactoryBean().create();
serviceInfos = client.getEndpoint().getService().getServiceInfos();
// there can only be one.
assertEquals(1, serviceInfos.size());
ServiceInfo serviceInfo = serviceInfos.get(0);
schemata = serviceInfo.getSchemas();
nameManager = BasicNameManager.newNameManager(serviceInfo);
NamespacePrefixAccumulator prefixAccumulator = new NamespacePrefixAccumulator(serviceInfo.getXmlSchemaCollection());
for (SchemaInfo schema : schemata) {
SchemaJavascriptBuilder builder = new SchemaJavascriptBuilder(serviceInfo.getXmlSchemaCollection(), prefixAccumulator, nameManager);
String allThatJavascript = builder.generateCodeForSchema(schema.getSchema());
assertNotNull(allThatJavascript);
testUtilities.readStringIntoRhino(allThatJavascript, schema.toString() + ".js");
}
}
use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.
the class DynamicClientFactory method addSchemas.
private void addSchemas(Options opts, SchemaCompiler schemaCompiler, List<ServiceInfo> serviceList, SchemaCollection schemaCollection) {
Map<String, Element> done = new HashMap<>();
Map<String, Element> notDone = new HashMap<>();
OASISCatalogManager catalog = bus.getExtension(OASISCatalogManager.class);
for (XmlSchema schema : schemaCollection.getXmlSchemas()) {
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
continue;
}
String key = schema.getSourceURI();
if (done.containsKey(key)) {
continue;
}
if (!key.startsWith("file:") && !key.startsWith("jar:")) {
XmlSchemaSerializer xser = new XmlSchemaSerializer();
xser.setExtReg(schemaCollection.getExtReg());
Document[] docs;
try {
docs = xser.serializeSchema(schema, false);
} catch (XmlSchemaSerializerException e) {
throw new RuntimeException(e);
}
Element ele = docs[0].getDocumentElement();
ele = removeImportElement(ele, key, catalog, done, notDone);
try {
docs[0].setDocumentURI(key);
} catch (Throwable t) {
// ignore - DOM level 3
}
if (ele != null) {
InputSource is = new InputSource((InputStream) null);
// key = key.replaceFirst("#types[0-9]+$", "");
is.setSystemId(key);
is.setPublicId(key);
opts.addGrammar(is);
schemaCompiler.parseSchema(key, ele);
}
}
}
for (XmlSchema schema : schemaCollection.getXmlSchemas()) {
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
continue;
}
String key = schema.getSourceURI();
if (done.containsKey(key)) {
continue;
}
if (key.startsWith("file:") || key.startsWith("jar:")) {
InputStream in = null;
try {
if (key.contains("#")) {
for (ServiceInfo si : serviceList) {
for (SchemaInfo sci : si.getSchemas()) {
if (key != null && key.equals(sci.getSystemId())) {
key = null;
}
}
}
}
if (key == null) {
continue;
}
if (key.startsWith("file:")) {
in = Files.newInputStream(new File(new URI(key)).toPath());
} else {
in = new URL(key).openStream();
}
XMLStreamReader reader = StaxUtils.createXMLStreamReader(key, in);
reader = new LocationFilterReader(reader, catalog);
InputSource is = new InputSource(key);
opts.addGrammar(is);
schemaCompiler.parseSchema(key, reader);
reader.close();
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
}
}
}
for (ServiceInfo si : serviceList) {
for (SchemaInfo sci : si.getSchemas()) {
String key = sci.getSystemId();
if (done.containsKey(key)) {
continue;
}
Element ele = sci.getElement();
ele = removeImportElement(ele, key, catalog, done, notDone);
if (ele != null) {
InputSource is = new InputSource((InputStream) null);
// key = key.replaceFirst("#types[0-9]+$", "");
is.setSystemId(key);
is.setPublicId(key);
opts.addGrammar(is);
schemaCompiler.parseSchema(key, StaxUtils.createXMLStreamReader(ele, key));
}
}
}
for (Map.Entry<String, Element> el : notDone.entrySet()) {
InputSource is = new InputSource((InputStream) null);
// key = key.replaceFirst("#types[0-9]+$", "");
is.setSystemId(el.getKey());
is.setPublicId(el.getKey());
opts.addGrammar(is);
schemaCompiler.parseSchema(el.getKey(), StaxUtils.createXMLStreamReader(el.getValue(), el.getKey()));
}
}
use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.
the class JavaToJSProcessor method process.
public void process() throws ToolException {
String oldClassPath = System.getProperty(JAVA_CLASS_PATH);
LOG.log(Level.INFO, "OLD_CP", oldClassPath);
if (context.get(ToolConstants.CFG_CLASSPATH) != null) {
String newCp = (String) context.get(ToolConstants.CFG_CLASSPATH);
System.setProperty(JAVA_CLASS_PATH, newCp + File.pathSeparator + oldClassPath);
LOG.log(Level.INFO, "NEW_CP", newCp);
}
// check for command line specification of data binding.
ServiceBuilder builder = getServiceBuilder();
ServiceInfo serviceInfo = builder.createService();
File jsFile = getOutputFile(builder.getOutputFile(), serviceInfo.getName().getLocalPart() + ".js");
BasicNameManager nameManager = BasicNameManager.newNameManager(serviceInfo, null);
NamespacePrefixAccumulator prefixManager = new NamespacePrefixAccumulator(serviceInfo.getXmlSchemaCollection());
Collection<SchemaInfo> schemata = serviceInfo.getSchemas();
BufferedWriter writer = null;
try {
OutputStream outputStream = Files.newOutputStream(jsFile.toPath());
if (null != context.get(ToolConstants.CFG_JAVASCRIPT_UTILS)) {
JavascriptGetInterceptor.writeUtilsToResponseStream(JavaToJSProcessor.class, outputStream);
}
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, UTF8);
writer = new BufferedWriter(outputStreamWriter);
for (SchemaInfo schema : schemata) {
SchemaJavascriptBuilder jsBuilder = new SchemaJavascriptBuilder(serviceInfo.getXmlSchemaCollection(), prefixManager, nameManager);
String allThatJavascript = jsBuilder.generateCodeForSchema(schema.getSchema());
writer.append(allThatJavascript);
}
ServiceJavascriptBuilder serviceBuilder = new ServiceJavascriptBuilder(serviceInfo, null, prefixManager, nameManager);
serviceBuilder.walk();
String serviceJavascript = serviceBuilder.getCode();
writer.append(serviceJavascript);
} catch (FileNotFoundException e) {
throw new ToolException(e);
} catch (IOException e) {
throw new ToolException(e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
throw new ToolException(e);
}
}
}
System.setProperty(JAVA_CLASS_PATH, oldClassPath);
LOG.log(Level.INFO, "RESUME_CP", oldClassPath);
}
Aggregations