Search in sources :

Example 1 with Options

use of com.squareup.wire.schema.Options in project schema2proto by entur.

the class SchemaParser method processAttribute.

private void processAttribute(MessageType messageType, Set<Object> processedXmlObjects, XSAttributeUse attr) {
    if (!processedXmlObjects.contains(attr)) {
        processedXmlObjects.add(attr);
        XSAttributeDecl decl = attr.getDecl();
        XSSimpleType type = decl.getType();
        if (type.getPrimitiveType() != null || type.isList() || type.isUnion()) {
            String fieldName = decl.getName();
            String doc = resolveDocumentationAnnotation(decl, false);
            int tag = messageType.getNextFieldNum();
            Location fieldLocation = getLocation(decl);
            Options fieldOptions = getFieldOptions(decl);
            String packageName = NamespaceHelper.xmlNamespaceToProtoFieldPackagename(type.getTargetNamespace(), configuration.forceProtoPackage);
            Label label = type.isList() ? Label.REPEATED : null;
            if (type.isRestriction() && type.getFacet(XSFacet.FACET_ENUMERATION) != null) {
                String enumName = createEnum(fieldName, type.asRestriction(), decl.isLocal() ? messageType : null);
                Field field = new Field(packageName, fieldLocation, label, fieldName, doc, tag, enumName, fieldOptions, false);
                field.setFromAttribute(true);
                addField(messageType, field);
            } else {
                String typeName = findFieldType(type);
                Field field = new Field(basicTypes.contains(typeName) ? null : packageName, fieldLocation, label, fieldName, doc, tag, typeName, fieldOptions, false);
                field.setFromAttribute(true);
                addField(messageType, field);
            }
        } else {
            LOGGER.error("Unhandled attribute use {}", attr.getDecl());
        }
    }
}
Also used : Options(com.squareup.wire.schema.Options) Field(com.squareup.wire.schema.Field) XSSimpleType(com.sun.xml.xsom.XSSimpleType) XSAttributeDecl(com.sun.xml.xsom.XSAttributeDecl) Label(com.squareup.wire.schema.Field.Label) Location(com.squareup.wire.schema.Location)

Example 2 with Options

use of com.squareup.wire.schema.Options in project schema2proto by entur.

the class SchemaParser method createEnum.

private String createEnum(String elementName, XSRestrictionSimpleType type, MessageType enclosingType) {
    Iterator<? extends XSFacet> it;
    String typeNameToUse;
    if (type.getName() != null) {
        typeNameToUse = type.getName();
        enclosingType = null;
    } else {
        String baseTypeName = type.getBaseType().getName();
        if (baseTypeName != null && !basicTypes.contains(baseTypeName)) {
            typeNameToUse = baseTypeName;
        } else if (enclosingType != null) {
            typeNameToUse = elementName + TYPE_SUFFIX;
        } else {
            typeNameToUse = elementName + GENERATED_NAME_PLACEHOLDER;
        }
    }
    Type protoType = getType(type.getTargetNamespace(), typeNameToUse);
    if (protoType == null) {
        type = type.asRestriction();
        Location location = getLocation(type);
        List<EnumConstant> constants = new ArrayList<>();
        it = type.getDeclaredFacets().iterator();
        int counter = 1;
        Set<String> addedValues = new HashSet<>();
        while (it.hasNext()) {
            List<OptionElement> optionElements = new ArrayList<>();
            XSFacet next = it.next();
            String doc = resolveDocumentationAnnotation(next, false);
            String enumValue = next.getValue().value;
            if (!addedValues.contains(enumValue)) {
                addedValues.add(enumValue);
                constants.add(new EnumConstant(location, enumValue, counter++, doc, new Options(Options.ENUM_VALUE_OPTIONS, optionElements)));
            }
        }
        List<OptionElement> enumOptionElements = new ArrayList<>();
        Options enumOptions = new Options(Options.ENUM_OPTIONS, enumOptionElements);
        String doc = resolveDocumentationAnnotation(type, false);
        ProtoType definedProtoType;
        if (enclosingType == null) {
            definedProtoType = ProtoType.get(typeNameToUse);
        } else {
            definedProtoType = ProtoType.get(enclosingType.getName(), typeNameToUse);
        }
        EnumType enumType = new EnumType(definedProtoType, location, doc, typeNameToUse, constants, new ArrayList<>(), enumOptions);
        if (enclosingType != null) {
            // if not already present
            boolean alreadyPresentAsNestedType = false;
            for (Type t : enclosingType.nestedTypes()) {
                if (t instanceof EnumType && ((EnumType) t).name().equals(typeNameToUse)) {
                    alreadyPresentAsNestedType = true;
                    break;
                }
            }
            if (!alreadyPresentAsNestedType) {
                enclosingType.nestedTypes().add(enumType);
            }
        } else {
            addType(type.getTargetNamespace(), enumType);
        }
    }
    return typeNameToUse;
}
Also used : Options(com.squareup.wire.schema.Options) ArrayList(java.util.ArrayList) ProtoType(com.squareup.wire.schema.ProtoType) Type(com.squareup.wire.schema.Type) XSType(com.sun.xml.xsom.XSType) ProtoType(com.squareup.wire.schema.ProtoType) XSSimpleType(com.sun.xml.xsom.XSSimpleType) XSListSimpleType(com.sun.xml.xsom.XSListSimpleType) XSComplexType(com.sun.xml.xsom.XSComplexType) XSRestrictionSimpleType(com.sun.xml.xsom.XSRestrictionSimpleType) MessageType(com.squareup.wire.schema.MessageType) EnumType(com.squareup.wire.schema.EnumType) XSFacet(com.sun.xml.xsom.XSFacet) EnumType(com.squareup.wire.schema.EnumType) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) EnumConstant(com.squareup.wire.schema.EnumConstant) Location(com.squareup.wire.schema.Location) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with Options

use of com.squareup.wire.schema.Options in project schema2proto by entur.

the class SchemaParser method createWrapperAndContinueProcessing.

private void createWrapperAndContinueProcessing(XSModelGroup modelGroup, XSParticle particle, MessageType messageType, Set<Object> processedXmlObjects, XSSchemaSet schemaSet, XSParticle[] children, String typeName, String targetNamespace, String fieldName, XSComplexType enclosingType) {
    if (!processedXmlObjects.contains(particle)) {
        processedXmlObjects.add(particle);
        String enclosingName = typeName;
        if (modelGroup.isModelGroupDecl()) {
            enclosingName = modelGroup.asModelGroupDecl().getName();
        }
        // Create new message type enclosed in existing
        String doc = resolveDocumentationAnnotation(modelGroup, false);
        Location location = getLocation(modelGroup);
        // Add message type to file
        List<OptionElement> messageOptions = new ArrayList<>();
        Options options = new Options(Options.MESSAGE_OPTIONS, messageOptions);
        MessageType wrapperType = new MessageType(ProtoType.get(typeName), location, doc, typeName, options);
        wrapperType.setWrapperMessageType(true);
        messageType.nestedTypes().add(wrapperType);
        Options fieldOptions = getFieldOptions(particle);
        String fieldPackagename = NamespaceHelper.xmlNamespaceToProtoFieldPackagename(targetNamespace, configuration.forceProtoPackage);
        Field field = new Field(fieldPackagename, location, Label.REPEATED, fieldName, doc, messageType.getNextFieldNum(), typeName, fieldOptions, false);
        addField(messageType, field);
        localTypes.add(new LocalType(particle, wrapperType, messageType, field, NamespaceHelper.xmlNamespaceToProtoPackage(targetNamespace, configuration.forceProtoPackage), enclosingType));
        processGroupAsSequence(particle, wrapperType, processedXmlObjects, schemaSet, children, enclosingName, targetNamespace, enclosingType);
    }
}
Also used : Options(com.squareup.wire.schema.Options) Field(com.squareup.wire.schema.Field) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ArrayList(java.util.ArrayList) MessageType(com.squareup.wire.schema.MessageType) Location(com.squareup.wire.schema.Location)

Example 4 with Options

use of com.squareup.wire.schema.Options in project schema2proto by entur.

the class SerializationTest method testBuildExtension.

@Test
public void testBuildExtension() {
    ProtoFile f = new ProtoFile(Syntax.PROTO_3, "default");
    Location loc = new Location("", "", 0, 0);
    List<Field> fields = new ArrayList<>();
    List<OptionElement> optionElements = new ArrayList<>();
    Options options = new Options(ProtoType.get("google.protobuf.MessageOptions"), optionElements);
    Field field = new Field(null, loc, null, "fieldname", "Base type this message actually is an extension of", 1101, "string", options, false);
    fields.add(field);
    Extend e = new Extend(loc, "Information elements extracted from the xsd structure", "google.protobuf.MessageOptions", fields);
    f.getExtendList().add(e);
    String schema = f.toSchema();
    System.out.println(schema);
    assertNotNull(schema);
}
Also used : Field(com.squareup.wire.schema.Field) Options(com.squareup.wire.schema.Options) Extend(com.squareup.wire.schema.Extend) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ProtoFile(com.squareup.wire.schema.ProtoFile) ArrayList(java.util.ArrayList) Location(com.squareup.wire.schema.Location) Test(org.junit.jupiter.api.Test)

Example 5 with Options

use of com.squareup.wire.schema.Options in project schema2proto by entur.

the class SerializationTest method testBuildProtoFile.

@Test
public void testBuildProtoFile() {
    ProtoFile f = new ProtoFile(Syntax.PROTO_3, "default");
    List<EnumConstant> values = new ArrayList<EnumConstant>();
    List<OptionElement> elements = new ArrayList<>();
    List<Reserved> reserveds = new ArrayList<>();
    Options constantOptions = new Options(ProtoType.BOOL, elements);
    Location valueLocation = new Location("base", "file", 1, 2);
    values.add(new EnumConstant(valueLocation, "VAL1", 1, "doc", constantOptions));
    Options options = new Options(ProtoType.BOOL, new ArrayList<>());
    f.types().add(new EnumType(ProtoType.INT32, valueLocation, "doc", "Name", values, reserveds, options));
    Location messagLocation = new Location("base", "path", 1, 1);
    ImmutableList<Field> declaredFields = ImmutableList.copyOf(new ArrayList<>());
    List<Field> extensionFields = new ArrayList<>();
    ImmutableList<OneOf> oneOfs = ImmutableList.copyOf(new ArrayList<>());
    ImmutableList<Type> nestedTypes = ImmutableList.copyOf(new ArrayList<>());
    List<Extensions> extensionsList = new ArrayList<>();
    List<OptionElement> messageOptionElements = new ArrayList<>();
    Options messageOptions = new Options(ProtoType.BOOL, messageOptionElements);
    MessageType message = new MessageType(ProtoType.BOOL, messagLocation, "doc", "messagename", declaredFields, extensionFields, oneOfs, nestedTypes, extensionsList, reserveds, messageOptions);
    f.types().add(message);
    String schema = f.toSchema();
    assertNotNull(schema);
}
Also used : Options(com.squareup.wire.schema.Options) ProtoFile(com.squareup.wire.schema.ProtoFile) ArrayList(java.util.ArrayList) Extensions(com.squareup.wire.schema.Extensions) Field(com.squareup.wire.schema.Field) OneOf(com.squareup.wire.schema.OneOf) ProtoType(com.squareup.wire.schema.ProtoType) Type(com.squareup.wire.schema.Type) EnumType(com.squareup.wire.schema.EnumType) MessageType(com.squareup.wire.schema.MessageType) EnumType(com.squareup.wire.schema.EnumType) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) EnumConstant(com.squareup.wire.schema.EnumConstant) MessageType(com.squareup.wire.schema.MessageType) Reserved(com.squareup.wire.schema.Reserved) Location(com.squareup.wire.schema.Location) Test(org.junit.jupiter.api.Test)

Aggregations

Options (com.squareup.wire.schema.Options)16 Location (com.squareup.wire.schema.Location)15 ArrayList (java.util.ArrayList)15 Field (com.squareup.wire.schema.Field)14 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)14 MessageType (com.squareup.wire.schema.MessageType)13 EnumConstant (com.squareup.wire.schema.EnumConstant)9 ProtoFile (com.squareup.wire.schema.ProtoFile)9 EnumType (com.squareup.wire.schema.EnumType)8 Type (com.squareup.wire.schema.Type)8 HashSet (java.util.HashSet)8 OneOf (com.squareup.wire.schema.OneOf)7 Set (java.util.Set)7 Schema (com.squareup.wire.schema.Schema)6 LinkedHashSet (java.util.LinkedHashSet)6 List (java.util.List)6 Objects (java.util.Objects)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 ImmutableList (com.google.common.collect.ImmutableList)5