Java源码示例:org.apache.ws.commons.schema.utils.XmlSchemaObjectBase

示例1
@SuppressWarnings({"unchecked", "rawtypes"})
private void handleGroupParticle(XmlSchemaGroupParticle target, XmlSchemaGroupParticle source) throws ParserException {
    final List sourceItems;
    final List targetItems;

    // unfortunately the group 'all, choice and sequence' classes don't implement a common interface
    // hence the kludgy code below
    if (source instanceof XmlSchemaAll) {
        sourceItems = ((XmlSchemaAll) source).getItems();
        targetItems = ((XmlSchemaAll) target).getItems();
    } else if (source instanceof XmlSchemaChoice) {
        sourceItems = ((XmlSchemaChoice)source).getItems();
        targetItems = ((XmlSchemaChoice)target).getItems();
    } else if (source instanceof XmlSchemaSequence) {
        sourceItems = ((XmlSchemaSequence)source).getItems();
        targetItems = ((XmlSchemaSequence)target).getItems();
    } else {
        throw new ParserException("Unsupported Group Particle type " + source.getClass().getName());
    }

    // add all source items to target schemas
    for (Object item : sourceItems) {
        targetItems.add(createXmlSchemaObjectBase((XmlSchemaObjectBase) item));
    }
}
 
示例2
/**
 * Copy all objects from source schema to target schema after calling extract methods to setup objects to extract.
 *
 * @throws ParserException on error.
 */
public void copyObjects() throws ParserException {
    while (!objectPairStack.isEmpty()) {

        // get objects in LIFO insertion order
        final ObjectPair<? extends XmlSchemaObjectBase> pair = objectPairStack.pop();
        final XmlSchemaObjectBase source = pair.getSource();
        final XmlSchemaObjectBase target = pair.getTarget();

        // namespace for new target objects created by pair handler
        final String targetNamespace;
        if (source instanceof XmlSchemaNamed) {
            targetNamespace = ((XmlSchemaNamed)source).getParent().getTargetNamespace();
        } else {
            targetNamespace = pair.getSourceSchemaNamespace();
        }

        // handle based on type
        withNamespace(targetNamespace, () ->
            HANDLER_MAP.entrySet().stream()
                    .filter(e -> e.getKey().test(source))
                    .findFirst()
                    .map(Map.Entry::getValue)
                    .orElseThrow(() -> new ParserException("Unsupported type " + source.getClass().getName()))
                    .apply(this, target, source)
        );
    }

    // resolve targetSchemas imports
    targetSchemas.addCrossImports();
}
 
示例3
private <T extends XmlSchemaObjectBase> void copyXmlSchemaObjectBase(T target, T source) throws ParserException {
    // copy non-blacklisted properties to target object
    copyNonBlackListedProperties(target, source);

    // put objects at top of stack (DFS) to be handled in copyObjects
    objectPairStack.push(new ObjectPair<>(target, source, getCurrentNamespace()));
}
 
示例4
@SuppressWarnings("unchecked")
private <T extends XmlSchemaObjectBase> T createXmlSchemaObject(T source, String targetNamespace) throws ReflectiveOperationException {
    T target;
    final Constructor<?> constructor = source.getClass().getConstructors()[0];
    if (constructor.getParameterCount() == 0) {
        target = (T) constructor.newInstance();
    } else {
        // some ref types have a constructor that takes an XmlSchema,
        final XmlSchema targetSchema = targetSchemas.getSchemaByTargetNamespace(targetNamespace);
        target = (T) constructor.newInstance(targetSchema);
    }
    return target;
}
 
示例5
/**
 * Add a field with associated properties to a complex type.
 * 
 * @param index the order of the field in the parent complex type
 * @param xsdSchemaObject the potential field
 * @param xsdSchemaObject the potential field
 * @param fields the parent complex type's fields collection
 * @param compositeTypes the lists of composite types being populated
 */
private void addField(int fieldIndex, XmlSchemaObjectBase xsdSchemaObject,
        Map < String, Object > fields, RootCompositeType compositeTypes) {
    if (xsdSchemaObject instanceof XmlSchemaElement) {
        XmlSchemaElement xsdElement = (XmlSchemaElement) xsdSchemaObject;
        fields.put(getFieldName(xsdElement),
                getProps(fieldIndex, xsdElement, compositeTypes));
    } else if (xsdSchemaObject instanceof XmlSchemaChoice) {
        XmlSchemaChoice xsdChoice = (XmlSchemaChoice) xsdSchemaObject;
        fields.put(getFieldName(xsdChoice),
                getProps(fieldIndex, xsdChoice, compositeTypes));
    }
    // TODO Add Groups
}
 
示例6
private void addCrossImports(XmlSchema schema, XmlSchemaAll all) {
    for (XmlSchemaObjectBase seqMember : all.getItems()) {
        if (seqMember instanceof XmlSchemaElement) {
            addElementCrossImportsElement(schema, (XmlSchemaElement)seqMember);
        }
    }
}
 
示例7
private void addCrossImports(XmlSchema schema, XmlSchemaChoice choice) {
    for (XmlSchemaObjectBase seqMember : choice.getItems()) {
        if (seqMember instanceof XmlSchemaElement) {
            addElementCrossImportsElement(schema, (XmlSchemaElement)seqMember);
        }
    }
}
 
示例8
private void handleAttributeGroup(XmlSchemaAttributeGroup target, XmlSchemaAttributeGroup source) throws ParserException {
    for (XmlSchemaAttributeGroupMember member : source.getAttributes()) {
        // add group member
        target.getAttributes().add((XmlSchemaAttributeGroupMember) createXmlSchemaObjectBase((XmlSchemaObjectBase)member));
    }
}
 
示例9
@SuppressWarnings("unchecked")
private <T extends XmlSchemaObjectBase> T createXmlSchemaObjectBase(T source) throws ParserException {

    // if source is an xsd:* type, return it as result
    if (source instanceof XmlSchemaType) {
        final QName qName = ((XmlSchemaType) source).getQName();
        if (qName != null && isXSDSchemaType(qName)) {
            return source;
        }
    }

    final String targetNamespace;
    final T target;

    try {
        // is it an XmlSchemaNamed that takes a schema as ctor argument?
        if (source instanceof XmlSchemaNamed) {

            final XmlSchemaNamed namedSource = (XmlSchemaNamed) source;

            // get target schema
            targetNamespace = namedSource.getParent().getTargetNamespace();
            final XmlSchema targetSchema = getOrCreateTargetSchema(targetNamespace);

            // if top-level, check if it already exists in targetSchema
            final boolean topLevel = namedSource.isTopLevel();
            if (topLevel) {
                final Optional<XmlSchemaObject> targetObject = targetSchema.getItems().stream()
                    // find matching class and QName
                    .filter(i -> source.getClass().isInstance(i) && ((XmlSchemaNamed) i).getQName().equals(namedSource.getQName()))
                    .findFirst();

                if (targetObject.isPresent()) {
                    // no need to create a new target object
                    return (T) targetObject.get();
                }
            }
            target = (T) createXmlSchemaNamedObject(namedSource, targetSchema, topLevel);

        } else {
            // other XmlSchemaObject
            targetNamespace = getCurrentNamespace();
            target = createXmlSchemaObject(source, targetNamespace);
        }

    } catch (ReflectiveOperationException e) {
        throw new ParserException(String.format("Error extracting type %s: %s", source.getClass().getName(),
                e.getMessage()), e);
    }

    // copy source to target using appropriate handlers
    withNamespace(targetNamespace, () -> copyXmlSchemaObjectBase(target,  source));

    return target;
}