Home | History | Annotate | Download | only in retriever
      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 org.json.JSONException;
     20 import org.json.JSONObject;
     21 
     22 /**
     23  * Factory to create asset matcher from JSON string.
     24  */
     25 /* package private */ final class AssetMatcherFactory {
     26 
     27     private static final String FIELD_NOT_STRING_FORMAT_STRING = "Expected %s to be string.";
     28     private static final String NAMESPACE_NOT_SUPPORTED_STRING = "Namespace %s is not supported.";
     29 
     30     public static AbstractAssetMatcher create(String query) throws AssociationServiceException,
     31             JSONException {
     32         JSONObject queryObject = new JSONObject(query);
     33 
     34         String namespace = queryObject.optString(Utils.NAMESPACE_FIELD, null);
     35         if (namespace == null) {
     36             throw new AssociationServiceException(String.format(
     37                     FIELD_NOT_STRING_FORMAT_STRING, Utils.NAMESPACE_FIELD));
     38         }
     39 
     40         if (namespace.equals(Utils.NAMESPACE_WEB)) {
     41             return new WebAssetMatcher(WebAsset.create(queryObject));
     42         } else if (namespace.equals(Utils.NAMESPACE_ANDROID_APP)) {
     43             return new AndroidAppAssetMatcher(AndroidAppAsset.create(queryObject));
     44         } else {
     45             throw new AssociationServiceException(
     46                     String.format(NAMESPACE_NOT_SUPPORTED_STRING, namespace));
     47         }
     48     }
     49 }
     50