org.apache.ws.commons.schema.XmlSchemaAttribute Java Examples

The following examples show how to use org.apache.ws.commons.schema.XmlSchemaAttribute. 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: Xsd2UmlConvert.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
private static final List<Attribute> parseFields(final XmlSchemaComplexType schemaComplexType,
        final XmlSchema schema, final Xsd2UmlConfig context) {
    final List<Attribute> attributes = new LinkedList<Attribute>();
    
    final XmlSchemaObjectCollection schemaItems = schemaComplexType.getAttributes();
    for (int i = 0, count = schemaItems.getCount(); i < count; i++) {
        final XmlSchemaObject schemaObject = schemaItems.getItem(i);
        if (schemaObject instanceof XmlSchemaAttribute) {
            final XmlSchemaAttribute schemaAttribute = (XmlSchemaAttribute) schemaObject;
            attributes.add(parseAttribute(schemaAttribute, schema, context));
            
        } else {
            throw new AssertionError(schemaObject);
        }
    }
    // parseAttributes(schemaComplexType.getAttributes(), schema);
    attributes.addAll(parseParticle(schemaComplexType.getParticle(), schema, context));
    
    return Collections.unmodifiableList(attributes);
}
 
Example #2
Source File: JavascriptUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * If the object is an attribute or an anyAttribute,
 * return the 'Annotated'. If it's not one of those, or it's a group,
 * throw. We're not ready for groups yet.
 * @param object
 */
public static XmlSchemaAnnotated getObjectAnnotated(XmlSchemaObject object, QName contextName) {

    if (!(object instanceof XmlSchemaAnnotated)) {
        unsupportedConstruct("NON_ANNOTATED_ATTRIBUTE",
                                            object.getClass().getSimpleName(),
                                            contextName, object);
    }
    if (!(object instanceof XmlSchemaAttribute)
        && !(object instanceof XmlSchemaAnyAttribute)) {
        unsupportedConstruct("EXOTIC_ATTRIBUTE",
                                            object.getClass().getSimpleName(), contextName,
                                            object);
    }

    return (XmlSchemaAnnotated) object;
}
 
Example #3
Source File: Xsd2UmlConvert.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
private static final Attribute parseAttribute(final XmlSchemaAttribute attribute, final XmlSchema schema,
        final Xsd2UmlConfig config) {
    
    final String name = config.getPlugin().nameFromSchemaAttributeName(attribute.getQName());
    final List<TaggedValue> taggedValues = new LinkedList<TaggedValue>();
    taggedValues.addAll(annotations(attribute, config));
    
    final XmlSchemaSimpleType simpleType = attribute.getSchemaType();
    final Identifier type;
    if (simpleType != null) {
        type = config.ensureId(getSimpleTypeName(simpleType));
    } else {
        type = config.ensureId(attribute.getSchemaTypeName());
    }
    final Multiplicity multiplicity = multiplicity(range(Occurs.ONE, Occurs.ONE));
    return new Attribute(Identifier.random(), name, type, multiplicity, taggedValues);
}
 
Example #4
Source File: XmlSchemaUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * due to a bug, feature, or just plain oddity of JAXB, it isn't good enough
 * to just check the form of an element and of its schema. If schema 'a'
 * (default unqualified) has a complex type with an element with a ref= to
 * schema (b) (default unqualified), JAXB seems to expect to see a
 * qualifier, anyway. <br/> So, if the element is local to a complex type,
 * all we care about is the default element form of the schema and the local
 * form of the element. <br/> If, on the other hand, the element is global,
 * we might need to compare namespaces. <br/>
 *
 * @param attribute the attribute
 * @param global if this element is a global element (complex type ref= to
 *                it, or in a part)
 * @param localSchema the schema of the complex type containing the
 *                reference, only used for the 'odd case'.
 * @param attributeSchema the schema for the element.
 * @return if the element needs to be qualified.
 */
public static boolean isAttributeQualified(XmlSchemaAttribute attribute,
                                         boolean global,
                                         XmlSchema localSchema,
                                         XmlSchema attributeSchema) {
    if (attribute.getQName() == null) {
        throw new RuntimeException("getSchemaQualifier on anonymous element.");
    }
    if (attribute.isRef()) {
        throw new RuntimeException("getSchemaQualified on the 'from' side of ref=.");
    }


    if (global) {
        return isAttributeNameQualified(attribute, attributeSchema)
            || (localSchema != null
                && !(attribute.getQName().getNamespaceURI().equals(localSchema.getTargetNamespace())));
    }
    return isAttributeNameQualified(attribute, attributeSchema);
}
 
Example #5
Source File: AttributeInfo.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static void factorySetupType(XmlSchemaAttribute element, SchemaCollection schemaCollection,
                                     AttributeInfo attributeInfo) {
    attributeInfo.type = element.getSchemaType();
    if (attributeInfo.type == null) {
        if (element.getSchemaTypeName().equals(Constants.XSD_ANYTYPE)) {
            attributeInfo.anyType = true;
        } else {
            attributeInfo.type = schemaCollection.getTypeByQName(element.getSchemaTypeName());
            if (attributeInfo.type == null
                && !element.getSchemaTypeName()
                        .getNamespaceURI().equals(Constants.URI_2001_SCHEMA_XSD)) {
                JavascriptUtils.unsupportedConstruct("MISSING_TYPE", element.getSchemaTypeName()
                        .toString(), element.getQName(), element);
            }
        }
    } else if (attributeInfo.type.getQName() != null
        && Constants.XSD_ANYTYPE.equals(attributeInfo.type.getQName())) {
        attributeInfo.anyType = true;
    }
}
 
Example #6
Source File: WSDLToCorbaHelper.java    From cxf with Apache License 2.0 5 votes vote down vote up
private boolean getAttributeQualification(XmlSchemaAttribute attr, String uri) {
    QName schemaName = attr.getQName();

    boolean qualified = false;
    if (attr.getForm() == XmlSchemaForm.QUALIFIED) {
        qualified = true;
    } else {
        qualified = WSDLUtils.isElementFormQualified(xmlSchemaList, schemaName);
    }
    return qualified;
}
 
Example #7
Source File: SmooksGenerator.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private void addComplexTypeData(XmlSchemaComplexType schemaType, ComplexTypeData complexTypeData) {
    XmlSchemaObjectCollection attributes = schemaType.getAttributes();
    int numElements = attributes.getCount();
    parseParticleForElements(extractParticle(schemaType), complexTypeData);

    // Iterate XML Schema items
    for (int i = 0; i < numElements; i++) {
        XmlSchemaAttribute schemaAttribute = (XmlSchemaAttribute) attributes.getItem(i);
        complexTypeData.getAttributes().add(schemaAttribute.getName());
    }
}
 
Example #8
Source File: SchemaCollection.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void addCrossImportsAttributeList(XmlSchema schema, List<XmlSchemaAttributeOrGroupRef> list) {
    for (XmlSchemaAttributeOrGroupRef attr : list) {
        final QName ref;
        if (attr instanceof XmlSchemaAttribute) {
            ref = ((XmlSchemaAttribute)attr).getRef().getTargetQName();
        } else {
            XmlSchemaAttributeGroupRef groupRef = (XmlSchemaAttributeGroupRef)attr;
            ref = groupRef.getRef().getTargetQName();
        }

        if (ref != null) {
            XmlSchemaUtils.addImportIfNeeded(schema, ref);
        }
    }
}
 
Example #9
Source File: SchemaCollection.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void addOneSchemaCrossImports(XmlSchema schema) {
    /*
     * We need to visit all the top-level items.
     */
    for (XmlSchemaElement element : schema.getElements().values()) {
        addElementCrossImportsElement(schema, element);
    }
    for (XmlSchemaAttribute attribute : schema.getAttributes().values()) {
        XmlSchemaUtils.addImportIfNeeded(schema, attribute.getRef().getTargetQName());
        XmlSchemaUtils.addImportIfNeeded(schema, attribute.getSchemaTypeName());
    }
    for (XmlSchemaType type : schema.getSchemaTypes().values()) {
        addCrossImportsType(schema, type);
    }
}
 
Example #10
Source File: XmlSchemaUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static boolean isAttributeNameQualified(XmlSchemaAttribute attribute, XmlSchema schema) {
    if (attribute.isRef()) {
        throw new RuntimeException("isElementNameQualified on element with ref=");
    }
    if (attribute.getForm().equals(XmlSchemaForm.QUALIFIED)) {
        return true;
    }
    if (attribute.getForm().equals(XmlSchemaForm.UNQUALIFIED)) {
        return false;
    }
    return schema.getAttributeFormDefault().equals(XmlSchemaForm.QUALIFIED);
}
 
Example #11
Source File: AttributeInfo.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static void factoryCommon(XmlSchemaAnnotated annotated, XmlSchema currentSchema,
                                  SchemaCollection schemaCollection,
                                  NamespacePrefixAccumulator prefixAccumulator,
                                  AttributeInfo attributeInfo) {

    if (annotated instanceof XmlSchemaAttribute) {
        XmlSchemaAttribute attribute = (XmlSchemaAttribute)annotated;
        String attributeNamespaceURI = attribute.getQName().getNamespaceURI();
        boolean attributeNoNamespace = "".equals(attributeNamespaceURI);

        XmlSchema attributeSchema = null;
        if (!attributeNoNamespace) {
            attributeSchema = schemaCollection.getSchemaByTargetNamespace(attributeNamespaceURI);
            if (attributeSchema == null) {
                throw new RuntimeException("Missing schema " + attributeNamespaceURI);
            }
        }

        boolean qualified = !attributeNoNamespace
                            && XmlSchemaUtils.isAttributeQualified(attribute, true, currentSchema,
                                                                 attributeSchema);
        attributeInfo.xmlName = prefixAccumulator.xmlAttributeString(attribute, qualified);
        // we are assuming here that we are not dealing, in close proximity,
        // with elements with identical local names and different
        // namespaces.
        attributeInfo.javascriptName = attribute.getQName().getLocalPart();
        attributeInfo.defaultValue = attribute.getDefaultValue();
        attributeInfo.fixedValue = attribute.getFixedValue();
        attributeInfo.use = attribute.getUse();
        factorySetupType(attribute, schemaCollection, attributeInfo);
    } else { // any
        attributeInfo.any = true;
        attributeInfo.xmlName = null; // unknown until runtime.
        attributeInfo.javascriptName = "any";
        attributeInfo.type = null; // runtime for any.
        attributeInfo.use = XmlSchemaUse.OPTIONAL;
    }
}
 
Example #12
Source File: AttributeInfo.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Fill in an AttributeInfo for an attribute or anyAttribute from a sequence.
 *
 * @param sequenceObject
 * @param currentSchema
 * @param schemaCollection
 * @param prefixAccumulator
 * @param contextName
 * @return
 */
public static AttributeInfo forLocalItem(XmlSchemaObject sequenceObject,
                                         XmlSchema currentSchema,
                                        SchemaCollection schemaCollection,
                                        NamespacePrefixAccumulator prefixAccumulator, QName contextName) {
    XmlSchemaAnnotated annotated = JavascriptUtils.getObjectAnnotated(sequenceObject, contextName);
    AttributeInfo attributeInfo = new AttributeInfo();
    XmlSchemaAnnotated realAnnotated = annotated;

    if (annotated instanceof XmlSchemaAttribute) {
        XmlSchemaAttribute attribute = (XmlSchemaAttribute)annotated;
        attributeInfo.use = attribute.getUse();

        if (attribute.getRef().getTarget() != null) {
            realAnnotated = attribute.getRef().getTarget();
            attributeInfo.global = true;
        }
    } else if (annotated instanceof XmlSchemaAnyAttribute) {
        attributeInfo.any = true;
        attributeInfo.xmlName = null; // unknown until runtime.
        attributeInfo.javascriptName = "any";
        attributeInfo.type = null; // runtime for any.
        attributeInfo.use = XmlSchemaUse.OPTIONAL;
    } else {
        throw new UnsupportedConstruct(LOG, "UNSUPPORTED_ATTRIBUTE_ITEM", annotated, contextName);
    }

    factoryCommon(realAnnotated, currentSchema, schemaCollection, prefixAccumulator, attributeInfo);

    attributeInfo.annotated = realAnnotated;

    return attributeInfo;
}
 
Example #13
Source File: AttributeInfo.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Create an elementInfo that stores information about a global, named,
 * element.
 *
 * @param attribute the element
 * @param currentSchema the schema it came from.
 * @param schemaCollection the collection of all schemas.
 * @param prefixAccumulator the accumulator that assigns prefixes.
 * @return
 */
public static AttributeInfo forGlobalAttribute(XmlSchemaAttribute attribute, XmlSchema currentSchema,
                                               SchemaCollection schemaCollection,
                                               NamespacePrefixAccumulator prefixAccumulator) {
    AttributeInfo attributeInfo = new AttributeInfo();
    attributeInfo.annotated = attribute;
    attributeInfo.global = true;

    factoryCommon(attribute, currentSchema, schemaCollection, prefixAccumulator, attributeInfo);
    return attributeInfo;
}
 
Example #14
Source File: NamespacePrefixAccumulator.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain a suitable name for use in Javascript for an attribute. This function
 * is purely a tribute to the awful modularity of XmlSchema.
 * @param attribute
 * @param qualified
 * @return
 */
public String xmlAttributeString(XmlSchemaAttribute attribute, boolean qualified) {
    if (qualified) {
        // What if there were a prefix in the element's qname? This is not apparently
        // something that happens in this environment.
        String prefix = getPrefix(attribute.getQName().getNamespaceURI());
        collect(prefix, attribute.getQName().getNamespaceURI());
        return prefix + ":" + attribute.getName();
    }
    return attribute.getName(); // use the non-qualified name.
}
 
Example #15
Source File: XmlSchemaExtractor.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private void handleAttribute(XmlSchemaAttribute target, XmlSchemaAttribute source) throws ParserException {
    // set use if not top-level
    if (!source.isTopLevel()) {
        target.setUse(source.getUse());
    }
    handleTypeNameAndType(source.getSchemaTypeName(), source.getSchemaType(),
            target::setSchemaTypeName, target::setSchemaType);
}
 
Example #16
Source File: SchemaCollection.java    From cxf with Apache License 2.0 4 votes vote down vote up
public XmlSchemaAttribute getAttributeByQName(QName qname) {
    return schemaCollection.getAttributeByQName(qname);
}
 
Example #17
Source File: ImportRepairTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testImportRepairs() throws Exception {
    if (System.getProperty("java.vendor").contains("IBM")) {
        //the version of xerces built into IBM jdk won't work
        //and we cannot get a good version unless we endorse it
        return;
    }

    collection = new SchemaCollection();
    XmlSchema importingSchema = newSchema(IMPORTING_SCHEMA);
    XmlSchema baseTypeSchema1 = newSchema(BASE_TYPE_SCHEMA1);
    XmlSchema baseTypeSchema2 = newSchema(BASE_TYPE_SCHEMA2);
    XmlSchema elementTypeSchema = newSchema(ELEMENT_TYPE_SCHEMA);
    XmlSchema elementSchema = newSchema(ELEMENT_SCHEMA);
    XmlSchema attributeSchema = newSchema(ATTRIBUTE_SCHEMA);
    XmlSchema attributeTypeSchema = newSchema(ATTRIBUTE_TYPE_SCHEMA);

    createBaseType1(baseTypeSchema1);

    createBaseType2(baseTypeSchema2);
    XmlSchemaComplexContentExtension derivedType1Extension = createDerivedType1(importingSchema);
    createDerivedType2(importingSchema);

    createImportedElement(elementSchema);

    createTypeImportingElement(importingSchema);

    createTypeImportedByElement(elementTypeSchema);

    createElementWithImportedType(importingSchema);

    createImportedAttribute(attributeSchema);

    XmlSchemaAttribute importingAttribute = new XmlSchemaAttribute(importingSchema, false);
    importingAttribute.getRef().setTargetQName(new QName(ATTRIBUTE_SCHEMA, "imported"));
    // borrow derivedType1 to make the reference.
    derivedType1Extension.getAttributes().add(importingAttribute);

    createImportedAttributeType(attributeTypeSchema);

    createAttributeImportingType(importingSchema);


    /*
     * Notice that no imports have been added. In an ideal world, XmlSchema would do this for us.
     */
    try {
        tryToParseSchemas();
        fail("Expected an exception");
    } catch (DOMErrorException e) {
        //ignore, expected
    }
    LOG.info("adding imports");
    collection.addCrossImports();
    tryToParseSchemas();
}
 
Example #18
Source File: ImportRepairTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void createAttributeImportingType(XmlSchema importingSchema) {
    XmlSchemaAttribute attributeImportingType = new XmlSchemaAttribute(importingSchema, true);
    attributeImportingType.setName("importingType");
    attributeImportingType.setSchemaTypeName(new QName(ATTRIBUTE_TYPE_SCHEMA, "importedAttributeType"));
}
 
Example #19
Source File: ImportRepairTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void createImportedAttribute(XmlSchema attributeSchema) {
    XmlSchemaAttribute importedAttribute = new XmlSchemaAttribute(attributeSchema, true);
    importedAttribute.setName("imported");
    importedAttribute.setSchemaTypeName(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "string"));
}
 
Example #20
Source File: CommonsSchemaInfoBuilder.java    From tomee with Apache License 2.0 4 votes vote down vote up
public static XmlTypeInfo createXmlTypeInfo(QName qname, XmlSchemaType type) {
    if (qname == null) throw new NullPointerException("qname is null");
    if (type == null) throw new NullPointerException("type is null");

    XmlTypeInfo typeInfo = new XmlTypeInfo();
    typeInfo.qname = qname;
    typeInfo.anonymous = qname.getLocalPart().indexOf('>') >= 0;

    if (type instanceof XmlSchemaSimpleType) {
        XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) type;
        XmlSchemaSimpleTypeContent content = simpleType.getContent();
        if (content instanceof XmlSchemaSimpleTypeList) {
            XmlSchemaSimpleTypeList list = (XmlSchemaSimpleTypeList) content;
            typeInfo.simpleBaseType = list.getItemType().getQName();

            // this is a list
            typeInfo.listType = true;
        } else if (content instanceof XmlSchemaSimpleTypeRestriction) {
            XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction) content;
            typeInfo.simpleBaseType = restriction.getBaseTypeName();

            // is this an enumeration?
            for (Iterator iterator = restriction.getFacets().getIterator(); iterator.hasNext(); ) {
                if (iterator.next() instanceof XmlSchemaEnumerationFacet) {
                    typeInfo.enumType = true;
                    break;
                }

            }
        }
    } else if (type instanceof XmlSchemaComplexType) {
        XmlSchemaComplexType complexType = (XmlSchemaComplexType) type;

        // SOAP array component type
        typeInfo.arrayComponentType = extractSoapArrayComponentType(complexType);

        // process attributes (skip soap arrays which have non-mappable attributes)
        if (!isSoapArray(complexType)) {
            XmlSchemaObjectCollection attributes = complexType.getAttributes();
            for (Iterator iterator = attributes.getIterator(); iterator.hasNext(); ) {
                Object item = iterator.next();
                if (item instanceof XmlSchemaAttribute) {
                    XmlSchemaAttribute attribute = (XmlSchemaAttribute) item;
                    Object old = typeInfo.attributes.put(attribute.getQName().getLocalPart(), attribute.getSchemaTypeName());
                    if (old != null) {
                        throw new IllegalArgumentException("Complain to your expert group member, spec does not support attributes with the same local name and differing namespaces: original: " + old + ", duplicate local name: " + attribute);
                    }
                }
            }
        }
    } else {
        LOG.warn("Unknown schema type class " + typeInfo.getClass().getName());
    }

    return typeInfo;
}
 
Example #21
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 #22
Source File: XsdToNeutralSchemaRepo.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
private void parseAttributes(XmlSchemaObjectCollection attributes, NeutralSchema complexSchema, XmlSchema schema) {

        if (attributes != null) {
            for (int i = 0; i < attributes.getCount(); i++) {
                XmlSchemaAttribute attribute = (XmlSchemaAttribute) attributes.getItem(i);
                QName attributeTypeName = attribute.getSchemaTypeName();

                XmlSchemaType attributeSchemaType = attribute.getSchemaType();

                if (attribute.getName() != null) {

                    String attributeName = attribute.getName();

                    // Derive Attribute Schema
                    NeutralSchema attributeSchema = null;
                    if (attributeSchemaType != null) {
                        attributeSchema = parse(attributeSchemaType, schema);
                    } else if (attributeTypeName != null) {
                        attributeSchema = getSchemaFactory().createSchema(attributeTypeName);
                    }

                    // Update Neutral Schema Field
                    if (attributeSchema != null) {

                        // Optional Attributes
                        if (attribute.getUse().equals(REQUIRED_USE)) {

                            AppInfo info = attributeSchema.getAppInfo();

                            if (info == null) {
                                info = new AppInfo(null);
                            }

                            info.put(REQUIRED_USE.getValue(), "true");
                            attributeSchema.addAnnotation(info);
                        }

                        complexSchema.addField(attributeName, attributeSchema);
                    }
                }
            }
        }
    }