use of org.apache.cxf.common.xmlschema.SchemaCollection in project tomee by apache.
the class StaxDataBinding method initialize.
public void initialize(Service service) {
for (ServiceInfo serviceInfo : service.getServiceInfos()) {
SchemaCollection schemaCollection = serviceInfo.getXmlSchemaCollection();
if (schemaCollection.getXmlSchemas().length > 1) {
// Schemas are already populated.
continue;
}
new ServiceModelVisitor(serviceInfo) {
@Override
public void begin(MessagePartInfo part) {
if (part.getTypeQName() != null || part.getElementQName() != null) {
return;
}
part.setTypeQName(Constants.XSD_ANYTYPE);
}
}.walk();
}
}
use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.
the class JAXBDataBinding method initialize.
@SuppressWarnings("unchecked")
public synchronized void initialize(Service service) {
inInterceptors.addIfAbsent(JAXBAttachmentSchemaValidationHack.INSTANCE);
inFaultInterceptors.addIfAbsent(JAXBAttachmentSchemaValidationHack.INSTANCE);
// context is already set, don't redo it
if (context != null) {
return;
}
contextClasses = new LinkedHashSet<>();
for (ServiceInfo serviceInfo : service.getServiceInfos()) {
JAXBContextInitializer initializer = new JAXBContextInitializer(getBus(), serviceInfo, contextClasses, typeRefs, this.getUnmarshallerProperties());
initializer.walk();
if (serviceInfo.getProperty("extra.class") != null) {
Set<Class<?>> exClasses = serviceInfo.getProperty("extra.class", Set.class);
contextClasses.addAll(exClasses);
}
}
String tns = getNamespaceToUse(service);
final CachedContextAndSchemas cachedContextAndSchemas;
try {
cachedContextAndSchemas = createJAXBContextAndSchemas(contextClasses, tns);
} catch (JAXBException e1) {
throw new ServiceConstructionException(e1);
}
final JAXBContext ctx = cachedContextAndSchemas.getContext();
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "CREATED_JAXB_CONTEXT", new Object[] { ctx, contextClasses });
}
setContext(ctx);
for (ServiceInfo serviceInfo : service.getServiceInfos()) {
SchemaCollection col = serviceInfo.getXmlSchemaCollection();
if (col.getXmlSchemas().length > 1) {
// someone has already filled in the types
justCheckForJAXBAnnotations(serviceInfo);
continue;
}
boolean schemasFromCache = false;
Collection<DOMSource> schemas = getSchemas();
if (schemas == null || schemas.isEmpty()) {
schemas = cachedContextAndSchemas.getSchemas();
if (schemas != null) {
schemasFromCache = true;
}
} else {
schemasFromCache = true;
}
Set<DOMSource> bi = new LinkedHashSet<>();
if (schemas == null) {
schemas = new LinkedHashSet<>();
try {
for (DOMResult r : generateJaxbSchemas()) {
DOMSource src = new DOMSource(r.getNode(), r.getSystemId());
if (isInBuiltInSchemas(r)) {
bi.add(src);
} else {
schemas.add(src);
}
}
// put any builtins at the end. Anything that DOES import them
// will cause it to load automatically and we'll skip them later
schemas.addAll(bi);
} catch (IOException e) {
throw new ServiceConstructionException("SCHEMA_GEN_EXC", LOG, e);
}
}
for (DOMSource r : schemas) {
if (bi.contains(r)) {
String ns = ((Document) r.getNode()).getDocumentElement().getAttribute("targetNamespace");
if (serviceInfo.getSchema(ns) != null) {
continue;
}
}
// StaxUtils.print(r.getNode());
// System.out.println();
addSchemaDocument(serviceInfo, col, (Document) r.getNode(), r.getSystemId());
}
JAXBSchemaInitializer schemaInit = new JAXBSchemaInitializer(serviceInfo, col, context, this.qualifiedSchemas, tns);
schemaInit.walk();
if (cachedContextAndSchemas != null && !schemasFromCache) {
cachedContextAndSchemas.setSchemas(schemas);
}
}
}
use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.
the class WadlGenerator method getSchemaCollection.
private SchemaCollection getSchemaCollection(ResourceTypes resourceTypes, JAXBContext context) {
if (context == null) {
return null;
}
SchemaCollection xmlSchemaCollection = new SchemaCollection();
Collection<DOMSource> schemas = new HashSet<>();
List<String> targetNamespaces = new ArrayList<>();
try {
for (DOMResult r : JAXBUtils.generateJaxbSchemas(context, CastUtils.cast(Collections.emptyMap(), String.class, DOMResult.class))) {
Document doc = (Document) r.getNode();
ElementQNameResolver theResolver = createElementQNameResolver(context);
String tns = doc.getDocumentElement().getAttribute("targetNamespace");
String tnsPrefix = doc.getDocumentElement().lookupPrefix(tns);
if (tnsPrefix == null) {
String tnsDecl = doc.getDocumentElement().getAttribute("xmlns:tns");
tnsPrefix = tnsDecl != null && tnsDecl.equals(tns) ? "tns:" : "";
} else {
tnsPrefix += ":";
}
if (supportJaxbXmlType) {
for (Class<?> cls : resourceTypes.getAllTypes().keySet()) {
if (isXmlRoot(cls)) {
continue;
}
XmlType root = cls.getAnnotation(XmlType.class);
if (root != null) {
QName typeName = theResolver.resolve(cls, new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
if (typeName != null && tns.equals(typeName.getNamespaceURI())) {
QName elementName = resourceTypes.getXmlNameMap().get(cls);
if (elementName == null) {
elementName = typeName;
}
Element newElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:element");
newElement.setAttribute("name", elementName.getLocalPart());
newElement.setAttribute("type", tnsPrefix + typeName.getLocalPart());
if (Modifier.isAbstract(cls.getModifiers()) && resourceTypes.getSubstitutions().values().contains(cls)) {
newElement.setAttribute("abstract", "true");
}
doc.getDocumentElement().appendChild(newElement);
}
}
}
if (supportJaxbSubstitutions) {
for (Map.Entry<Class<?>, Class<?>> entry : resourceTypes.getSubstitutions().entrySet()) {
QName typeName = theResolver.resolve(entry.getKey(), new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
for (Element element : DOMUtils.findAllElementsByTagNameNS(doc.getDocumentElement(), Constants.URI_2001_SCHEMA_XSD, "element")) {
if (element.getAttribute("name").equals(typeName.getLocalPart())) {
QName groupName = theResolver.resolve(entry.getValue(), new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
if (groupName != null) {
element.setAttribute("substitutionGroup", tnsPrefix + groupName.getLocalPart());
}
}
}
}
}
}
if (supportCollections && !resourceTypes.getCollectionMap().isEmpty()) {
for (Map.Entry<Class<?>, QName> entry : resourceTypes.getCollectionMap().entrySet()) {
QName colQName = entry.getValue();
if (colQName == null) {
colQName = theResolver.resolve(entry.getKey(), new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
if (colQName != null) {
colQName = new QName(colQName.getNamespaceURI(), colQName.getLocalPart() + "s", colQName.getPrefix());
}
}
if (colQName == null) {
continue;
}
if (tns.equals(colQName.getNamespaceURI())) {
QName typeName = theResolver.resolve(entry.getKey(), new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
if (typeName != null) {
Element newElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:element");
newElement.setAttribute("name", colQName.getLocalPart());
Element ctElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:complexType");
newElement.appendChild(ctElement);
Element seqElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:sequence");
ctElement.appendChild(seqElement);
Element xsElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:element");
seqElement.appendChild(xsElement);
xsElement.setAttribute("ref", tnsPrefix + typeName.getLocalPart());
xsElement.setAttribute("minOccurs", "0");
xsElement.setAttribute("maxOccurs", "unbounded");
doc.getDocumentElement().appendChild(newElement);
}
}
}
}
DOMSource source = new DOMSource(doc, r.getSystemId());
schemas.add(source);
if (!StringUtils.isEmpty(tns)) {
targetNamespaces.add(tns);
}
}
} catch (IOException e) {
LOG.fine("No schema can be generated");
return null;
}
boolean hackAroundEmptyNamespaceIssue = false;
for (DOMSource r : schemas) {
hackAroundEmptyNamespaceIssue = addSchemaDocument(xmlSchemaCollection, targetNamespaces, (Document) r.getNode(), r.getSystemId(), hackAroundEmptyNamespaceIssue);
}
return xmlSchemaCollection;
}
use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.
the class SoapBindingFactory method addMessageFromBinding.
protected void addMessageFromBinding(ExtensibilityElement ext, BindingOperationInfo bop, boolean isInput) {
SoapHeader header = SOAPBindingUtil.getSoapHeader(ext);
ServiceInfo serviceInfo = bop.getBinding().getService();
if (header != null && header.getMessage() == null) {
throw new RuntimeException("Problem with WSDL: soap:header element" + " for operation " + bop.getName() + " under binding " + bop.getBinding().getName() + " does not contain a valid message attribute.");
}
if (header != null && serviceInfo.getMessage(header.getMessage()) == null) {
Definition def = (Definition) serviceInfo.getProperty(WSDLServiceBuilder.WSDL_DEFINITION);
SchemaCollection schemas = serviceInfo.getXmlSchemaCollection();
if (def != null && schemas != null) {
QName qn = header.getMessage();
javax.wsdl.Message msg = findMessage(qn, def);
if (msg != null) {
addOutOfBandParts(bop, msg, schemas, isInput, header.getPart());
serviceInfo.refresh();
} else {
throw new RuntimeException("Problem with WSDL: soap:header element" + " for operation " + bop.getName() + " is referring to an undefined wsdl:message element: " + qn);
}
}
}
}
use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.
the class StaxDataBinding method initialize.
public void initialize(Service service) {
for (ServiceInfo serviceInfo : service.getServiceInfos()) {
SchemaCollection schemaCollection = serviceInfo.getXmlSchemaCollection();
if (schemaCollection.getXmlSchemas().length > 1) {
// Schemas are already populated.
continue;
}
new ServiceModelVisitor(serviceInfo) {
@Override
public void begin(MessagePartInfo part) {
if (part.getTypeQName() != null || part.getElementQName() != null) {
return;
}
part.setTypeQName(Constants.XSD_ANYTYPE);
}
}.walk();
}
}
Aggregations