ConfigXmlParser.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Locale;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. import org.xmlpull.v1.XmlPullParser;
  24. import org.xmlpull.v1.XmlPullParserException;
  25. import android.content.Context;
  26. public class ConfigXmlParser {
  27. private static String TAG = "ConfigXmlParser";
  28. private String launchUrl = "file:///android_asset/www/index.html";
  29. private CordovaPreferences prefs = new CordovaPreferences();
  30. private ArrayList<PluginEntry> pluginEntries = new ArrayList<PluginEntry>(20);
  31. public CordovaPreferences getPreferences() {
  32. return prefs;
  33. }
  34. public ArrayList<PluginEntry> getPluginEntries() {
  35. return pluginEntries;
  36. }
  37. public String getLaunchUrl() {
  38. return launchUrl;
  39. }
  40. public void parse(Context action) {
  41. // First checking the class namespace for config.xml
  42. int id = action.getResources().getIdentifier("config", "xml", action.getClass().getPackage().getName());
  43. if (id == 0) {
  44. // If we couldn't find config.xml there, we'll look in the namespace from AndroidManifest.xml
  45. id = action.getResources().getIdentifier("config", "xml", action.getPackageName());
  46. if (id == 0) {
  47. LOG.e(TAG, "res/xml/config.xml is missing!");
  48. return;
  49. }
  50. }
  51. parse(action.getResources().getXml(id));
  52. }
  53. boolean insideFeature = false;
  54. String service = "", pluginClass = "", paramType = "";
  55. boolean onload = false;
  56. public void parse(XmlPullParser xml) {
  57. int eventType = -1;
  58. while (eventType != XmlPullParser.END_DOCUMENT) {
  59. if (eventType == XmlPullParser.START_TAG) {
  60. handleStartTag(xml);
  61. }
  62. else if (eventType == XmlPullParser.END_TAG)
  63. {
  64. handleEndTag(xml);
  65. }
  66. try {
  67. eventType = xml.next();
  68. } catch (XmlPullParserException e) {
  69. e.printStackTrace();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75. public void handleStartTag(XmlPullParser xml) {
  76. String strNode = xml.getName();
  77. if (strNode.equals("feature")) {
  78. //Check for supported feature sets aka. plugins (Accelerometer, Geolocation, etc)
  79. //Set the bit for reading params
  80. insideFeature = true;
  81. service = xml.getAttributeValue(null, "name");
  82. }
  83. else if (insideFeature && strNode.equals("param")) {
  84. paramType = xml.getAttributeValue(null, "name");
  85. if (paramType.equals("service")) // check if it is using the older service param
  86. service = xml.getAttributeValue(null, "value");
  87. else if (paramType.equals("package") || paramType.equals("android-package"))
  88. pluginClass = xml.getAttributeValue(null,"value");
  89. else if (paramType.equals("onload"))
  90. onload = "true".equals(xml.getAttributeValue(null, "value"));
  91. }
  92. else if (strNode.equals("preference")) {
  93. String name = xml.getAttributeValue(null, "name").toLowerCase(Locale.ENGLISH);
  94. String value = xml.getAttributeValue(null, "value");
  95. prefs.set(name, value);
  96. }
  97. else if (strNode.equals("content")) {
  98. String src = xml.getAttributeValue(null, "src");
  99. if (src != null) {
  100. setStartUrl(src);
  101. }
  102. }
  103. }
  104. public void handleEndTag(XmlPullParser xml) {
  105. String strNode = xml.getName();
  106. if (strNode.equals("feature")) {
  107. pluginEntries.add(new PluginEntry(service, pluginClass, onload));
  108. service = "";
  109. pluginClass = "";
  110. insideFeature = false;
  111. onload = false;
  112. }
  113. }
  114. private void setStartUrl(String src) {
  115. Pattern schemeRegex = Pattern.compile("^[a-z-]+://");
  116. Matcher matcher = schemeRegex.matcher(src);
  117. if (matcher.find()) {
  118. launchUrl = src;
  119. } else {
  120. if (src.charAt(0) == '/') {
  121. src = src.substring(1);
  122. }
  123. launchUrl = "file:///android_asset/www/" + src;
  124. }
  125. }
  126. }