@Override public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { ResName attrResName = ResName.qualifyResName(attrName, xmlContext.getPackageName(), "attr"); attributeResources.add(new AttributeResource(attrResName, buf.toString(), xmlContext.getPackageName())); } });
private void loadFile(Path dir) throws IOException { if (!Files.exists(dir)) { throw new RuntimeException("no such directory " + dir); } if (!Files.isDirectory(dir)) { return; } Qualifiers qualifiers; try { qualifiers = Qualifiers.fromParentDir(dir); } catch (IllegalArgumentException e) { Logger.warn(dir + ": " + e.getMessage()); return; } for (Path file : Fs.listFiles(dir, path -> path.getFileName().toString().endsWith(".xml"))) { loadResourceXmlFile(new XmlContext(packageName, file, qualifiers)); } }
@Test public void shouldNotOverwriteValuesWithMatchingQualifiers() { ResBundle bundle = new ResBundle(); XmlContext xmlContext = mock(XmlContext.class); when(xmlContext.getQualifiers()).thenReturn(Qualifiers.parse("--")); when(xmlContext.getConfig()).thenReturn(new ResTable_config()); when(xmlContext.getPackageName()).thenReturn("org.robolectric"); TypedResource firstValue = new TypedResource<>("first_value", ResType.CHAR_SEQUENCE, xmlContext); TypedResource secondValue = new TypedResource<>("second_value", ResType.CHAR_SEQUENCE, xmlContext); bundle.put(new ResName("org.robolectric", "string", "resource_name"), firstValue); bundle.put(new ResName("org.robolectric", "string", "resource_name"), secondValue); assertThat(bundle.get(new ResName("org.robolectric", "string", "resource_name"), from("")).getData()).isEqualTo("first_value"); }
private String configFrom(String path) { Path xmlFile = Paths.get(path, "whatever.xml"); Qualifiers qualifiers = Qualifiers.fromParentDir(xmlFile.getParent()); ResTable_config config = new XmlContext("package", xmlFile, qualifiers) .getConfig(); return config.toString(); }
@Nonnull private static TypedResource<String> createStringTypedResource(String str, String qualifiersStr) { XmlContext mockXmlContext = mock(XmlContext.class); Qualifiers qualifiers = Qualifiers.parse(qualifiersStr); when(mockXmlContext.getQualifiers()).thenReturn(qualifiers); when(mockXmlContext.getConfig()).thenReturn(qualifiers.getConfig()); return new TypedResource<>(str, ResType.CHAR_SEQUENCE, mockXmlContext); }
@Override protected void loadResourceXmlFile(XmlContext xmlContext) { resourceTable.addResource( type, Fs.baseNameFor(xmlContext.getXmlFile()), new FileTypedResource(xmlContext.getXmlFile(), resType, xmlContext)); } }.load(type);
public TypedResource pick(ResName resName, ResTable_config toMatch) { List<TypedResource> values = map.get(resName); if (values == null || values.size() == 0) return null; TypedResource bestMatchSoFar = null; for (TypedResource candidate : values) { ResTable_config candidateConfig = candidate.getConfig(); if (candidateConfig.match(toMatch)) { if (bestMatchSoFar == null || candidateConfig.isBetterThan(bestMatchSoFar.getConfig(), toMatch)) { bestMatchSoFar = candidate; } } } if (Logger.loggingEnabled()) { Logger.debug("Picked '%s' for %s for qualifiers '%s' (%d candidates)", bestMatchSoFar == null ? "<none>" : bestMatchSoFar.getXmlContext().getQualifiers().toString(), resName.getFullyQualifiedName(), toMatch, values.size()); } return bestMatchSoFar; }
public ResTable_config getConfig() { return xmlContext.getConfig(); }
@Override protected void loadResourceXmlFile(XmlContext xmlContext) { Path xmlFile = xmlContext.getXmlFile(); XMLStreamReader xmlStreamReader; try { xmlStreamReader = factory.createXMLStreamReader(Fs.getInputStream(xmlFile)); doParse(xmlStreamReader, xmlContext); } catch (Exception e) { throw new RuntimeException("error parsing " + xmlFile, e); } if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }
public TypedResource(T data, ResType resType, XmlContext xmlContext) { this.data = data; this.resType = resType; this.xmlContext = xmlContext; String qualifiers = xmlContext.getQualifiers(); this.qualifiers = qualifiers == null ? "--" : "-" + qualifiers + "-"; }
public ResTable_config getConfig() { return xmlContext.getConfig(); }
@Override public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { String styleParent = parent; if (styleParent == null) { int lastDot = name.lastIndexOf('.'); if (lastDot != -1) { styleParent = name.substring(0, lastDot); } } StyleData styleData = new StyleData(xmlContext.getPackageName(), name, styleParent, attributeResources); resourceTable.addResource("style", styleData.getName(), new TypedResource<>(styleData, resType, xmlContext)); } }
XmlContext fakeXmlContext = new XmlContext(resourceTable.getPackageName(), f, qualifiers); resourceTable.addResource(type, shortName, new FileTypedResource.Image(f, isNinePatch, fakeXmlContext));
@Override protected void loadResourceXmlFile(XmlContext xmlContext) { resourceTable.addResource(type, xmlContext.getXmlFile().getBaseName(), new FileTypedResource(xmlContext.getXmlFile(), resType, xmlContext)); } }.load(type);
public TypedResource pick(ResName resName, ResTable_config toMatch) { List<TypedResource> values = map.get(resName); if (values == null || values.size() == 0) return null; TypedResource bestMatchSoFar = null; for (TypedResource candidate : values) { ResTable_config candidateConfig = candidate.getConfig(); if (candidateConfig.match(toMatch)) { if (bestMatchSoFar == null || candidateConfig.isBetterThan(bestMatchSoFar.getConfig(), toMatch)) { bestMatchSoFar = candidate; } } } if (Logger.loggingEnabled()) { Logger.debug("Picked '%s' for %s for qualifiers '%s' (%d candidates)", bestMatchSoFar == null ? "<none>" : bestMatchSoFar.getXmlContext().getQualifiers().toString(), resName.getFullyQualifiedName(), toMatch, values.size()); } return bestMatchSoFar; }
final String reference = typedResource.asString(); ResName refResName = AttributeResource.getResourceReference(reference, typedResource.getXmlContext().getPackageName(), null); typedValue.resourceId = resourceTable.getResourceId(refResName); typedValue.data = typedValue.resourceId; final String reference = typedResource.asString(); final ResName attrResName = AttributeResource.getStyleReference(reference, typedResource.getXmlContext().getPackageName(), "attr"); typedValue.data = resourceTable.getResourceId(attrResName);
@Before public void setUp() throws Exception { Path xmlFile = Paths.get("res/values/foo.xml"); Qualifiers qualifiers = Qualifiers.fromParentDir(xmlFile.getParent()); xmlContext = new XmlContext("", xmlFile, qualifiers); }
@Override protected void loadResourceXmlFile(XmlContext xmlContext) { resourceTable.addResource(type, xmlContext.getXmlFile().getBaseName(), new FileTypedResource(xmlContext.getXmlFile(), resType, xmlContext)); } }.load(type);
@Override public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { ResName attrResName = ResName.qualifyResName(attrName, xmlContext.getPackageName(), "attr"); attributeResources.add(new AttributeResource(attrResName, buf.toString(), xmlContext.getPackageName())); } });
private void loadRawFiles(PackageResourceTable resourceTable, String resourceType, Path rawDir) throws IOException { Qualifiers qualifiers; try { qualifiers = Qualifiers.fromParentDir(rawDir); } catch (IllegalArgumentException e) { Logger.warn(rawDir + ": " + e.getMessage()); return; } for (Path file : Fs.listFiles(rawDir)) { String fileBaseName = Fs.baseNameFor(file); resourceTable.addResource( resourceType, fileBaseName, new FileTypedResource( file, ResType.FILE, new XmlContext(resourceTable.getPackageName(), file, qualifiers))); } } }