org.apache.ws.commons.schema.utils.NamespaceMap Java Examples

The following examples show how to use org.apache.ws.commons.schema.utils.NamespaceMap. 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: XsdAnnotationEmitter.java    From legstar-core2 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Adds the COXB namespace and associated prefixes to the XML schema.
 */
protected void addNamespaceContext() {
    NamespaceMap prefixmap = new NamespaceMap();
    NamespacePrefixList npl = getXsd().getNamespaceContext();
    if (npl == null) {
        /* We get an NPE if we don't add this. */
        prefixmap.add("", XMLConstants.W3C_XML_SCHEMA_NS_URI);
    } else {
        for (int i = 0; i < npl.getDeclaredPrefixes().length; i++) {
            prefixmap.add(npl.getDeclaredPrefixes()[i],
                    npl.getNamespaceURI(npl.getDeclaredPrefixes()[i]));
        }
    }
    prefixmap.add(getCOXBNamespacePrefix(), getCOXBNamespace());
    getXsd().setNamespaceContext(prefixmap);

}
 
Example #2
Source File: JAXBSchemaInitializer.java    From cxf with Apache License 2.0 6 votes vote down vote up
private SchemaInfo createSchemaIfNeeded(String namespace, NamespaceMap nsMap) {
    SchemaInfo schemaInfo = serviceInfo.getSchema(namespace);
    if (schemaInfo == null) {
        XmlSchema xmlSchema = schemas.newXmlSchemaInCollection(namespace);

        if (qualifiedSchemas) {
            xmlSchema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
        }

        xmlSchema.setNamespaceContext(nsMap);

        schemaInfo = new SchemaInfo(namespace);
        schemaInfo.setSchema(xmlSchema);
        serviceInfo.addSchema(schemaInfo);
    }
    return schemaInfo;
}
 
Example #3
Source File: DataServiceDocLitWrappedSchemaGenerator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the XML schema with the given namespace.
 * @param cparams Common parameters used in the schema generator
 * @param namespace The target namespace of the XML schema
 * @return The XML schema object
 */
private static XmlSchema retrieveSchema(CommonParams cparams, String namespace) {
	Map<String, XmlSchema> schemaMap = cparams.getSchemaMap();
	if (!schemaMap.containsKey(namespace)) {
		XmlSchema schema = new XmlSchema(namespace, cparams.getXmlSchemaCollection());
		schema.setNamespaceContext(new NamespaceMap());
		schemaMap.put(namespace, schema);
		schema.setElementFormDefault(new XmlSchemaForm(XmlSchemaForm.QUALIFIED));
		schema.setAttributeFormDefault(new XmlSchemaForm(XmlSchemaForm.UNQUALIFIED));
	}
	return schemaMap.get(namespace);
}
 
Example #4
Source File: DBDeployer.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a schema from a DataService object, to be used later in WSDL generation.
 */
@SuppressWarnings ("unchecked")
private void createDSSchema(AxisService axisService, DataService dataService)
		throws DataServiceFault {
	NamespaceMap map = new NamespaceMap();
	map.put(Java2WSDLConstants.DEFAULT_SCHEMA_NAMESPACE_PREFIX, Java2WSDLConstants.URI_2001_SCHEMA_XSD);
	axisService.setNamespaceMap(map);
	DataServiceDocLitWrappedSchemaGenerator.populateServiceSchema(axisService);
}
 
Example #5
Source File: Cob2Xsd.java    From legstar-core2 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Create an empty XML Schema.
 * <p/>
 * If no targetNamespace, make sure there is no default namespace otherwise
 * our complex types would be considered part of that default namespace
 * (usually XML Schema namespace).
 * 
 * @param encoding the character set used to encode this XML Schema
 * @param targetNamespace the target namespace to use (null for no namespace)
 * @return a new empty XML schema using the model
 */
protected XmlSchema createXmlSchema(final String encoding, final String targetNamespace) {
    XmlSchema xsd = new XmlSchema(targetNamespace,
            new XmlSchemaCollection());
    if (targetNamespace != null) {
        xsd.setElementFormDefault(XmlSchemaForm.QUALIFIED);
    }
    xsd.setAttributeFormDefault(null);
    xsd.setInputEncoding(encoding);
    if (targetNamespace == null) {
        NamespaceMap prefixmap = new NamespaceMap();
        NamespacePrefixList npl = xsd.getNamespaceContext();
        if (npl == null) {
            prefixmap.add("xsd", XMLConstants.W3C_XML_SCHEMA_NS_URI);
        } else {
            for (int i = 0; i < npl.getDeclaredPrefixes().length; i++) {
                String prefix = npl.getDeclaredPrefixes()[i];
                String namespace = npl.getNamespaceURI(prefix);
                if (namespace.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
                    if (prefix.equals("")) {
                        prefix = "xsd";
                    }
                }
                prefixmap.add(prefix, namespace);
            }
        }
        xsd.setNamespaceContext(prefixmap);
    }
    return xsd;
}
 
Example #6
Source File: AbstractAegisTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected XmlSchema newXmlSchema(String targetNamespace) {
    XmlSchema s = new XmlSchema();
    s.setTargetNamespace(targetNamespace);
    NamespaceMap xmlsNamespaceMap = new NamespaceMap();
    s.setNamespaceContext(xmlsNamespaceMap);

    // tns: is conventional, and besides we have unit tests that are hardcoded to it.
    xmlsNamespaceMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, targetNamespace);

    // ditto for xsd: instead of just namespace= for the schema schema.
    xmlsNamespaceMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
    return s;
}
 
Example #7
Source File: JAXBSchemaInitializer.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void createBridgeXsElement(MessagePartInfo part, QName qn, QName typeName) {
    XmlSchemaElement el = null;
    SchemaInfo schemaInfo = serviceInfo.getSchema(qn.getNamespaceURI());
    if (schemaInfo != null) {
        el = schemaInfo.getElementByQName(qn);
        if (el == null) {
            createXsElement(schemaInfo.getSchema(), part, typeName, schemaInfo);

        } else if (!typeName.equals(el.getSchemaTypeName())) {
            throw new Fault(new Message("CANNOT_CREATE_ELEMENT", LOG,
                                        qn, typeName, el.getSchemaTypeName()));
        }
        return;
    }

    XmlSchema schema = schemas.newXmlSchemaInCollection(qn.getNamespaceURI());
    if (qualifiedSchemas) {
        schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
    }
    schemaInfo = new SchemaInfo(qn.getNamespaceURI(), qualifiedSchemas, false);
    schemaInfo.setSchema(schema);

    el = createXsElement(schema, part, typeName, schemaInfo);

    NamespaceMap nsMap = new NamespaceMap();
    nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, schema.getTargetNamespace());
    nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
    schema.setNamespaceContext(nsMap);

    serviceInfo.addSchema(schemaInfo);
}
 
Example #8
Source File: SchemaCollection.java    From cxf with Apache License 2.0 5 votes vote down vote up
public SchemaCollection(XmlSchemaCollection col) {
    schemaCollection = col;
    if (schemaCollection.getNamespaceContext() == null) {
        // an empty prefix map avoids extra checks for null.
        schemaCollection.setNamespaceContext(new NamespaceMap());
    }
}
 
Example #9
Source File: ImportRepairTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private XmlSchema newSchema(String uri) {
    XmlSchema schema = collection.newXmlSchemaInCollection(uri);
    NamespaceMap map = new NamespaceMap();
    map.add("", XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schema.setNamespaceContext(map);
    return schema;
}
 
Example #10
Source File: ReflectionServiceFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
private SchemaInfo getOrCreateSchema(ServiceInfo serviceInfo, String namespaceURI, boolean qualified) {
    for (SchemaInfo s : serviceInfo.getSchemas()) {
        if (s.getNamespaceURI().equals(namespaceURI)) {
            return s;
        }
    }

    SchemaInfo schemaInfo = new SchemaInfo(namespaceURI);
    SchemaCollection col = serviceInfo.getXmlSchemaCollection();
    XmlSchema schema = col.getSchemaByTargetNamespace(namespaceURI);

    if (schema != null) {
        schemaInfo.setSchema(schema);
        serviceInfo.addSchema(schemaInfo);
        return schemaInfo;
    }

    schema = col.newXmlSchemaInCollection(namespaceURI);
    if (qualified) {
        schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
    }
    schemaInfo.setSchema(schema);

    Map<String, String> explicitNamespaceMappings = this.getDataBinding().getDeclaredNamespaceMappings();
    if (explicitNamespaceMappings == null) {
        explicitNamespaceMappings = Collections.emptyMap();
    }
    NamespaceMap nsMap = new NamespaceMap();
    for (Map.Entry<String, String> mapping : explicitNamespaceMappings.entrySet()) {
        nsMap.add(mapping.getValue(), mapping.getKey());
    }

    if (!explicitNamespaceMappings.containsKey(WSDLConstants.NS_SCHEMA_XSD)) {
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
    }
    if (!explicitNamespaceMappings.containsKey(serviceInfo.getTargetNamespace())) {
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, serviceInfo.getTargetNamespace());
    }
    schema.setNamespaceContext(nsMap);
    serviceInfo.addSchema(schemaInfo);
    return schemaInfo;
}
 
Example #11
Source File: SchemaInfo.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Build and return a DOM tree for this schema.
 * @return a DOM Element representation of the schema
 */
public synchronized Element getElement() {
    // if someone recently used this DOM tree, take advantage.
    Element element = cachedElement == null ? null : cachedElement.get();
    if (element != null) {
        return element;
    }
    if (getSchema() == null) {
        throw new RuntimeException("No XmlSchema in SchemaInfo");
    }

    XmlSchema sch = getSchema();
    synchronized (sch) {
        XmlSchema schAgain = getSchema();
        // XML Schema blows up when the context is null as opposed to empty.
        // Some unit tests really want to see 'tns:'.
        if (schAgain.getNamespaceContext() == null) {
            NamespaceMap nsMap = new NamespaceMap();
            nsMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
            nsMap.add("tns", schAgain.getTargetNamespace());
            schAgain.setNamespaceContext(nsMap);
        }
        Document serializedSchema;
        try {
            serializedSchema = schAgain.getSchemaDocument();
        } catch (XmlSchemaSerializerException e) {
            throw new RuntimeException("Error serializing Xml Schema", e);
        }
        element = serializedSchema.getDocumentElement();
        cachedElement = new SoftReference<>(element);
    }
    // A problem can occur with the ibm jdk when the XmlSchema
    // object is serialized. The xmlns declaration gets incorrectly
    // set to the same value as the targetNamespace attribute.
    // The aegis databinding tests demonstrate this particularly.
    if (element.getPrefix() == null
        && !Constants.URI_2001_SCHEMA_XSD.equals(element.getAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI,
                                                                        Constants.XMLNS_ATTRIBUTE))) {

        Attr attr = element.getOwnerDocument()
            .createAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE);
        attr.setValue(Constants.URI_2001_SCHEMA_XSD);
        element.setAttributeNodeNS(attr);
    }
    return element;
}