Java Code Examples for org.apache.ws.commons.schema.XmlSchema#getTypeByName()

The following examples show how to use org.apache.ws.commons.schema.XmlSchema#getTypeByName() . 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: TypesUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static Scope generateAnonymousScopedName(Scope scope, XmlSchema schema) {
    Scope scopedName = null;
    XmlSchemaType anonSchemaType = null;
    int id = 0;
    do {
        id++;
        StringBuilder name = new StringBuilder();
        name.append('_');
        name.append("Anon").append(Integer.toString(id));
        name.append('_');
        name.append(scope.tail());
        scopedName = new Scope(scope.getParent(), name.toString());
        QName scopedQName = new QName(schema.getTargetNamespace(), scopedName.toString());
        anonSchemaType = schema.getTypeByName(scopedQName);
    } while (anonSchemaType != null);

    return scopedName;
}
 
Example 2
Source File: WSDLToCorbaHelper.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected XmlSchemaType lookUpType(Part part) {
    XmlSchemaType schemaType = null;
    for (XmlSchema xmlSchema : xmlSchemaList.getXmlSchemas()) {
        if (part.getElementName() != null) {
            XmlSchemaElement schemaElement = xmlSchema.getElementByName(part.getElementName());
            if (schemaElement != null) {
                schemaType = schemaElement.getSchemaType();
            }
        } else {
            if (part.getTypeName() != null) {
                schemaType = xmlSchema.getTypeByName(part.getTypeName());
            }
        }
        if (schemaType != null) {
            return schemaType;
        }
    }

    return schemaType;
}
 
Example 3
Source File: WSDLToCorbaHelper.java    From cxf with Apache License 2.0 6 votes vote down vote up
private XmlSchemaType findTypeInSchema(XmlSchema xmlSchema, QName typeName) {
    XmlSchemaType schemaType = null;

    if (xmlSchema.getElementByName(typeName) != null) {
        XmlSchemaElement schemaElement = xmlSchema.getElementByName(typeName);
        schemaType = schemaElement.getSchemaType();
    } else if (xmlSchema.getTypeByName(typeName) != null) {
        schemaType = xmlSchema.getTypeByName(typeName);
    }
    if (schemaType != null) {
        return schemaType;
    }
    for (XmlSchemaExternal extSchema : xmlSchema.getExternals()) {
        if (!(extSchema instanceof XmlSchemaImport)) {
            schemaType = findTypeInSchema(extSchema.getSchema(), typeName);
            if (schemaType != null) {
                return schemaType;
            }
        }
    }

    return null;
}
 
Example 4
Source File: ScopedNameVisitor.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected static void populateAliasSchemaType(CorbaType corbaType,
                                              WSDLASTVisitor wsdlVisitor,
                                              VisitorTypeHolder holder) {
    XmlSchemaCollection schemas = wsdlVisitor.getSchemas();
    TypeMappingType typeMap = wsdlVisitor.getTypeMap();
    holder.setCorbaType(corbaType);
    Alias alias = (Alias) corbaType;
    //loop through alias base types, till you get a non-alias corba type
    CorbaType type = findCorbaType(typeMap, alias.getBasetype());
    while ((type != null) && (type instanceof Alias)) {
        alias = (Alias) type;
        type = findCorbaType(typeMap, alias.getBasetype());
    }
    QName tname;
    if (type == null) {
        //it must be a primitive type
        tname = xmlSchemaPrimitiveMap.get(alias.getBasetype());
    } else {
        tname = type.getType();
    }
    XmlSchemaType stype = schemas.getTypeByQName(tname);
    if (stype == null) {
        XmlSchema xmlSchema = wsdlVisitor.getManager().getXmlSchema(tname.getNamespaceURI());
        if (xmlSchema != null) {
            stype = xmlSchema.getTypeByName(tname);
        } else {
            stype = wsdlVisitor.getSchema().getTypeByName(tname);
        }
    }
    holder.setSchemaType(stype);
}
 
Example 5
Source File: WSDLParameter.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static XmlSchemaType findSchemaType(XmlSchema xmlSchema, QName typeName) {

        XmlSchemaType schemaType = xmlSchema.getTypeByName(typeName);

        // Endpoint reference types will give a null schemaType
        // here, so we need to
        // go through the list of imports to find the definition for
        // an Endpoint
        // reference type.

        if (schemaType == null) {
            for (XmlSchemaExternal ext : xmlSchema.getExternals()) {
                if (ext instanceof XmlSchemaImport) {
                    XmlSchemaImport xmlImport = (XmlSchemaImport)ext;
                    if (xmlImport.getNamespace().equals(typeName.getNamespaceURI())) {
                        XmlSchema importSchema = xmlImport.getSchema();
                        schemaType = importSchema.getTypeByName(typeName);
                    } else {
                        schemaType = findSchemaType(ext.getSchema(), typeName);
                        if (schemaType != null) {
                            return schemaType;
                        }
                    }
                }
            }
            if (schemaType != null) {
                return schemaType;
            }
        }
        return schemaType;
    }
 
Example 6
Source File: BeanTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNillableInt() throws Exception {
    defaultContext();
    BeanTypeInfo info = new BeanTypeInfo(IntBean.class, "urn:Bean");
    info.setTypeMapping(mapping);

    BeanType type = new BeanType(info);
    type.setTypeClass(IntBean.class);
    type.setTypeMapping(mapping);
    type.setSchemaType(new QName("urn:Bean", "bean"));

    XmlSchema schema = newXmlSchema("urn:Bean");
    type.writeSchema(schema);

    XmlSchemaComplexType btype = (XmlSchemaComplexType)schema.getTypeByName("bean");
    XmlSchemaSequence seq = (XmlSchemaSequence)btype.getParticle();
    boolean int1ok = false;
    boolean int2ok = 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 ("int1".equals(oe.getName())) {
                int1ok = true;
                assertTrue(oe.isNillable());
                assertEquals(0, oe.getMinOccurs());
            } else if ("int2".equals(oe.getName())) {
                int2ok = true;
                assertEquals(0, oe.getMinOccurs());
                assertFalse(oe.isNillable());
            }
        }
    }
    assertTrue(int1ok);
    assertTrue(int2ok);
}
 
Example 7
Source File: BeanTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNillableAnnotation() throws Exception {
    context = new AegisContext();
    TypeCreationOptions config = new TypeCreationOptions();
    config.setDefaultNillable(false);
    config.setDefaultMinOccurs(1);
    context.setTypeCreationOptions(config);
    context.initialize();
    mapping = context.getTypeMapping();

    BeanType type = (BeanType)mapping.getTypeCreator().createType(BeanWithNillableItem.class);
    type.setTypeClass(BeanWithNillableItem.class);
    type.setTypeMapping(mapping);

    XmlSchema schema = newXmlSchema("urn:Bean");
    type.writeSchema(schema);

    XmlSchemaComplexType btype = (XmlSchemaComplexType)schema.getTypeByName("BeanWithNillableItem");
    XmlSchemaSequence seq = (XmlSchemaSequence)btype.getParticle();
    boolean itemFound = false;
    boolean itemNotNillableFound = 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 ("item".equals(oe.getName())) {
                itemFound = true;
                assertTrue(oe.isNillable());
                assertEquals(0, oe.getMinOccurs());
            } else if ("itemNotNillable".equals(oe.getName())) {
                itemNotNillableFound = true;
                assertFalse(oe.isNillable());
            }
        }
    }
    assertTrue(itemFound);
    assertTrue(itemNotNillableFound);
}
 
Example 8
Source File: BeanTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNillableIntMinOccurs1() throws Exception {
    context = new AegisContext();
    TypeCreationOptions config = new TypeCreationOptions();
    config.setDefaultMinOccurs(1);
    config.setDefaultNillable(false);
    context.setTypeCreationOptions(config);
    context.initialize();
    mapping = context.getTypeMapping();

    BeanType type = (BeanType)mapping.getTypeCreator().createType(IntBean.class);
    type.setTypeClass(IntBean.class);
    type.setTypeMapping(mapping);

    XmlSchema schema = newXmlSchema("urn:Bean");
    type.writeSchema(schema);

    XmlSchemaComplexType btype = (XmlSchemaComplexType)schema.getTypeByName("IntBean");
    XmlSchemaSequence seq = (XmlSchemaSequence)btype.getParticle();
    boolean int1ok = 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 ("int1".equals(oe.getName())) {
                int1ok = true;
                assertFalse(oe.isNillable());
                assertEquals(1, oe.getMinOccurs());
            }
        }
    }
    assertTrue(int1ok);
}
 
Example 9
Source File: BeanTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testCharMappings() 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 charok = 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 ("character".equals(oe.getName())) {
                charok = true;
                assertNotNull(oe.getSchemaTypeName());
                assertTrue(oe.isNillable());
                assertEquals(CharacterAsStringType.CHARACTER_AS_STRING_TYPE_QNAME,
                             oe.getSchemaTypeName());
            }
        }
    }
    assertTrue(charok);
}
 
Example 10
Source File: ScopedNameVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static boolean findSchemaTypeInGlobalScope(Scope scope,
                                                   Definition defn,
                                                   XmlSchema currentSchema,
                                                   AST node,
                                                   WSDLASTVisitor wsdlVisitor,
                                                   VisitorTypeHolder holder) {
    XmlSchemaCollection schemas = wsdlVisitor.getSchemas();
    TypeMappingType typeMap = wsdlVisitor.getTypeMap();
    ModuleToNSMapper mapper = wsdlVisitor.getModuleToNSMapper();
    WSDLSchemaManager manager = wsdlVisitor.getManager();

    Scope scopedName = new Scope(scope, node);
    String name = node.toString();
    if (isFullyScopedName(node)) {
        scopedName = getFullyScopedName(new Scope(), node);
        name = scopedName.toString();
    }
    boolean result = findNonSchemaType(name, wsdlVisitor, holder);
    if (!result) {
        XmlSchema xmlSchema = currentSchema;
        QName qname = null;
        String tns = mapper.map(scopedName.getParent());
        if (tns != null) {
            xmlSchema = manager.getXmlSchema(tns);
            if (xmlSchema != null) {
                qname = new QName(xmlSchema.getTargetNamespace(), scopedName.tail());
            }
        } else {
            qname = new QName(xmlSchema.getTargetNamespace(), name);
        }
        XmlSchemaType stype = null;
        if (qname != null) {
            // Exceptions are treated as a special case as above
            if (exceptionMode) {
                qname = new QName(xmlSchema.getTargetNamespace(), qname.getLocalPart() + "Type");
            }
            stype = xmlSchema.getTypeByName(qname);
            if (stype == null) {
                stype = schemas.getTypeByQName(qname);
            }
        }
        if (stype != null) {
            result = true;
            if (holder != null) {
                holder.setSchemaType(stype);
                holder.setCorbaType(getCorbaSchemaType(xmlSchema, typeMap, stype, scopedName));
                //add a xmlschema import
                if (!currentSchema.getTargetNamespace().equals(xmlSchema.getTargetNamespace())) {
                    String importFile = wsdlVisitor.getOutputDir()
                        + System.getProperty("file.separator")
                        + scopedName.getParent().toString("_");
                    manager.addXmlSchemaImport(currentSchema, xmlSchema, importFile);
                }
            }
        }
    }
    return result;
}
 
Example 11
Source File: ScopedNameVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static boolean findScopeSchemaType(Scope scopedName, XmlSchema schemaRef,
                                       WSDLASTVisitor wsdlVisitor,
                                       VisitorTypeHolder holder) {

    XmlSchemaCollection schemas = wsdlVisitor.getSchemas();
    TypeMappingType typeMap = wsdlVisitor.getTypeMap();
    ModuleToNSMapper mapper = wsdlVisitor.getModuleToNSMapper();
    WSDLSchemaManager manager = wsdlVisitor.getManager();

    boolean result = findNonSchemaType(scopedName.toString(), wsdlVisitor, holder);
    if (!result) {
        QName qname = null;
        XmlSchema xmlSchema = schemaRef;
        String tns = wsdlVisitor.getModuleToNSMapper().map(scopedName.getParent());
        if (tns != null) {
            xmlSchema = wsdlVisitor.getManager().getXmlSchema(tns);
        }
        XmlSchemaType stype = null;
        if (xmlSchema != null) {
            // Exceptions are treated as a special case as for the
            // doc/literal style
            // in the schema we will have an element and a complextype
            // so the name
            // and the typename will be different.

            String scopedNameString = null;
            if (mapper.isDefaultMapping()) {
                scopedNameString = scopedName.toString();
            } else {
                scopedNameString = scopedName.tail();
            }

            if (exceptionMode) {
                qname = new QName(xmlSchema.getTargetNamespace(), scopedNameString + "Type");
            } else {
                qname = new QName(xmlSchema.getTargetNamespace(), scopedNameString);
            }

            stype = xmlSchema.getTypeByName(qname);
            if (stype == null) {
                stype = schemas.getTypeByQName(qname);
            }
        }
        if (stype != null) {
            result = true;
        }
        if (result && holder != null) {
            holder.setSchemaType(stype);
            holder.setCorbaType(getCorbaSchemaType(xmlSchema, typeMap, stype, scopedName));
            // add a xmlschema import
            if (!schemaRef.getTargetNamespace().equals(xmlSchema.getTargetNamespace())) {
                String importFile = wsdlVisitor.getOutputDir() + System.getProperty("file.separator")
                                    + scopedName.getParent().toString("_");
                manager.addXmlSchemaImport(schemaRef, xmlSchema, importFile);
            }
        }
    }
    return result;
}
 
Example 12
Source File: BeanTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 13
Source File: BeanTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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(Byte.valueOf((byte)0xfe));
    bean.setLittleByte((byte)0xfd);
    Element element = writeObjectToElement(type, bean, getContext());
    Byte bb = Byte.valueOf((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();

}
 
Example 14
Source File: ArrayType.java    From cxf with Apache License 2.0 2 votes vote down vote up
/**
 * Since both an Array and a List can have the same type definition, double check that there isn't already
 * a defined type already.
 *
 * @param root
 * @return
 */
private boolean hasDefinedArray(XmlSchema root) {
    return root.getTypeByName(getSchemaType().getLocalPart()) != null;
}