|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.avro; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URI; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collection; |
| 24 | + |
| 25 | +/** |
| 26 | + * Schema parser for JSON formatted schemata. This initial implementation simply |
| 27 | + * delegates to the {@link Schema.Parser} class, though it should be refactored |
| 28 | + * out of there. |
| 29 | + * |
| 30 | + * <p> |
| 31 | + * Note: this class is intentionally not available via the Java |
| 32 | + * {@link java.util.ServiceLoader}, as its use is hardcoded as fallback when no |
| 33 | + * service exists. This enables users to reliably override the standard JSON |
| 34 | + * parser as well. |
| 35 | + * </p> |
| 36 | + */ |
| 37 | +public class JsonSchemaParser implements FormattedSchemaParser { |
| 38 | + /** |
| 39 | + * <p> |
| 40 | + * Parse a schema written in the internal (JSON) format without any validations. |
| 41 | + * </p> |
| 42 | + * |
| 43 | + * <p> |
| 44 | + * Using this method is only safe if used to parse a write schema (i.e., a |
| 45 | + * schema used to read Avro data). Other usages, for example by generated Avro |
| 46 | + * code, can cause interoperability problems. |
| 47 | + * </p> |
| 48 | + * |
| 49 | + * <p> |
| 50 | + * Use with care and sufficient testing! |
| 51 | + * </p> |
| 52 | + * |
| 53 | + * @param fragments one or more strings making up the schema (some schemata |
| 54 | + * exceed the compiler limits) |
| 55 | + * @return the parsed schema |
| 56 | + */ |
| 57 | + public static Schema parseInternal(String... fragments) { |
| 58 | + StringBuilder buffer = new StringBuilder(); |
| 59 | + for (String fragment : fragments) { |
| 60 | + buffer.append(fragment); |
| 61 | + } |
| 62 | + return new JsonSchemaParser().parse(new ArrayList<>(), buffer, true); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Schema parse(Collection<Schema> schemas, URI baseUri, CharSequence formattedSchema) |
| 67 | + throws IOException, SchemaParseException { |
| 68 | + return parse(schemas, formattedSchema, false); |
| 69 | + } |
| 70 | + |
| 71 | + private Schema parse(Collection<Schema> schemas, CharSequence formattedSchema, boolean skipValidation) |
| 72 | + throws SchemaParseException { |
| 73 | + // TODO: refactor JSON parsing out of the Schema class |
| 74 | + Schema.Parser parser; |
| 75 | + if (skipValidation) { |
| 76 | + parser = new Schema.Parser(Schema.NameValidator.NO_VALIDATION); |
| 77 | + parser.setValidateDefaults(false); |
| 78 | + } else { |
| 79 | + parser = new Schema.Parser(); |
| 80 | + } |
| 81 | + if (schemas != null) { |
| 82 | + parser.addTypes(schemas); |
| 83 | + } |
| 84 | + Schema schema = parser.parse(formattedSchema.toString()); |
| 85 | + if (schemas != null) { |
| 86 | + schemas.addAll(parser.getTypes().values()); |
| 87 | + } |
| 88 | + return schema; |
| 89 | + } |
| 90 | +} |
0 commit comments