Skip to main content
summaryrefslogblamecommitdiffstats
blob: 680651eaca4e5db9bdb3816ad4096ab06b0cce1e (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                                                
                                                            



                                                                        


                                                            

                                                                                 
                                                 
 

                        


                                                             




                                          
                                         






                                                                    
                                                  
 
                                           
 

                                

                                                 

                                  
                                                                                                                              
                                                   
                                           
                                     

         







                                                                                                                         



                                                                                                
 
                                                          
                                                                  
                     
                                                                                                      
                                      







                                                                                                                             
                                
                                                                                                               
                                                          
                                      





                                                                                                                       
                                


                                                                   
                                              





                                                                                                                    
                         





                                                                    


                 
 
                                             
                                                                     

         


                                     
 
 
/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.core;

import java.util.Locale;

import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskDataCollector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Parser for RDF bugzilla query results.
 * 
 * @author Rob Elves
 */
public class SaxBugzillaQueryContentHandler extends DefaultHandler {

	private StringBuffer characters;

	private final TaskDataCollector collector;

	private final String repositoryUrl;

	private int resultCount;

	private final TaskAttributeMapper mapper;

	private TaskData taskData;

	public SaxBugzillaQueryContentHandler(String repositoryUrl, TaskDataCollector collector, TaskAttributeMapper mapper) {
		this.repositoryUrl = repositoryUrl;
		this.collector = collector;
		this.mapper = mapper;
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		characters.append(ch, start, length);
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		characters = new StringBuffer();
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {

		String parsedText = characters.toString();
		BugzillaAttribute tag = BugzillaAttribute.UNKNOWN;
		try {
			tag = BugzillaAttribute.valueOf(localName.trim().toUpperCase(Locale.ENGLISH));
			switch (tag) {
			case QUERY_TIMESTAMP:
				if (collector instanceof BugzillaTaskDataCollector) {
					if (parsedText != null && parsedText.length() > 0) {
						BugzillaTaskDataCollector bCollector = (BugzillaTaskDataCollector) collector;
						bCollector.setQueryTimestamp(parsedText);
					}
				}
				break;
			case ID:
				taskData = new TaskData(mapper, getConnectorKind(), repositoryUrl, parsedText);
				taskData.setPartial(true);
				break;
			case SHORT_SHORT_DESC:
				if (taskData != null) {
					BugzillaTaskDataHandler.createAttribute(taskData, BugzillaAttribute.SHORT_DESC)
							.setValue(parsedText);
				}
				break;
			case LI:
				if (taskData != null) {
					collector.accept(taskData);
				}
				resultCount++;
				break;
			default:
				if (taskData != null) {
					BugzillaTaskDataHandler.createAttribute(taskData, tag).setValue(parsedText);
				}
				break;
			}
		} catch (RuntimeException e) {
			if (e instanceof IllegalArgumentException) {
				// ignore unrecognized tags
				return;
			}
			throw e;
		}

	}

	protected String getConnectorKind() {
		return mapper.getTaskRepository().getConnectorKind();
	}

	public int getResultCount() {
		return resultCount;
	}

}

Back to the top