Code example for AttributeList

Methods: getValue

0
		super.startElement(name, attributes);
		if (!running) {
			throw new SAXException("Parser has been stopped");
		} 
 
		String tagSectionId = attributes.getValue("section-id");
		if (name.equals("content")) {
			currentArticle = new Article();
			currentElementContents = new StringBuilder();
			currentArticle.setId(attributes.getValue("id"));
			currentArticle.setWebUrl(attributes.getValue("web-url"));
			 
			final String sectionId = tagSectionId;
			Section section = sectionDAO.getSectionById(sectionId);
			currentArticle.setSection(section);
 
			final String dateString = attributes.getValue("web-publication-date");
			if (dateString != null) {
				currentArticle.setPubDate(DateTimeHelper.parseDate(dateString));
			} 
		} 
 
		if (name.equals("field")) {
			String fieldname = attributes.getValue("name");
			if (!fieldname.equals(currentField)) {
				currentField = fieldname;
			}		 
		} 
 
		if (name.equals("results")) {
			checksum = attributes.getValue("checksum");
			description = htmlCleaner.stripHtml(attributes.getValue("description"));
			if (articleCallback != null) {
				articleCallback.descriptionReady(description);
			} 
		} 
		 
		if (name.equals("tag")) {
			final String tagType = attributes.getValue("type");
			if (TAG_TYPES_TO_TAKE.contains(tagType)) {
				Section tagSection = null;
				if (tagSectionId != null) {
					tagSection = sectionDAO.getSectionById(tagSectionId);
				} 
				Tag tag = new Tag(attributes.getValue("web-title"), attributes.getValue("id"), tagSection, tagType);
				currentArticle.addTag(tag);
			} 
		} 
 
		if (name.equals("refinement-group")) {
			currentRefinementGroupType = attributes.getValue("type");
		} 
 
		if (name.equals("refinement")) {
			if (currentRefinementGroupType != null) {
				processRefinementTag(attributes);
			} 
		} 
 
		if (name.equals("asset")) {
			currentMediaElement = new MediaElement(attributes.getValue("type"), attributes.getValue("file"));			
		} 
	} 
 
	@Override 
	public void endElement(String name) throws SAXException {
		super.endElement(name);
		 
		if (currentField != null && currentMediaElement == null) {
 
			if (currentField.equals("headline")) {
				currentArticle.setHeadline(htmlCleaner.stripHtml(currentElementContents.toString()));
			} 
 
			if (currentField.equals("byline")) {
				currentArticle.setByline(htmlCleaner.stripHtml(currentElementContents.toString()));
			} 
 
			if (currentField.equals("standfirst")) {
				currentArticle.setStandfirst(htmlCleaner.stripHtml(currentElementContents.toString()));
			} 
 
			if (currentField.equals("thumbnail")) {
				currentArticle.setThumbnailUrl(currentElementContents.toString());
			} 
			 
			if (currentField.equals("short-url")) {			
				currentArticle.setShortUrl(currentElementContents.toString());
			} 
			 
			if (currentField.equals("body")) {
				final String rawBodyText = currentElementContents.toString();
				currentArticle.setRedistributionAllowed(!NO_REDISTRIBUTION_RIGHTS_BODY_TEXT.equals(rawBodyText));
				currentArticle.setDescription(htmlCleaner.stripHtml(rawBodyText));
			} 
			 
			currentField = null;
			currentElementContents = new StringBuilder();
		} 
		 
		if (currentMediaElement != null) {
			if (currentField != null && currentField.equals("thumbnail")) {
				currentMediaElement.setThumbnail(currentElementContents.toString());
			} 
			 
			if (currentField != null && currentField.equals("caption")) {
				currentMediaElement.setCaption(htmlCleaner.stripHtml(currentElementContents.toString()));
			} 
			 
			if (currentField != null && currentField.equals("width")) {
				currentMediaElement.setWidth(Integer.parseInt(currentElementContents.toString()));
			} 
			 
			currentField = null;
			currentElementContents = new StringBuilder();
		} 
				 
		if (name.equals("asset")) {
			currentArticle.addMediaElement(new MediaElement(currentMediaElement));
			currentMediaElement = null;
		} 
 
		if (name.equals("content")) {
			boolean isArticleValid = true; // currentArticle.getId() != null; TODO this needs to be higher up - about and content api have different rules
			if (isArticleValid) {
				articles.add(currentArticle);
				if (articleCallback != null) {
					articleCallback.articleReady(currentArticle);
				} 
				 
			} else { 
				Log.w(TAG, "Ignoring invalid article: " + currentArticle.getHeadline());
			} 
		} 
	} 
 
	@Override 
	public void characters(char[] ch, int start, int length)
			throws SAXException { 
		super.characters(ch, start, length);
		if (currentField != null) {
			for (int i = start; i < start + length; i++) {
				currentElementContents.append(ch[i]);
			} 
		} 
	} 
 
	public void stop() { 
		this.running = false;
	} 
	 
	 
	private void processRefinementTag(AttributeList attributes) {		
		final String refinementId = attributes.getValue("id");
		final String displayName = attributes.getValue("display-name");
		final String refinedUrl = attributes.getValue("refined-url");
		 
		int refinementCount = 0;
		final String refinementCountString = attributes.getValue("count");
		try { 
			refinementCount = Integer.parseInt(refinementCountString);
		} catch (NumberFormatException e) {
			Log.i(TAG, "NumberFormatException while parsing refinement count: " + refinementCount);
		}