LayoutFileParser.java revision 731b74f7f44e67312a1fc4161c4e0aae221b2417
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * Unless required by applicable law or agreed to in writing, software 8 * distributed under the License is distributed on an "AS IS" BASIS, 9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 * See the License for the specific language governing permissions and 11 * limitations under the License. 12 */ 13 14package android.databinding.tool.store; 15 16import org.antlr.v4.runtime.ANTLRInputStream; 17import org.antlr.v4.runtime.CommonTokenStream; 18import org.antlr.v4.runtime.ParserRuleContext; 19import org.antlr.v4.runtime.misc.NotNull; 20import org.apache.commons.io.FileUtils; 21import org.apache.commons.lang3.StringEscapeUtils; 22import org.apache.commons.lang3.StringUtils; 23import org.w3c.dom.Document; 24import org.w3c.dom.Node; 25import org.w3c.dom.NodeList; 26import org.xml.sax.SAXException; 27 28import android.databinding.parser.XMLLexer; 29import android.databinding.parser.XMLParser; 30import android.databinding.parser.XMLParserBaseVisitor; 31import android.databinding.tool.processing.ErrorMessages; 32import android.databinding.tool.processing.Scope; 33import android.databinding.tool.processing.scopes.FileScopeProvider; 34import android.databinding.tool.util.L; 35import android.databinding.tool.util.ParserHelper; 36import android.databinding.tool.util.Preconditions; 37import android.databinding.tool.util.XmlEditor; 38 39import java.io.File; 40import java.io.FileReader; 41import java.io.IOException; 42import java.net.MalformedURLException; 43import java.net.URISyntaxException; 44import java.net.URL; 45import java.util.ArrayList; 46import java.util.HashMap; 47import java.util.List; 48import java.util.Map; 49 50import javax.xml.parsers.DocumentBuilder; 51import javax.xml.parsers.DocumentBuilderFactory; 52import javax.xml.parsers.ParserConfigurationException; 53import javax.xml.xpath.XPath; 54import javax.xml.xpath.XPathConstants; 55import javax.xml.xpath.XPathExpression; 56import javax.xml.xpath.XPathExpressionException; 57import javax.xml.xpath.XPathFactory; 58 59/** 60 * Gets the list of XML files and creates a list of 61 * {@link android.databinding.tool.store.ResourceBundle} that can be persistent or converted to 62 * LayoutBinder. 63 */ 64public class LayoutFileParser { 65 66 private static final String XPATH_BINDING_LAYOUT = "/layout"; 67 68 private static final String LAYOUT_PREFIX = "@layout/"; 69 70 public ResourceBundle.LayoutFileBundle parseXml(final File xml, String pkg, int minSdk) 71 throws ParserConfigurationException, IOException, SAXException, 72 XPathExpressionException { 73 try { 74 Scope.enter(new FileScopeProvider() { 75 @Override 76 public String provideScopeFilePath() { 77 return xml.getAbsolutePath(); 78 } 79 }); 80 final String xmlNoExtension = ParserHelper.stripExtension(xml.getName()); 81 final String newTag = xml.getParentFile().getName() + '/' + xmlNoExtension; 82 File original = stripFileAndGetOriginal(xml, newTag); 83 if (original == null) { 84 L.d("assuming the file is the original for %s", xml.getAbsoluteFile()); 85 original = xml; 86 } 87 return parseXml(original, pkg); 88 } finally { 89 Scope.exit(); 90 } 91 } 92 93 private ResourceBundle.LayoutFileBundle parseXml(final File original, String pkg) 94 throws IOException { 95 try { 96 Scope.enter(new FileScopeProvider() { 97 @Override 98 public String provideScopeFilePath() { 99 return original.getAbsolutePath(); 100 } 101 }); 102 final String xmlNoExtension = ParserHelper.stripExtension(original.getName()); 103 ANTLRInputStream inputStream = new ANTLRInputStream(new FileReader(original)); 104 XMLLexer lexer = new XMLLexer(inputStream); 105 CommonTokenStream tokenStream = new CommonTokenStream(lexer); 106 XMLParser parser = new XMLParser(tokenStream); 107 XMLParser.DocumentContext expr = parser.document(); 108 XMLParser.ElementContext root = expr.element(); 109 if (!"layout".equals(root.elmName.getText())) { 110 return null; 111 } 112 XMLParser.ElementContext data = getDataNode(root); 113 XMLParser.ElementContext rootView = getViewNode(original, root); 114 115 if (hasMergeInclude(rootView)) { 116 L.e(ErrorMessages.INCLUDE_INSIDE_MERGE); 117 return null; 118 } 119 boolean isMerge = "merge".equals(rootView.elmName.getText()); 120 121 ResourceBundle.LayoutFileBundle bundle = new ResourceBundle.LayoutFileBundle(original, 122 xmlNoExtension, original.getParentFile().getName(), pkg, isMerge); 123 final String newTag = original.getParentFile().getName() + '/' + xmlNoExtension; 124 parseData(original, data, bundle); 125 parseExpressions(newTag, rootView, isMerge, bundle); 126 return bundle; 127 } finally { 128 Scope.exit(); 129 } 130 } 131 132 private void parseExpressions(String newTag, final XMLParser.ElementContext rootView, 133 final boolean isMerge, ResourceBundle.LayoutFileBundle bundle) { 134 final List<XMLParser.ElementContext> bindingElements = new ArrayList<>(); 135 final List<XMLParser.ElementContext> otherElementsWithIds = new ArrayList<>(); 136 rootView.accept(new XMLParserBaseVisitor<Void>() { 137 @Override 138 public Void visitElement(@NotNull XMLParser.ElementContext ctx) { 139 if (filter(ctx)) { 140 bindingElements.add(ctx); 141 } else { 142 String name = ctx.elmName.getText(); 143 if (!"include".equals(name) && !"fragment".equals(name) && 144 attributeMap(ctx).containsKey("android:id")) { 145 otherElementsWithIds.add(ctx); 146 } 147 } 148 visitChildren(ctx); 149 return null; 150 } 151 152 private boolean filter(XMLParser.ElementContext ctx) { 153 if (isMerge) { 154 // account for XMLParser.ContentContext 155 if (ctx.getParent().getParent() == rootView) { 156 return true; 157 } 158 } else if (ctx == rootView) { 159 return true; 160 } 161 if (hasIncludeChild(ctx)) { 162 return true; 163 } 164 if (XmlEditor.hasExpressionAttributes(ctx)) { 165 return true; 166 } 167 return false; 168 } 169 170 private boolean hasIncludeChild(XMLParser.ElementContext ctx) { 171 for (XMLParser.ElementContext child : XmlEditor.elements(ctx)) { 172 if ("include".equals(child.elmName.getText())) { 173 return true; 174 } 175 } 176 return false; 177 } 178 }); 179 180 final HashMap<XMLParser.ElementContext, String> nodeTagMap = 181 new HashMap<XMLParser.ElementContext, String>(); 182 L.d("number of binding nodes %d", bindingElements.size()); 183 int tagNumber = 0; 184 for (XMLParser.ElementContext parent : bindingElements) { 185 final Map<String, String> attributes = attributeMap(parent); 186 String nodeName = parent.elmName.getText(); 187 String viewName = null; 188 String includedLayoutName = null; 189 final String id = attributes.get("android:id"); 190 final String tag; 191 final String originalTag = attributes.get("android:tag"); 192 if ("include".equals(nodeName)) { 193 // get the layout attribute 194 final String includeValue = attributes.get("layout"); 195 if (StringUtils.isEmpty(includeValue)) { 196 L.e("%s must include a layout", parent); 197 } 198 if (!includeValue.startsWith(LAYOUT_PREFIX)) { 199 L.e("included value (%s) must start with %s.", 200 includeValue, LAYOUT_PREFIX); 201 } 202 // if user is binding something there, there MUST be a layout file to be 203 // generated. 204 String layoutName = includeValue.substring(LAYOUT_PREFIX.length()); 205 includedLayoutName = layoutName; 206 final ParserRuleContext myParentContent = parent.getParent(); 207 Preconditions.check(myParentContent instanceof XMLParser.ContentContext, 208 "parent of an include tag must be a content context but it is %s", 209 myParentContent.getClass().getCanonicalName()); 210 final ParserRuleContext grandParent = myParentContent.getParent(); 211 Preconditions.check(grandParent instanceof XMLParser.ElementContext, 212 "grandparent of an include tag must be an element context but it is %s", 213 grandParent.getClass().getCanonicalName()); 214 //noinspection SuspiciousMethodCalls 215 tag = nodeTagMap.get(grandParent); 216 } else if ("fragment".equals(nodeName)) { 217 L.e("fragments do not support data binding expressions."); 218 continue; 219 } else { 220 viewName = getViewName(parent); 221 // account for XMLParser.ContentContext 222 if (rootView == parent || (isMerge && parent.getParent().getParent() == rootView)) { 223 tag = newTag + "_" + tagNumber; 224 } else { 225 tag = "binding_" + tagNumber; 226 } 227 tagNumber++; 228 } 229 final ResourceBundle.BindingTargetBundle bindingTargetBundle = 230 bundle.createBindingTarget(id, viewName, true, tag, originalTag, 231 new Location(parent)); 232 nodeTagMap.put(parent, tag); 233 bindingTargetBundle.setIncludedLayout(includedLayoutName); 234 235 for (XMLParser.AttributeContext attr : XmlEditor.expressionAttributes(parent)) { 236 String value = escapeQuotes(attr.attrValue.getText(), true); 237 if (value.charAt(0) == '@' && value.charAt(1) == '{' && 238 value.charAt(value.length() - 1) == '}') { 239 final String strippedValue = value.substring(2, value.length() - 1); 240 Location attrLocation = new Location(attr); 241 Location valueLocation = new Location(); 242 // offset to 0 based 243 valueLocation.startLine = attr.attrValue.getLine() - 1; 244 valueLocation.startOffset = attr.attrValue.getCharPositionInLine() + 245 attr.attrValue.getText().indexOf(strippedValue); 246 valueLocation.endLine = attrLocation.endLine; 247 valueLocation.endOffset = attrLocation.endOffset - 2; // account for: "} 248 bindingTargetBundle.addBinding(escapeQuotes(attr.attrName.getText(), false) 249 , strippedValue, attrLocation, valueLocation); 250 } 251 } 252 } 253 254 for (XMLParser.ElementContext elm : otherElementsWithIds) { 255 final String id = attributeMap(elm).get("android:id"); 256 final String className = getViewName(elm); 257 bundle.createBindingTarget(id, className, true, null, null, new Location(elm)); 258 } 259 } 260 261 private String getViewName(XMLParser.ElementContext elm) { 262 String viewName = elm.elmName.getText(); 263 if ("view".equals(viewName)) { 264 String classNode = attributeMap(elm).get("class"); 265 if (StringUtils.isEmpty(classNode)) { 266 L.e("No class attribute for 'view' node"); 267 } 268 viewName = classNode; 269 } 270 return viewName; 271 } 272 273 private void parseData(File xml, XMLParser.ElementContext data, 274 ResourceBundle.LayoutFileBundle bundle) { 275 if (data == null) { 276 return; 277 } 278 for (XMLParser.ElementContext imp : filter(data, "import")) { 279 final Map<String, String> attrMap = attributeMap(imp); 280 String type = attrMap.get("type"); 281 String alias = attrMap.get("alias"); 282 Preconditions.check(StringUtils.isNotBlank(type), "Type of an import cannot be empty." 283 + " %s in %s", imp.toStringTree(), xml); 284 if (StringUtils.isEmpty(alias)) { 285 final String[] split = StringUtils.split(type, '.'); 286 alias = split[split.length - 1]; 287 } 288 bundle.addImport(alias, type, new Location(imp)); 289 } 290 291 for (XMLParser.ElementContext variable : filter(data, "variable")) { 292 final Map<String, String> attrMap = attributeMap(variable); 293 String type = attrMap.get("type"); 294 String name = attrMap.get("name"); 295 Preconditions.checkNotNull(type, "variable must have a type definition %s in %s", 296 variable.toStringTree(), xml); 297 Preconditions.checkNotNull(name, "variable must have a name %s in %s", 298 variable.toStringTree(), xml); 299 bundle.addVariable(name, type, new Location(variable)); 300 } 301 final String className = attributeMap(data).get("class"); 302 if (StringUtils.isNotBlank(className)) { 303 bundle.setBindingClass(className); 304 } 305 } 306 307 private XMLParser.ElementContext getDataNode(XMLParser.ElementContext root) { 308 final List<XMLParser.ElementContext> data = filter(root, "data"); 309 if (data.size() == 0) { 310 return null; 311 } 312 Preconditions.check(data.size() == 1, "XML layout can have only 1 data tag"); 313 return data.get(0); 314 } 315 316 private XMLParser.ElementContext getViewNode(File xml, XMLParser.ElementContext root) { 317 final List<XMLParser.ElementContext> view = filterNot(root, "data"); 318 Preconditions.check(view.size() == 1, "XML layout %s must have 1 view but has %s. root" 319 + " children count %s", xml, view.size(), root.getChildCount()); 320 return view.get(0); 321 } 322 323 private List<XMLParser.ElementContext> filter(XMLParser.ElementContext root, 324 String name) { 325 List<XMLParser.ElementContext> result = new ArrayList<>(); 326 if (root == null) { 327 return result; 328 } 329 final XMLParser.ContentContext content = root.content(); 330 if (content == null) { 331 return result; 332 } 333 for (XMLParser.ElementContext child : XmlEditor.elements(root)) { 334 if (name.equals(child.elmName.getText())) { 335 result.add(child); 336 } 337 } 338 return result; 339 } 340 341 private List<XMLParser.ElementContext> filterNot(XMLParser.ElementContext root, 342 String name) { 343 List<XMLParser.ElementContext> result = new ArrayList<>(); 344 if (root == null) { 345 return result; 346 } 347 final XMLParser.ContentContext content = root.content(); 348 if (content == null) { 349 return result; 350 } 351 for (XMLParser.ElementContext child : XmlEditor.elements(root)) { 352 if (!name.equals(child.elmName.getText())) { 353 result.add(child); 354 } 355 } 356 return result; 357 } 358 359 private boolean hasMergeInclude(XMLParser.ElementContext rootView) { 360 return "merge".equals(rootView.elmName.getText()) && filter(rootView, "include").size() > 0; 361 } 362 363 private File stripFileAndGetOriginal(File xml, String binderId) 364 throws ParserConfigurationException, IOException, SAXException, 365 XPathExpressionException { 366 L.d("parsing resource file %s", xml.getAbsolutePath()); 367 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 368 DocumentBuilder builder = factory.newDocumentBuilder(); 369 Document doc = builder.parse(xml); 370 XPathFactory xPathFactory = XPathFactory.newInstance(); 371 XPath xPath = xPathFactory.newXPath(); 372 final XPathExpression commentElementExpr = xPath 373 .compile("//comment()[starts-with(., \" From: file:\")][last()]"); 374 final NodeList commentElementNodes = (NodeList) commentElementExpr 375 .evaluate(doc, XPathConstants.NODESET); 376 L.d("comment element nodes count %s", commentElementNodes.getLength()); 377 if (commentElementNodes.getLength() == 0) { 378 L.d("cannot find comment element to find the actual file"); 379 return null; 380 } 381 final Node first = commentElementNodes.item(0); 382 String actualFilePath = first.getNodeValue().substring(" From:".length()).trim(); 383 L.d("actual file to parse: %s", actualFilePath); 384 File actualFile = urlToFile(new java.net.URL(actualFilePath)); 385 if (!actualFile.canRead()) { 386 L.d("cannot find original, skipping. %s", actualFile.getAbsolutePath()); 387 return null; 388 } 389 390 // now if file has any binding expressions, find and delete them 391 // TODO we should rely on namespace to avoid parsing file twice 392 boolean changed = isBindingLayout(doc, xPath); 393 if (changed) { 394 stripBindingTags(xml, binderId); 395 } 396 return actualFile; 397 } 398 399 private boolean isBindingLayout(Document doc, XPath xPath) throws XPathExpressionException { 400 return !get(doc, xPath, XPATH_BINDING_LAYOUT).isEmpty(); 401 } 402 403 private List<Node> get(Document doc, XPath xPath, String pattern) 404 throws XPathExpressionException { 405 final XPathExpression expr = xPath.compile(pattern); 406 return toList((NodeList) expr.evaluate(doc, XPathConstants.NODESET)); 407 } 408 409 private List<Node> toList(NodeList nodeList) { 410 List<Node> result = new ArrayList<Node>(); 411 for (int i = 0; i < nodeList.getLength(); i++) { 412 result.add(nodeList.item(i)); 413 } 414 return result; 415 } 416 417 private void stripBindingTags(File xml, String newTag) throws IOException { 418 String res = XmlEditor.strip(xml, newTag); 419 if (res != null) { 420 L.d("file %s has changed, overwriting %s", xml.getName(), xml.getAbsolutePath()); 421 FileUtils.writeStringToFile(xml, res); 422 } 423 } 424 425 public static File urlToFile(URL url) throws MalformedURLException { 426 try { 427 return new File(url.toURI()); 428 } catch (IllegalArgumentException e) { 429 MalformedURLException ex = new MalformedURLException(e.getLocalizedMessage()); 430 ex.initCause(e); 431 throw ex; 432 } catch (URISyntaxException e) { 433 return new File(url.getPath()); 434 } 435 } 436 437 private static Map<String, String> attributeMap(XMLParser.ElementContext root) { 438 final Map<String, String> result = new HashMap<>(); 439 for (XMLParser.AttributeContext attr : XmlEditor.attributes(root)) { 440 result.put(escapeQuotes(attr.attrName.getText(), false), 441 escapeQuotes(attr.attrValue.getText(), true)); 442 } 443 return result; 444 } 445 446 private static String escapeQuotes(String textWithQuotes, boolean unescapeValue) { 447 char first = textWithQuotes.charAt(0); 448 int start = 0, end = textWithQuotes.length(); 449 if (first == '"' || first == '\'') { 450 start = 1; 451 } 452 char last = textWithQuotes.charAt(textWithQuotes.length() - 1); 453 if (last == '"' || last == '\'') { 454 end -= 1; 455 } 456 String val = textWithQuotes.substring(start, end); 457 if (unescapeValue) { 458 return StringEscapeUtils.unescapeXml(val); 459 } else { 460 return val; 461 } 462 } 463} 464