1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.statementservice.retriever;
18 
19 import com.android.statementservice.utils.StatementUtils;
20 
21 import org.json.JSONException;
22 import org.json.JSONObject;
23 
24 /**
25  * Factory to create asset matcher from JSON string.
26  */
27 /* package private */ final class AssetMatcherFactory {
28 
29     private static final String FIELD_NOT_STRING_FORMAT_STRING = "Expected %s to be string.";
30     private static final String NAMESPACE_NOT_SUPPORTED_STRING = "Namespace %s is not supported.";
31 
create(String query)32     public static AbstractAssetMatcher create(String query) throws AssociationServiceException,
33             JSONException {
34         JSONObject queryObject = new JSONObject(query);
35 
36         String namespace = queryObject.optString(StatementUtils.NAMESPACE_FIELD, null);
37         if (namespace == null) {
38             throw new AssociationServiceException(String.format(
39                     FIELD_NOT_STRING_FORMAT_STRING, StatementUtils.NAMESPACE_FIELD));
40         }
41 
42         if (namespace.equals(StatementUtils.NAMESPACE_WEB)) {
43             return new WebAssetMatcher(WebAsset.create(queryObject));
44         } else if (namespace.equals(StatementUtils.NAMESPACE_ANDROID_APP)) {
45             return new AndroidAppAssetMatcher(AndroidAppAsset.create(queryObject));
46         } else {
47             throw new AssociationServiceException(
48                     String.format(NAMESPACE_NOT_SUPPORTED_STRING, namespace));
49         }
50     }
51 }
52