All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.robolectric.res.StaxStyleLoader Maven / Gradle / Ivy

There is a newer version: 4.13
Show newest version
package org.robolectric.res;

import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class StaxStyleLoader extends StaxLoader {
  private String name;
  private String parent;
  private List attributeResources;

  public StaxStyleLoader(PackageResourceTable resourceTable, String attrType, ResType resType) {
    super(resourceTable, attrType, resType);

    addHandler("item", new NodeHandler() {
      private String attrName;
      private StringBuilder buf = new StringBuilder();

      @Override
      public void onStart(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException {
        attrName = xml.getAttributeValue(null, "name");
        buf.setLength(0);
      }

      @Override
      public void onCharacters(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException {
        buf.append(xml.getText());
      }

      @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()));
      }
    });
  }

  @Override
  public void onStart(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException {
    name = xml.getAttributeValue(null, "name");
    parent = xml.getAttributeValue(null, "parent");
    attributeResources = new ArrayList<>();
  }

  @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);
      }
    }

    String styleNameWithUnderscores = underscorize(name);
    StyleData styleData = new StyleData(xmlContext.getPackageName(), styleNameWithUnderscores, underscorize(styleParent), attributeResources);

    resourceTable.addResource("style", styleData.getName(), new TypedResource<>(styleData, resType, xmlContext));
  }

  private String underscorize(String s) {
    return s == null ? null : s.replace('.', '_');
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy