/** * Deserialize an instance from the given XML input and validate it. * * @param input XML input * @return deserialized schema model * @throws IOException if an I/O error occurs * @throws InvalidSchemaException if the XML input or decoded {@link SchemaModel} is invalid * @throws IllegalArgumentException if {@code input} is null */ public static SchemaModel fromXML(InputStream input) throws IOException { Preconditions.checkArgument(input != null, "null input"); final SchemaModel schemaModel = new SchemaModel(); try { final XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(input); schemaModel.readXML(reader); } catch (XMLStreamException e) { throw new InvalidSchemaException("error parsing schema model XML", e); } schemaModel.validate(); return schemaModel; }
/** * Validate a {@link SchemaModel}. * * <p> * This method only statically checks the schema; it does not validate the schema against any other * existing schema versions that may be previously recorded in a database. * * <p> * To validate a schema against a particular database, simply attempt to create a transaction * via {@link #createTransaction createTransaction()}. To validate that a collection of schemas * are mutually consistent independently from any database, use {@link #validateSchemas validateSchemas()}. * * @param fieldTypeRegistry registry of simple field types * @param schemaModel schema to validate * @throws InvalidSchemaException if {@code schemaModel} is invalid * @throws IllegalArgumentException if either parameter is null */ public static void validateSchema(FieldTypeRegistry fieldTypeRegistry, SchemaModel schemaModel) { // Sanity check Preconditions.checkArgument(fieldTypeRegistry != null, "null fieldTypeRegistry"); Preconditions.checkArgument(schemaModel != null, "null schemaModel"); // Validate schemaModel.validate(); try { new Schema(1, new byte[0], schemaModel, fieldTypeRegistry); } catch (IllegalArgumentException e) { throw new InvalidSchemaException("invalid schema: " + e.getMessage(), e); } }
if (schemaModel == null) continue; schemaModel.validate(); final int version = index + 1; try {
Preconditions.checkArgument(schemaModel != null || version >= 0, "can't auto-generate version without schema model"); if (schemaModel != null) { schemaModel.validate(); if (version == -1) version = schemaModel.autogenerateVersion();