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

net.sf.andromedaioc.model.builder.xml.XmlContextBeansProvider Maven / Gradle / Ivy

The newest version!
package net.sf.andromedaioc.model.builder.xml;

import net.sf.andromedaioc.exception.XmlParseException;
import net.sf.andromedaioc.model.beans.*;
import net.sf.andromedaioc.model.provider.Provider;
import net.sf.andromedaioc.resource.ResourceProvider;
import net.sf.andromedaioc.util.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.*;
import java.util.*;

public class XmlContextBeansProvider implements Provider> {

    private final String xmlContext;
    private final ResourceProvider resourceProvider;
    private final Provider generatedBeanIdProvider;
    private final Map beans;

    public XmlContextBeansProvider(String xmlContext, ResourceProvider resourceProvider, Provider generatedBeanIdProvider) {
        this.xmlContext = xmlContext;
        this.resourceProvider = resourceProvider;
        this.generatedBeanIdProvider = generatedBeanIdProvider;
        beans = new HashMap();
    }

    public Map provide() {

        try {
            ByteArrayInputStream xmlStream = readXml();
            // TODO: problem with xsd validation for android, so have to disable it
            // validateXmlResource(xmlStream);
            xmlStream.reset();
            parseXmlResource(xmlStream);
            return beans;
        } catch(Exception e) {
            throw new XmlParseException(String.format("Error parsing xml context. Reason: %s", e.getMessage()), e);
        }
    }

    private ByteArrayInputStream readXml() {
        InputStream contextResource = null;
        try {
            contextResource = (InputStream) resourceProvider.getResource(xmlContext);
            byte[] buffer = new byte[1000];
            int bytesRead;
            byte[] result = new byte[0];
            while((bytesRead = contextResource.read(buffer, 0, 1000)) >= 0) {
                int currentResultLength = result.length;
                byte[] newResult = new byte[currentResultLength + bytesRead];
                System.arraycopy(result, 0, newResult, 0, currentResultLength);
                System.arraycopy(buffer, 0, newResult, currentResultLength, bytesRead);
                result = newResult;
            }
            return new ByteArrayInputStream(result);
        } catch(Exception e) {
            throw new XmlParseException(String.format("Error parsing xml context. Reason: %s", e.getMessage()), e);
        } finally {
            if(contextResource != null) {
                try {
                    contextResource.close();
                } catch(Exception e) {
                    throw new RuntimeException(String.format("Failed close xml context stream. Reason: %s", e.getMessage()), e);
                }
            }
        }
    }

    private void validateXmlResource(InputStream resource) throws Exception {
        AndromedaXmlValidator validator = new AndromedaXmlValidator();
        validator.validate(resource);
    }

    private void parseXmlResource(InputStream resource) throws Exception {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(resource);
        doc.getDocumentElement().normalize();
        Element element = doc.getDocumentElement();
        if(!XmlContextElement.BEANS.getName().equals(element.getTagName())) {
            throw new XmlParseException("No root document element found");
        }
        NodeList childNodes = element.getChildNodes();
        for(int i=0; i collectItems(Node collectionNode) throws Exception {
        List items = new ArrayList();
        NodeList childNodes = collectionNode.getChildNodes();
        for(int i=0; i collectEntries(Node mapNode) throws Exception {
        List entries = new ArrayList();
        NodeList childNodes = mapNode.getChildNodes();
        for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy