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

org.jboss.windup.graph.dao.XmlResourceDao Maven / Gradle / Ivy

The newest version!
package org.jboss.windup.graph.dao;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.io.IOUtils;
import org.jboss.windup.engine.util.xml.LocationAwareXmlReader;
import org.jboss.windup.graph.model.meta.xml.NamespaceMeta;
import org.jboss.windup.graph.model.resource.ArchiveEntryResource;
import org.jboss.windup.graph.model.resource.Resource;
import org.jboss.windup.graph.model.resource.XmlResource;
import org.w3c.dom.Document;

import com.google.common.collect.Iterables;

public class XmlResourceDao extends BaseDao {

	public XmlResourceDao() {
		super(XmlResource.class);
	}

	@Inject
	private ArchiveEntryDao archiveEntryDao;
	
	@Inject
	private NamespaceDao namespaceDao;

	public Iterable containsNamespaceURI(String namespaceURI) {
		
		List> result = new LinkedList>();
		for(NamespaceMeta resource : namespaceDao.findByURI(namespaceURI)) {
			result.add(resource.getXmlResources());
		}
		
		//now, check thether it is null.
		if(result == null || result.size() == 0) {
			return new LinkedList();
		}
		return Iterables.concat(result);
	}
	
	public Iterable findByRootTag(String rootTagName) {
		return getByProperty("rootTagName", rootTagName);
	}
	
	
	public Document asDocument(XmlResource resource) throws RuntimeException {
		Resource underlyingResource = resource.getResource();
		if(underlyingResource instanceof ArchiveEntryResource) {
			InputStream is = null;
			try {
				is = archiveEntryDao.asInputStream((ArchiveEntryResource)underlyingResource);
				Document parsedDocument = LocationAwareXmlReader.readXML(is);
				return parsedDocument;
			}
			catch(Exception e) {
				throw new RuntimeException("Exception reading document.", e);
			}
			finally {
				IOUtils.closeQuietly(is);
			}
		}

		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy