use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class TestDateMapping method testWriteCustomTypeSchemaType.
@Test
public void testWriteCustomTypeSchemaType() throws Exception {
context = new AegisContext();
Set<java.lang.reflect.Type> rootClasses = new HashSet<java.lang.reflect.Type>();
rootClasses.add(BeanWithDate.class);
context.setRootClasses(rootClasses);
context.initialize();
BeanWithDate bean = new BeanWithDate();
java.sql.Date date = new java.sql.Date(0);
bean.setFig(date);
AegisType sbType = context.getTypeMapping().getType(bean.getClass());
// dummy to put schema in.
XmlSchema root = new XmlSchema();
/* will explode if the type object created for the custom mapping isn't fully initialized.
*/
sbType.writeSchema(root);
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class JAXBSchemaInitializer method createBridgeXsElement.
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);
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class JAXBSchemaInitializer method buildExceptionType.
private void buildExceptionType(MessagePartInfo part, Class<?> cls) {
SchemaInfo schemaInfo = null;
for (SchemaInfo s : serviceInfo.getSchemas()) {
if (s.getNamespaceURI().equals(part.getElementQName().getNamespaceURI())) {
schemaInfo = s;
break;
}
}
XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
XmlType xmlTypeAnno = cls.getAnnotation(XmlType.class);
String[] propertyOrder = null;
boolean respectXmlTypeNS = false;
XmlSchema faultBeanSchema = null;
if (xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.namespace()) && !xmlTypeAnno.namespace().equals(part.getElementQName().getNamespaceURI())) {
respectXmlTypeNS = true;
NamespaceMap nsMap = new NamespaceMap();
nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, xmlTypeAnno.namespace());
nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
SchemaInfo faultBeanSchemaInfo = createSchemaIfNeeded(xmlTypeAnno.namespace(), nsMap);
faultBeanSchema = faultBeanSchemaInfo.getSchema();
}
if (xmlTypeAnno != null && xmlTypeAnno.propOrder().length > 0) {
propertyOrder = xmlTypeAnno.propOrder();
// TODO: handle @XmlAccessOrder
}
XmlSchema schema = null;
if (schemaInfo == null) {
NamespaceMap nsMap = new NamespaceMap();
nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, part.getElementQName().getNamespaceURI());
nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
schemaInfo = createSchemaIfNeeded(part.getElementQName().getNamespaceURI(), nsMap);
}
schema = schemaInfo.getSchema();
// Before updating everything, make sure we haven't added this
// type yet. Multiple methods that throw the same exception
// types will cause duplicates.
String faultTypeName = xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.name()) ? xmlTypeAnno.name() : part.getElementQName().getLocalPart();
XmlSchemaType existingType = schema.getTypeByName(faultTypeName);
if (existingType != null) {
return;
}
XmlSchemaElement el = new XmlSchemaElement(schema, true);
el.setName(part.getElementQName().getLocalPart());
part.setXmlSchema(el);
schemaInfo.setElement(null);
if (respectXmlTypeNS) {
// create complexType in the new created schema for xmlType
schema = faultBeanSchema;
}
XmlSchemaComplexType ct = new XmlSchemaComplexType(schema, true);
ct.setName(faultTypeName);
el.setSchemaTypeName(ct.getQName());
XmlSchemaSequence seq = new XmlSchemaSequence();
ct.setParticle(seq);
String namespace = part.getElementQName().getNamespaceURI();
XmlAccessType accessType = Utils.getXmlAccessType(cls);
//
for (Field f : Utils.getFields(cls, accessType)) {
// map field
Type type = Utils.getFieldType(f);
// generic return type.
if ((type == null) && (f.getGenericType() instanceof ParameterizedType)) {
type = f.getGenericType();
}
if (generateGenericType(type)) {
buildGenericElements(schema, seq, f);
} else {
JAXBBeanInfo beanInfo = getBeanInfo(type);
if (beanInfo != null) {
XmlElement xmlElementAnno = f.getAnnotation(XmlElement.class);
addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type), xmlElementAnno);
}
}
}
for (Method m : Utils.getGetters(cls, accessType)) {
// map method
Type type = Utils.getMethodReturnType(m);
// generic return type.
if ((type == null) && (m.getGenericReturnType() instanceof ParameterizedType)) {
type = m.getGenericReturnType();
}
if (generateGenericType(type)) {
buildGenericElements(schema, seq, m, type);
} else {
JAXBBeanInfo beanInfo = getBeanInfo(type);
if (beanInfo != null) {
int idx = m.getName().startsWith("get") ? 3 : 2;
String name = m.getName().substring(idx);
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
XmlElement xmlElementAnno = m.getAnnotation(XmlElement.class);
addElement(schema, seq, beanInfo, new QName(namespace, name), isArray(type), xmlElementAnno);
}
}
}
// Create element in xsd:sequence for Exception.class
if (Exception.class.isAssignableFrom(cls)) {
addExceptionMessage(cls, schema, seq);
}
if (propertyOrder != null) {
if (propertyOrder.length == seq.getItems().size()) {
sortItems(seq, propertyOrder);
} else if (propertyOrder.length > 1 || (propertyOrder.length == 1 && !propertyOrder[0].isEmpty())) {
LOG.log(Level.WARNING, "propOrder in @XmlType doesn't define all schema elements :" + Arrays.toString(propertyOrder));
}
}
if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL) && propertyOrder == null) {
sort(seq);
}
schemas.addCrossImports();
part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class BeanTest method testByteMappings.
@Test
public void testByteMappings() throws Exception {
defaultContext();
BeanType type = (BeanType) mapping.getTypeCreator().createType(SimpleBean.class);
type.setTypeClass(SimpleBean.class);
type.setTypeMapping(mapping);
XmlSchema schema = newXmlSchema("urn:Bean");
type.writeSchema(schema);
XmlSchemaComplexType btype = (XmlSchemaComplexType) schema.getTypeByName("SimpleBean");
XmlSchemaSequence seq = (XmlSchemaSequence) btype.getParticle();
boolean littleByteOk = false;
boolean bigByteOk = false;
for (int x = 0; x < seq.getItems().size(); x++) {
XmlSchemaSequenceMember o = seq.getItems().get(x);
if (o instanceof XmlSchemaElement) {
XmlSchemaElement oe = (XmlSchemaElement) o;
if ("littleByte".equals(oe.getName())) {
littleByteOk = true;
assertNotNull(oe.getSchemaTypeName());
assertEquals(Constants.XSD_BYTE, oe.getSchemaTypeName());
} else if ("bigByte".equals(oe.getName())) {
bigByteOk = true;
assertNotNull(oe.getSchemaTypeName());
assertEquals(Constants.XSD_BYTE, oe.getSchemaTypeName());
}
}
}
assertTrue(littleByteOk);
assertTrue(bigByteOk);
SimpleBean bean = new SimpleBean();
bean.setBigByte(new Byte((byte) 0xfe));
bean.setLittleByte((byte) 0xfd);
Element element = writeObjectToElement(type, bean, getContext());
Byte bb = new Byte((byte) 0xfe);
String bbs = bb.toString();
assertValid("/b:root/bz:bigByte[text()='" + bbs + "']", element);
// Test reading
ElementReader reader = new ElementReader(getResourceAsStream("byteBeans.xml"));
bean = (SimpleBean) type.readObject(reader, getContext());
assertEquals(-5, bean.getLittleByte());
assertEquals(25, bean.getBigByte().byteValue());
reader.getXMLStreamReader().close();
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class BeanTest method testAttributeMap.
@Test
public void testAttributeMap() throws Exception {
defaultContext();
BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, "urn:Bean");
info.mapAttribute("howdy", new QName("urn:Bean", "howdy"));
info.mapAttribute("bleh", new QName("urn:Bean", "bleh"));
info.setTypeMapping(mapping);
BeanType type = new BeanType(info);
type.setTypeClass(SimpleBean.class);
type.setTypeMapping(mapping);
type.setSchemaType(new QName("urn:Bean", "bean"));
ElementReader reader = new ElementReader(getResourceAsStream("bean4.xml"));
SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
assertEquals("bleh", bean.getBleh());
assertEquals("howdy", bean.getHowdy());
reader.getXMLStreamReader().close();
// Test writing
Element element = writeObjectToElement(type, bean, getContext());
assertValid("/b:root[@b:bleh='bleh']", element);
assertValid("/b:root[@b:howdy='howdy']", element);
XmlSchema schema = newXmlSchema("urn:Bean");
type.writeSchema(schema);
XmlSchemaComplexType stype = (XmlSchemaComplexType) schema.getTypeByName("bean");
boolean howdy = false;
boolean bleh = false;
for (int x = 0; x < stype.getAttributes().size(); x++) {
XmlSchemaObject o = stype.getAttributes().get(x);
if (o instanceof XmlSchemaAttribute) {
XmlSchemaAttribute a = (XmlSchemaAttribute) o;
if ("howdy".equals(a.getName())) {
howdy = true;
}
if ("bleh".equals(a.getName())) {
bleh = true;
}
}
}
assertTrue(howdy);
assertTrue(bleh);
}
Aggregations