• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.googlecode.android_scripting.language;
18 
19 import com.googlecode.android_scripting.rpc.ParameterDescriptor;
20 
21 public class HtmlLanguage extends Language {
22 
23   /** Returns the Android package import statement. */
24   @Override
getImportStatement()25   protected String getImportStatement() {
26     return "<html>\n<head>\n<script>";
27   }
28 
29   @Override
getRpcReceiverDeclaration(String rpcReceiver)30   protected String getRpcReceiverDeclaration(String rpcReceiver) {
31     return String.format("var %s = new Android();\n</script>\n</head>\n<body>\n\n</body>\n</html>",
32         rpcReceiver);
33   }
34 
35   @Override
getMethodCallText(String receiver, String method, ParameterDescriptor[] parameters)36   protected String getMethodCallText(String receiver, String method,
37       ParameterDescriptor[] parameters) {
38     StringBuilder result =
39         new StringBuilder().append(getApplyReceiverText(receiver)).append(getApplyOperatorText())
40             .append(method);
41     if (parameters.length > 0) {
42       result.append(getLeftParametersText());
43     } else {
44       result.append(getQuote());
45     }
46     String separator = "";
47     for (ParameterDescriptor parameter : parameters) {
48       result.append(separator).append(getValueText(parameter));
49       separator = getParameterSeparator();
50     }
51     result.append(getRightParametersText());
52 
53     return result.toString();
54   }
55 
56   @Override
getApplyOperatorText()57   protected String getApplyOperatorText() {
58     return ".call('";
59   }
60 
61   @Override
getLeftParametersText()62   protected String getLeftParametersText() {
63     return "', ";
64   }
65 
66   @Override
getRightParametersText()67   protected String getRightParametersText() {
68     return ")";
69   }
70 
71   @Override
getQuote()72   protected String getQuote() {
73     return "'";
74   }
75 
76 }
77