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

org.openfuxml.util.query.XmlHtmlQuery Maven / Gradle / Ivy

The newest version!
package org.openfuxml.util.query;

import java.util.List;

import net.sf.exlp.exception.ExlpXpathNotFoundException;
import net.sf.exlp.exception.ExlpXpathNotUniqueException;
import net.sf.exlp.xml.io.Dir;
import net.sf.exlp.xml.io.File;

import org.apache.commons.jxpath.JXPathContext;
import org.openfuxml.xml.renderer.cmp.Html;
import org.openfuxml.xml.renderer.html.Renderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XmlHtmlQuery
{
	final static Logger logger = LoggerFactory.getLogger(XmlHtmlQuery.class);
	
	@SuppressWarnings("unchecked")
	public static synchronized Renderer getRenderer(Html html, String code) throws ExlpXpathNotFoundException, ExlpXpathNotUniqueException
	{
		JXPathContext context = JXPathContext.newContext(html);
		List listResult = (List)context.selectNodes("//renderer[@code='"+code+"']");
		if(listResult.size()==0){throw new ExlpXpathNotFoundException("No "+Renderer.class.getSimpleName()+" found for code="+code);}
		else if(listResult.size()>1){throw new ExlpXpathNotUniqueException("No unique "+Renderer.class.getSimpleName()+" for code="+code);}
		
		return listResult.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public static synchronized File getFile(Dir dirs, String code) throws ExlpXpathNotFoundException, ExlpXpathNotUniqueException
	{
		JXPathContext context = JXPathContext.newContext(dirs);
		List listResult = (List)context.selectNodes("//file[@code='"+code+"']");
		if(listResult.size()==0){throw new ExlpXpathNotFoundException("No file found for code="+code);}
		else if(listResult.size()>1){throw new ExlpXpathNotUniqueException("No unique file for code="+code);}
		
		return listResult.get(0);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy