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

com.smartlogic.classificationserver.client.Result Maven / Gradle / Ivy

There is a newer version: 5.6.1
Show newest version
package com.smartlogic.classificationserver.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Result extends ClassifiableObject {
	protected static final Log logger = LogFactory.getLog(Result.class);

	public Result(byte[] data) throws ClassificationException {
		// If there is no data provided, then just return an empty category list
		if (data == null) return;
		
		try {
			Element element = getRootElement(data);
			
			NodeList nodeList = element.getElementsByTagName("STRUCTUREDDOCUMENT");
			if ((nodeList == null) || (nodeList.getLength() == 0)) {
				throw new ClassificationException("No STRUCTUREDDOCUMENT element returned by classification server: " + toString(data));
			}
			Element structuredDocumentElement = (Element)nodeList.item(0);

			addMetadata(structuredDocumentElement);

			// The hash value can be present as a HASH element on the Structured Document element, as a SYSTEM node, or as a META node
			NodeList hashNodeList = element.getElementsByTagName("HASH");
			if ((hashNodeList != null) && (hashNodeList.getLength() > 0)) {
				Element hashNode = (Element)hashNodeList.item(0);
				setHash(hashNode.getAttribute("value"));
			}
			if (hash == null) getHashFromElements(element, "SYSTEM");
			if (hash == null) getHashFromElements(element, "META");
			
			NodeList articleNodeList = structuredDocumentElement.getElementsByTagName("ARTICLE");
			if (articleNodeList != null) {
				for (int a = 0; a < articleNodeList.getLength(); a++) {
					Element articleElement = (Element)articleNodeList.item(a);
					addArticle(new Article(articleElement));
				}
			}
		} catch (ParserConfigurationException e) {
			throw new ClassificationException("ParserConfigurationException raised: " + e.getMessage());
		} catch (UnsupportedEncodingException e) {
			throw new ClassificationException("UnsupportedEncodingException raised: " + e.getMessage());
		} catch (SAXException e) {
			throw new ClassificationException("SAXException raised: " + e.getMessage() + "\n" + toString(data));
		} catch (IOException e) {
			throw new ClassificationException("IOException raised: " + e.getMessage() + "\n" + toString(data));
		}
	}
	
	
	
	private void getHashFromElements(Element parentElement, String candidateName) {
		NodeList candidateNodeList = parentElement.getElementsByTagName(candidateName);
		if ((candidateNodeList != null) && (candidateNodeList.getLength() > 0)) {
			for (int n = 0; n < candidateNodeList.getLength(); n++) {
				Element candidateElement = (Element)candidateNodeList.item(0);
				if ("HASH".equals(candidateElement.getAttribute("name"))) {
					setHash(candidateElement.getAttribute("value"));
				}
			}
		}
	}



	private List
articles = new LinkedList
(); /** * Return the set of articles within the one results * @return the articles */ public List
getArticles() { return articles; } private void addArticle(Article article) { articles.add(article); } private String hash = null; public String getHash() { return hash; } public void setHash(String hash) { this.hash = hash; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy