Java Code Examples for org.xmlpull.v1.XmlPullParser#getAttributeCount()

The following examples show how to use org.xmlpull.v1.XmlPullParser#getAttributeCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AtomParser.java    From Focus with GNU General Public License v3.0 6 votes vote down vote up
private static Message readFeedLink(XmlPullParser parser) throws IOException, XmlPullParserException {
    String url = "";
    parser.require(XmlPullParser.START_TAG, null, LINK);
    boolean is_false = false;
    for (int i = 0; i < parser.getAttributeCount(); i++) {
        switch (parser.getAttributeName(i)) {
            case "href":
                url = parser.getAttributeType(i);
                break;
            case "rel":
                //直接返回,这个地址是不需要的
                is_false = true;
                break;
        }
        if (is_false) {
            break;
        }
    }
    if (is_false) {
        return new Message(false);
    } else {
        parser.next();
        return new Message(true, url);
    }
}
 
Example 2
Source File: ComponentDescription.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 *
 */
private static String getSingleAttribute(XmlPullParser p, String name)
  throws IllegalXMLException
{
  String res = null;
  for (int i = 0; i < p.getAttributeCount(); i++) {
    if (p.getAttributeName(i).equals(name)) {
      res = p.getAttributeValue(i);
    } else {
      unrecognizedAttr(p, i);
    }
  }

  if (res == null) {
    missingAttr(p, name);
  }
  return res;
}
 
Example 3
Source File: FullBackup.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Let's be strict about the type of xml the client can write. If we see anything untoward,
 * throw an XmlPullParserException.
 */
private void validateInnerTagContents(XmlPullParser parser) throws XmlPullParserException {
    if (parser == null) {
        return;
    }
    switch (parser.getName()) {
        case TAG_INCLUDE:
            if (parser.getAttributeCount() > 3) {
                throw new XmlPullParserException("At most 3 tag attributes allowed for "
                        + "\"include\" tag (\"domain\" & \"path\""
                        + " & optional \"requiredFlags\").");
            }
            break;
        case TAG_EXCLUDE:
            if (parser.getAttributeCount() > 2) {
                throw new XmlPullParserException("At most 2 tag attributes allowed for "
                        + "\"exclude\" tag (\"domain\" & \"path\".");
            }
            break;
        default:
            throw new XmlPullParserException("A valid tag is one of \"<include/>\" or" +
                    " \"<exclude/>. You provided \"" + parser.getName() + "\"");
    }
}
 
Example 4
Source File: AccessoryFilter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public static AccessoryFilter read(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    String manufacturer = null;
    String model = null;
    String version = null;

    int count = parser.getAttributeCount();
    for (int i = 0; i < count; i++) {
        String name = parser.getAttributeName(i);
        String value = parser.getAttributeValue(i);

        if ("manufacturer".equals(name)) {
            manufacturer = value;
        } else if ("model".equals(name)) {
            model = value;
        } else if ("version".equals(name)) {
            version = value;
        }
    }
    return new AccessoryFilter(manufacturer, model, version);
}
 
Example 5
Source File: AtomParser.java    From Focus with GNU General Public License v3.0 6 votes vote down vote up
private static Message readFeedLink(XmlPullParser parser) throws IOException, XmlPullParserException {
    String url = "";
    parser.require(XmlPullParser.START_TAG, null, LINK);
    boolean is_false = false;
    for (int i = 0; i < parser.getAttributeCount(); i++) {
        switch (parser.getAttributeName(i)) {
            case "href":
                url = parser.getAttributeType(i);
                break;
            case "rel":
                //直接返回,这个地址是不需要的
                is_false = true;
                break;
        }
        if (is_false) {
            break;
        }
    }
    if (is_false) {
        return new Message(false);
    } else {
        parser.next();
        return new Message(true, url);
    }
}
 
Example 6
Source File: RepositoryXmlParser.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Data parseData(XmlPullParser p) throws Exception {
  Data d = new Data(p.getAttributeCount() == 1 && "namespace".equals(p.getAttributeName(0))
                    ? p.getAttributeValue(0) : "");
  if (p.isEmptyElementTag()) {
    p.next(); // We still get an END_TAG
    return d;
  }
  int startDepth = p.getDepth();
  p.next();
  while( !(p.getEventType() == XmlPullParser.END_DOCUMENT)
      && !(p.getEventType() == XmlPullParser.END_TAG 
      && p.getDepth() == startDepth)) {
    if (p.getEventType() != XmlPullParser.START_TAG) {
      p.next();
      continue;
    }
    if ("attribute".equalsIgnoreCase(p.getName())) {
      parseAttribute(p, d);
      p.next();
      continue;
    }
    if ("directive".equalsIgnoreCase(p.getName())) {
      parseDirective(p, d);
      p.next();
      continue;
    }
  }
  return d;
}
 
Example 7
Source File: NumberParse.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
static final NumberParse getNumberParseAttr(String name, XmlPullParser attributes) {
    int n = attributes.getAttributeCount();
    for (int i = 0; i < n; i++) {
        if (attributes.getAttributeName(i).equals(name)) {
            return NumberParse.parseNumbers(attributes.getAttributeValue(i));
        }
    }
    return null;
}
 
Example 8
Source File: XhtmlParser.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private XhtmlNode parseNode(XmlPullParser xpp) throws XmlPullParserException, IOException, FHIRFormatError  {
  XhtmlNode res = new XhtmlNode(NodeType.Element);
  res.setName(xpp.getName());
  
  for (int i = 0; i < xpp.getAttributeCount(); i++) {
    String an = "xml".equals(xpp.getAttributePrefix(i)) ? "xml:"+xpp.getAttributeName(i) : xpp.getAttributeName(i);
    String av = xpp.getAttributeValue(i);
    if (attributeIsOk(xpp.getName(), an, av)) {
      res.getAttributes().put(an, av);
    }
  }
  int eventType = xpp.next();
  while (eventType != XmlPullParser.END_TAG) {
    if (eventType == XmlPullParser.TEXT) {
      res.addText(xpp.getText());
      xpp.next();
    } else if (eventType == XmlPullParser.COMMENT) {
      res.addComment(xpp.getText());
      xpp.next();
    } else if (eventType == XmlPullParser.START_TAG) {
      if (elementIsOk(xpp.getName()))
        res.getChildNodes().add(parseNode(xpp));
    } else
      throw new FHIRFormatError("Unhandled XHTML feature: "+Integer.toString(eventType)+descLoc());
    eventType = xpp.getEventType();
  }
  xpp.next();
  return res;
}
 
Example 9
Source File: ManifestPullParser.java    From ic3 with Apache License 2.0 5 votes vote down vote up
private boolean handleProviderStart(XmlPullParser parser) {
  boolean r = handleComponentStart(parser, PROVIDER, Constants.ComponentShortType.PROVIDER);
  String readPermission = currentComponent.getPermission();
  String writePermission = currentComponent.getPermission();
  Set<String> authorities = new HashSet<String>();
  boolean grantUriPermissions = false;

  for (int i = 0; i < parser.getAttributeCount(); ++i) {
    if (!parser.getAttributeNamespace(i).equals(NAMESPACE)) {
      continue;
    }
    String attributeName = parser.getAttributeName(i);
    // permissions
    // Note: readPermission and writePermission attributes take precedence over
    // permission attribute
    // (http://developer.android.com/guide/topics/manifest/provider-element.html).
    if (attributeName.equals(READ_PERMISSION)) {
      readPermission = parser.getAttributeValue(i);
    } else if (attributeName.equals(WRITE_PERMISSION)) {
      writePermission = parser.getAttributeValue(i);
    } else if (attributeName.equals(AUTHORITIES)) {
      // the "AUTHORITIES" attribute contains a list of authorities separated by semicolons.
      String s = parser.getAttributeValue(i);
      for (String a : s.split(";")) {
        authorities.add(a);
      }
    } else if (attributeName.equals(GRANT_URI_PERMISSIONS)) {
      grantUriPermissions = !parser.getAttributeValue(i).equals("false");
    }
  }

  currentComponent =
      new ManifestProviderComponent(currentComponent.getType(), currentComponent.getName(),
          currentComponent.isExported(), currentComponent.isFoundExported(), readPermission,
          writePermission, authorities, grantUriPermissions);
  return r;
}
 
Example 10
Source File: Util.java    From ssj with GNU General Public License v3.0 5 votes vote down vote up
public static String xmlToString(XmlPullParser parser) throws XmlPullParserException, IOException
{
    if (parser.getEventType() == XmlPullParser.TEXT)
    {
        return parser.getText();
    }

    int depth = parser.getDepth();
    String tag;
    StringBuilder str = new StringBuilder();
    do
    {
        if (parser.getEventType() == XmlPullParser.START_TAG)
        {
            str.append("<").append(parser.getName());

            //add attributes
            for (int i = 0; i < parser.getAttributeCount(); ++i)
                str.append(" ").append(parser.getAttributeName(i)).append("=").append("\"").append(parser.getAttributeValue(i)).append("\"");
            str.append(">");

            //go deeper
            parser.next();
            str.append(xmlToString(parser));
        }

        if (parser.getEventType() == XmlPullParser.END_TAG)
        {
            str.append("</").append(parser.getName()).append(">");
        }

        parser.next();
    }
    while (parser.getEventType() != XmlPullParser.END_DOCUMENT && parser.getDepth() >= depth);

    return str.toString();
}
 
Example 11
Source File: XmlTvParser.java    From xipl with Apache License 2.0 5 votes vote down vote up
private static Advertisement parseAd(XmlPullParser parser, String adType)
        throws IOException, XmlPullParserException, ParseException {
    Long startTimeUtcMillis = null;
    Long stopTimeUtcMillis = null;
    int type = Advertisement.TYPE_VAST;
    for (int i = 0; i < parser.getAttributeCount(); ++i) {
        String attr = parser.getAttributeName(i);
        String value = parser.getAttributeValue(i);
        if (ATTR_AD_START.equalsIgnoreCase(attr)) {
            startTimeUtcMillis = DATE_FORMAT.parse(value).getTime();
        } else if (ATTR_AD_STOP.equalsIgnoreCase(attr)) {
            stopTimeUtcMillis = DATE_FORMAT.parse(value).getTime();
        } else if (ATTR_AD_TYPE.equalsIgnoreCase(attr)) {
            if (VALUE_ADVERTISEMENT_TYPE_VAST.equalsIgnoreCase(attr)) {
                type = Advertisement.TYPE_VAST;
            }
        }
    }
    String requestUrl = null;
    while (parser.next() != XmlPullParser.END_DOCUMENT) {
        if (parser.getEventType() == XmlPullParser.START_TAG) {
            if (TAG_REQUEST_URL.equalsIgnoreCase(parser.getName())) {
                requestUrl = parser.nextText();
            }
        } else if (TAG_AD.equalsIgnoreCase(parser.getName())
                && parser.getEventType() == XmlPullParser.END_TAG) {
            break;
        }
    }
    Advertisement.Builder builder = new Advertisement.Builder();
    if (adType.equals(TAG_PROGRAM)) {
        if (startTimeUtcMillis == null || stopTimeUtcMillis == null) {
            throw new IllegalArgumentException(
                    "start, stop time of program ads cannot be null");
        }
        builder.setStartTimeUtcMillis(startTimeUtcMillis);
        builder.setStopTimeUtcMillis(stopTimeUtcMillis);
    }
    return builder.setType(type).setRequestUrl(requestUrl).build();
}
 
Example 12
Source File: ManifestPullParser.java    From ic3 with Apache License 2.0 5 votes vote down vote up
private boolean handleDataStart(XmlPullParser parser) {
  if (currentIntentFilter != null) {
    ManifestData manifestData = new ManifestData();

    for (int i = 0; i < parser.getAttributeCount(); ++i) {
      if (!parser.getAttributeNamespace(i).equals(NAMESPACE)) {
        continue;
      }
      String attributeName = parser.getAttributeName(i);
      String attributeValue = parser.getAttributeValue(i);
      if (attributeName.equals(MIME_TYPE)) {
        manifestData.setMimeType(attributeValue);
      } else if (attributeName.equals(SCHEME)) {
        manifestData.setScheme(attributeValue);
      } else if (attributeName.equals(HOST)) {
        manifestData.setHost(attributeValue);
      } else if (attributeName.equals(PORT)) {
        manifestData.setPort(attributeValue);
      } else if (attributeName.equals(PATH)) {
        manifestData.setPath(attributeValue);
      } else if (attributeName.equals(PATH_PATTERN)) {
        manifestData.setPath(attributeValue);
      } else if (attributeName.equals(PATH_PREFIX)) {
        manifestData.setPath(String.format("%s(.*)", attributeValue));
      }
    }

    currentIntentFilter.addData(manifestData);
  }

  return true;
}
 
Example 13
Source File: XmlPullParserUtil.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the value of an attribute of the current start tag. Any raw attribute names in the
 * current start tag have their prefixes stripped before matching.
 *
 * @param xpp The {@link XmlPullParser} to query.
 * @param attributeName The name of the attribute.
 * @return The value of the attribute, or null if the current event is not a start tag or if no
 *     such attribute was found.
 */
public static @Nullable String getAttributeValueIgnorePrefix(
    XmlPullParser xpp, String attributeName) {
  int attributeCount = xpp.getAttributeCount();
  for (int i = 0; i < attributeCount; i++) {
    if (stripPrefix(xpp.getAttributeName(i)).equals(attributeName)) {
      return xpp.getAttributeValue(i);
    }
  }
  return null;
}
 
Example 14
Source File: DashManifestParser.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses an event object.
 *
 * @param xpp The current xml parser.
 * @param scratchOutputStream A {@link ByteArrayOutputStream} that's used when parsing the object.
 * @return The serialized byte array.
 * @throws XmlPullParserException If there is any error parsing this node.
 * @throws IOException If there is any error reading from the underlying input stream.
 */
protected byte[] parseEventObject(XmlPullParser xpp, ByteArrayOutputStream scratchOutputStream)
    throws XmlPullParserException, IOException {
  scratchOutputStream.reset();
  XmlSerializer xmlSerializer = Xml.newSerializer();
  xmlSerializer.setOutput(scratchOutputStream, C.UTF8_NAME);
  // Start reading everything between <Event> and </Event>, and serialize them into an Xml
  // byte array.
  xpp.nextToken();
  while (!XmlPullParserUtil.isEndTag(xpp, "Event")) {
    switch (xpp.getEventType()) {
      case (XmlPullParser.START_DOCUMENT):
        xmlSerializer.startDocument(null, false);
        break;
      case (XmlPullParser.END_DOCUMENT):
        xmlSerializer.endDocument();
        break;
      case (XmlPullParser.START_TAG):
        xmlSerializer.startTag(xpp.getNamespace(), xpp.getName());
        for (int i = 0; i < xpp.getAttributeCount(); i++) {
          xmlSerializer.attribute(xpp.getAttributeNamespace(i), xpp.getAttributeName(i),
              xpp.getAttributeValue(i));
        }
        break;
      case (XmlPullParser.END_TAG):
        xmlSerializer.endTag(xpp.getNamespace(), xpp.getName());
        break;
      case (XmlPullParser.TEXT):
        xmlSerializer.text(xpp.getText());
        break;
      case (XmlPullParser.CDSECT):
        xmlSerializer.cdsect(xpp.getText());
        break;
      case (XmlPullParser.ENTITY_REF):
        xmlSerializer.entityRef(xpp.getText());
        break;
      case (XmlPullParser.IGNORABLE_WHITESPACE):
        xmlSerializer.ignorableWhitespace(xpp.getText());
        break;
      case (XmlPullParser.PROCESSING_INSTRUCTION):
        xmlSerializer.processingInstruction(xpp.getText());
        break;
      case (XmlPullParser.COMMENT):
        xmlSerializer.comment(xpp.getText());
        break;
      case (XmlPullParser.DOCDECL):
        xmlSerializer.docdecl(xpp.getText());
        break;
      default: // fall out
    }
    xpp.nextToken();
  }
  xmlSerializer.flush();
  return scratchOutputStream.toByteArray();
}
 
Example 15
Source File: TtmlDecoder.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private TtmlNode parseNode(XmlPullParser parser, TtmlNode parent,
    Map<String, TtmlRegion> regionMap, FrameAndTickRate frameAndTickRate)
    throws SubtitleDecoderException {
  long duration = C.TIME_UNSET;
  long startTime = C.TIME_UNSET;
  long endTime = C.TIME_UNSET;
  String regionId = TtmlNode.ANONYMOUS_REGION_ID;
  String imageId = null;
  String[] styleIds = null;
  int attributeCount = parser.getAttributeCount();
  TtmlStyle style = parseStyleAttributes(parser, null);
  for (int i = 0; i < attributeCount; i++) {
    String attr = parser.getAttributeName(i);
    String value = parser.getAttributeValue(i);
    switch (attr) {
      case ATTR_BEGIN:
        startTime = parseTimeExpression(value, frameAndTickRate);
        break;
      case ATTR_END:
        endTime = parseTimeExpression(value, frameAndTickRate);
        break;
      case ATTR_DURATION:
        duration = parseTimeExpression(value, frameAndTickRate);
        break;
      case ATTR_STYLE:
        // IDREFS: potentially multiple space delimited ids
        String[] ids = parseStyleIds(value);
        if (ids.length > 0) {
          styleIds = ids;
        }
        break;
      case ATTR_REGION:
        if (regionMap.containsKey(value)) {
          // If the region has not been correctly declared or does not define a position, we use
          // the anonymous region.
          regionId = value;
        }
        break;
      case ATTR_IMAGE:
        // Parse URI reference only if refers to an element in the same document (it must start
        // with '#'). Resolving URIs from external sources is not supported.
        if (value.startsWith("#")) {
          imageId = value.substring(1);
        }
        break;
      default:
        // Do nothing.
        break;
    }
  }
  if (parent != null && parent.startTimeUs != C.TIME_UNSET) {
    if (startTime != C.TIME_UNSET) {
      startTime += parent.startTimeUs;
    }
    if (endTime != C.TIME_UNSET) {
      endTime += parent.startTimeUs;
    }
  }
  if (endTime == C.TIME_UNSET) {
    if (duration != C.TIME_UNSET) {
      // Infer the end time from the duration.
      endTime = startTime + duration;
    } else if (parent != null && parent.endTimeUs != C.TIME_UNSET) {
      // If the end time remains unspecified, then it should be inherited from the parent.
      endTime = parent.endTimeUs;
    }
  }
  return TtmlNode.buildNode(
      parser.getName(), startTime, endTime, style, styleIds, regionId, imageId);
}
 
Example 16
Source File: XmlTvParser.java    From androidtv-sample-inputs with Apache License 2.0 4 votes vote down vote up
private static Program parseProgram(XmlPullParser parser)
        throws IOException, XmlPullParserException, ParseException {
    String channelId = null;
    Long startTimeUtcMillis = null;
    Long endTimeUtcMillis = null;
    String videoSrc = null;
    int videoType = TvContractUtils.SOURCE_TYPE_HTTP_PROGRESSIVE;
    for (int i = 0; i < parser.getAttributeCount(); ++i) {
        String attr = parser.getAttributeName(i);
        String value = parser.getAttributeValue(i);
        if (ATTR_CHANNEL.equalsIgnoreCase(attr)) {
            channelId = value;
        } else if (ATTR_START.equalsIgnoreCase(attr)) {
            startTimeUtcMillis = DATE_FORMAT.parse(value).getTime();
        } else if (ATTR_STOP.equalsIgnoreCase(attr)) {
            endTimeUtcMillis = DATE_FORMAT.parse(value).getTime();
        } else if (ATTR_VIDEO_SRC.equalsIgnoreCase(attr)) {
            videoSrc = value;
        } else if (ATTR_VIDEO_TYPE.equalsIgnoreCase(attr)) {
            if (VALUE_VIDEO_TYPE_HTTP_PROGRESSIVE.equals(value)) {
                videoType = TvContractUtils.SOURCE_TYPE_HTTP_PROGRESSIVE;
            } else if (VALUE_VIDEO_TYPE_HLS.equals(value)) {
                videoType = TvContractUtils.SOURCE_TYPE_HLS;
            } else if (VALUE_VIDEO_TYPE_MPEG_DASH.equals(value)) {
                videoType = TvContractUtils.SOURCE_TYPE_MPEG_DASH;
            }
        }
    }
    String title = null;
    String description = null;
    XmlTvIcon icon = null;
    List<String> category = new ArrayList<>();
    List<TvContentRating> rating = new ArrayList<>();
    List<Advertisement> ads = new ArrayList<>();
    while (parser.next() != XmlPullParser.END_DOCUMENT) {
        String tagName = parser.getName();
        if (parser.getEventType() == XmlPullParser.START_TAG) {
            if (TAG_TITLE.equalsIgnoreCase(parser.getName())) {
                title = parser.nextText();
            } else if (TAG_DESC.equalsIgnoreCase(tagName)) {
                description = parser.nextText();
            } else if (TAG_ICON.equalsIgnoreCase(tagName)) {
                icon = parseIcon(parser);
            } else if (TAG_CATEGORY.equalsIgnoreCase(tagName)) {
                category.add(parser.nextText());
            } else if (TAG_RATING.equalsIgnoreCase(tagName)) {
                TvContentRating xmlTvRating = xmlTvRatingToTvContentRating(parseRating(parser));
                if (xmlTvRating != null) {
                    rating.add(xmlTvRating);
                }
            } else if (TAG_AD.equalsIgnoreCase(tagName)) {
                ads.add(parseAd(parser, TAG_PROGRAM));
            }
        } else if (TAG_PROGRAM.equalsIgnoreCase(tagName)
                && parser.getEventType() == XmlPullParser.END_TAG) {
            break;
        }
    }
    if (TextUtils.isEmpty(channelId)
            || startTimeUtcMillis == null
            || endTimeUtcMillis == null) {
        throw new IllegalArgumentException("channel, start, and end can not be null.");
    }
    InternalProviderData internalProviderData = new InternalProviderData();
    internalProviderData.setVideoType(videoType);
    internalProviderData.setVideoUrl(videoSrc);
    internalProviderData.setAds(ads);
    return new Program.Builder()
            .setChannelId(channelId.hashCode())
            .setTitle(title)
            .setDescription(description)
            .setPosterArtUri(icon.src)
            .setCanonicalGenres(category.toArray(new String[category.size()]))
            .setStartTimeUtcMillis(startTimeUtcMillis)
            .setEndTimeUtcMillis(endTimeUtcMillis)
            .setContentRatings(rating.toArray(new TvContentRating[rating.size()]))
            // NOTE: {@code COLUMN_INTERNAL_PROVIDER_DATA} is a private field
            // where TvInputService can store anything it wants. Here, we store
            // video type and video URL so that TvInputService can play the
            // video later with this field.
            .setInternalProviderData(internalProviderData)
            .build();
}
 
Example 17
Source File: TtmlDecoder.java    From K-Sonic with MIT License 4 votes vote down vote up
private TtmlNode parseNode(XmlPullParser parser, TtmlNode parent,
    Map<String, TtmlRegion> regionMap, FrameAndTickRate frameAndTickRate)
    throws SubtitleDecoderException {
  long duration = C.TIME_UNSET;
  long startTime = C.TIME_UNSET;
  long endTime = C.TIME_UNSET;
  String regionId = TtmlNode.ANONYMOUS_REGION_ID;
  String[] styleIds = null;
  int attributeCount = parser.getAttributeCount();
  TtmlStyle style = parseStyleAttributes(parser, null);
  for (int i = 0; i < attributeCount; i++) {
    String attr = parser.getAttributeName(i);
    String value = parser.getAttributeValue(i);
    switch (attr) {
      case ATTR_BEGIN:
        startTime = parseTimeExpression(value, frameAndTickRate);
        break;
      case ATTR_END:
        endTime = parseTimeExpression(value, frameAndTickRate);
        break;
      case ATTR_DURATION:
        duration = parseTimeExpression(value, frameAndTickRate);
        break;
      case ATTR_STYLE:
        // IDREFS: potentially multiple space delimited ids
        String[] ids = parseStyleIds(value);
        if (ids.length > 0) {
          styleIds = ids;
        }
        break;
      case ATTR_REGION:
        if (regionMap.containsKey(value)) {
          // If the region has not been correctly declared or does not define a position, we use
          // the anonymous region.
          regionId = value;
        }
        break;
      default:
        // Do nothing.
        break;
    }
  }
  if (parent != null && parent.startTimeUs != C.TIME_UNSET) {
    if (startTime != C.TIME_UNSET) {
      startTime += parent.startTimeUs;
    }
    if (endTime != C.TIME_UNSET) {
      endTime += parent.startTimeUs;
    }
  }
  if (endTime == C.TIME_UNSET) {
    if (duration != C.TIME_UNSET) {
      // Infer the end time from the duration.
      endTime = startTime + duration;
    } else if (parent != null && parent.endTimeUs != C.TIME_UNSET) {
      // If the end time remains unspecified, then it should be inherited from the parent.
      endTime = parent.endTimeUs;
    }
  }
  return TtmlNode.buildNode(parser.getName(), startTime, endTime, style, styleIds, regionId);
}
 
Example 18
Source File: XmlToJson.java    From XmlToJson with Apache License 2.0 4 votes vote down vote up
private void readTags(Tag parent, XmlPullParser xpp) {
    try {
        int eventType;
        do {
            eventType = xpp.next();
            if (eventType == XmlPullParser.START_TAG) {
                String tagName = xpp.getName();
                String path = parent.getPath() + "/" + tagName;

                boolean skipTag = mSkippedTags.contains(path);

                Tag child = new Tag(path, tagName);
                if (!skipTag) {
                    parent.addChild(child);
                }

                // Attributes are taken into account as key/values in the child
                int attrCount = xpp.getAttributeCount();
                for (int i = 0; i < attrCount; ++i) {
                    String attrName = xpp.getAttributeName(i);
                    String attrValue = xpp.getAttributeValue(i);
                    String attrPath = parent.getPath() + "/" + child.getName() + "/" + attrName;

                    // Skip Attributes
                    if (mSkippedAttributes.contains(attrPath)) {
                        continue;
                    }

                    attrName = getAttributeNameReplacement(attrPath, attrName);
                    Tag attribute = new Tag(attrPath, attrName);
                    attribute.setContent(attrValue);
                    child.addChild(attribute);
                }

                readTags(child, xpp);
            } else if (eventType == XmlPullParser.TEXT) {
                String text = xpp.getText();
                parent.setContent(text);
            } else if (eventType == XmlPullParser.END_TAG) {
                return;
            } else if (eventType == XmlPullParser.END_DOCUMENT) {
                return;
            } else {
                Log.i(TAG, "unknown xml eventType " + eventType);
            }
        } while (eventType != XmlPullParser.END_DOCUMENT);
    } catch (XmlPullParserException | IOException | NullPointerException e) {
        e.printStackTrace();
    }
}
 
Example 19
Source File: AnimatorInflater.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static StateListAnimator createStateListAnimatorFromXml(Context context,
        XmlPullParser parser, AttributeSet attributeSet)
        throws IOException, XmlPullParserException {
    int type;
    StateListAnimator stateListAnimator = new StateListAnimator();

    while (true) {
        type = parser.next();
        switch (type) {
            case XmlPullParser.END_DOCUMENT:
            case XmlPullParser.END_TAG:
                return stateListAnimator;

            case XmlPullParser.START_TAG:
                // parse item
                Animator animator = null;
                if ("item".equals(parser.getName())) {
                    int attributeCount = parser.getAttributeCount();
                    int[] states = new int[attributeCount];
                    int stateIndex = 0;
                    for (int i = 0; i < attributeCount; i++) {
                        int attrName = attributeSet.getAttributeNameResource(i);
                        if (attrName == R.attr.animation) {
                            final int animId = attributeSet.getAttributeResourceValue(i, 0);
                            animator = loadAnimator(context, animId);
                        } else {
                            states[stateIndex++] =
                                    attributeSet.getAttributeBooleanValue(i, false) ?
                                            attrName : -attrName;
                        }
                    }
                    if (animator == null) {
                        animator = createAnimatorFromXml(context.getResources(),
                                context.getTheme(), parser, 1f);
                    }

                    if (animator == null) {
                        throw new Resources.NotFoundException(
                                "animation state item must have a valid animation");
                    }
                    stateListAnimator
                            .addState(StateSet.trimStateSet(states, stateIndex), animator);
                }
                break;
        }
    }
}
 
Example 20
Source File: TtmlDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private TtmlNode parseNode(XmlPullParser parser, TtmlNode parent,
    Map<String, TtmlRegion> regionMap, FrameAndTickRate frameAndTickRate)
    throws SubtitleDecoderException {
  long duration = C.TIME_UNSET;
  long startTime = C.TIME_UNSET;
  long endTime = C.TIME_UNSET;
  String regionId = TtmlNode.ANONYMOUS_REGION_ID;
  String imageId = null;
  String[] styleIds = null;
  int attributeCount = parser.getAttributeCount();
  TtmlStyle style = parseStyleAttributes(parser, null);
  for (int i = 0; i < attributeCount; i++) {
    String attr = parser.getAttributeName(i);
    String value = parser.getAttributeValue(i);
    switch (attr) {
      case ATTR_BEGIN:
        startTime = parseTimeExpression(value, frameAndTickRate);
        break;
      case ATTR_END:
        endTime = parseTimeExpression(value, frameAndTickRate);
        break;
      case ATTR_DURATION:
        duration = parseTimeExpression(value, frameAndTickRate);
        break;
      case ATTR_STYLE:
        // IDREFS: potentially multiple space delimited ids
        String[] ids = parseStyleIds(value);
        if (ids.length > 0) {
          styleIds = ids;
        }
        break;
      case ATTR_REGION:
        if (regionMap.containsKey(value)) {
          // If the region has not been correctly declared or does not define a position, we use
          // the anonymous region.
          regionId = value;
        }
        break;
      case ATTR_IMAGE:
        // Parse URI reference only if refers to an element in the same document (it must start
        // with '#'). Resolving URIs from external sources is not supported.
        if (value.startsWith("#")) {
          imageId = value.substring(1);
        }
        break;
      default:
        // Do nothing.
        break;
    }
  }
  if (parent != null && parent.startTimeUs != C.TIME_UNSET) {
    if (startTime != C.TIME_UNSET) {
      startTime += parent.startTimeUs;
    }
    if (endTime != C.TIME_UNSET) {
      endTime += parent.startTimeUs;
    }
  }
  if (endTime == C.TIME_UNSET) {
    if (duration != C.TIME_UNSET) {
      // Infer the end time from the duration.
      endTime = startTime + duration;
    } else if (parent != null && parent.endTimeUs != C.TIME_UNSET) {
      // If the end time remains unspecified, then it should be inherited from the parent.
      endTime = parent.endTimeUs;
    }
  }
  return TtmlNode.buildNode(
      parser.getName(), startTime, endTime, style, styleIds, regionId, imageId);
}