private Map<String, TtmlStyle> parseHeader( XmlPullParser xmlParser, Map<String, TtmlStyle> globalStyles) throws IOException, XmlPullParserException { do { xmlParser.next(); if (ParserUtil.isStartTag(xmlParser, TtmlNode.TAG_STYLE)) { String parentStyleId = xmlParser.getAttributeValue(null, ATTR_STYLE); TtmlStyle style = parseStyleAttributes(xmlParser, new TtmlStyle()); if (parentStyleId != null) { String[] ids = parseStyleIds(parentStyleId); for (int i = 0; i < ids.length; i++) { style.chain(globalStyles.get(ids[i])); } } if (style.getId() != null) { globalStyles.put(style.getId(), style); } } } while (!ParserUtil.isEndTag(xmlParser, TtmlNode.TAG_HEAD)); return globalStyles; }
private TtmlStyle parseStyleAttributes(XmlPullParser parser, TtmlStyle style) { int attributeCount = parser.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { String attributeName = parser.getAttributeName(i); String attributeValue = parser.getAttributeValue(i); switch (ParserUtil.removeNamespacePrefix(attributeName)) { case TtmlNode.ATTR_ID: if (TtmlNode.TAG_STYLE.equals(parser.getName())) { style = createIfNull(style).setId(attributeValue); } break; case TtmlNode.ATTR_TTS_BACKGROUND_COLOR: style = createIfNull(style); try { style.setBackgroundColor(TtmlColorParser.parseColor(attributeValue)); } catch (IllegalArgumentException e) { Log.w(TAG, "failed parsing background value: '" + attributeValue + "'"); } break; case TtmlNode.ATTR_TTS_COLOR: style = createIfNull(style); try { style.setColor(TtmlColorParser.parseColor(attributeValue)); } catch (IllegalArgumentException e) { Log.w(TAG, "failed parsing color value: '" + attributeValue + "'"); } break; case TtmlNode.ATTR_TTS_FONT_FAMILY: style = createIfNull(style).setFontFamily(attributeValue); break; case TtmlNode.ATTR_TTS_FONT_SIZE: // TODO: handle size break; case TtmlNode.ATTR_TTS_FONT_WEIGHT: style = createIfNull(style).setBold(TtmlNode.BOLD.equalsIgnoreCase(attributeValue)); break; case TtmlNode.ATTR_TTS_FONT_STYLE: style = createIfNull(style).setItalic(TtmlNode.ITALIC.equalsIgnoreCase(attributeValue)); break; case TtmlNode.ATTR_TTS_TEXT_ALIGN: switch (Util.toLowerInvariant(attributeValue)) { case TtmlNode.LEFT: style = createIfNull(style).setTextAlign(Layout.Alignment.ALIGN_NORMAL); break; case TtmlNode.START: style = createIfNull(style).setTextAlign(Layout.Alignment.ALIGN_NORMAL); break; case TtmlNode.RIGHT: style = createIfNull(style).setTextAlign(Layout.Alignment.ALIGN_OPPOSITE); break; case TtmlNode.END: style = createIfNull(style).setTextAlign(Layout.Alignment.ALIGN_OPPOSITE); break; case TtmlNode.CENTER: style = createIfNull(style).setTextAlign(Layout.Alignment.ALIGN_CENTER); break; } break; case TtmlNode.ATTR_TTS_TEXT_DECORATION: switch (Util.toLowerInvariant(attributeValue)) { case TtmlNode.LINETHROUGH: style = createIfNull(style).setLinethrough(true); break; case TtmlNode.NO_LINETHROUGH: style = createIfNull(style).setLinethrough(false); break; case TtmlNode.UNDERLINE: style = createIfNull(style).setUnderline(true); break; case TtmlNode.NO_UNDERLINE: style = createIfNull(style).setUnderline(false); break; } break; default: // ignore break; } } return style; }