XMLReader.java

85 lines | 2.97 kB Blame History Raw Download
/*
 * Copyright 2010-2011 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.ning.billing.catalog.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

import com.ning.billing.catalog.Catalog;
import com.ning.billing.catalog.Duration;
import com.ning.billing.catalog.Plan;
import com.ning.billing.catalog.PlanPhase;
import com.ning.billing.catalog.PriceList;
import com.ning.billing.catalog.Product;
import com.ning.billing.catalog.ProductType;
import com.ning.billing.catalog.ValidatingConfig.ValidationErrors;
import com.ning.billing.catalog.api.InvalidConfigException;

public class XMLReader {


    public static Catalog getCatalogFromName(URL url) throws SAXException, InvalidConfigException, JAXBException {
        JAXBContext context =JAXBContext.newInstance(Catalog.class,Plan.class,Duration.class, Product.class, ProductType.class, PlanPhase.class, PriceList.class);

        InputStream resourceStream = XMLReader.class.getResourceAsStream("/CatalogSchema.xsd");
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI );
        Schema schema = factory.newSchema(new StreamSource(resourceStream));

        Unmarshaller um = context.createUnmarshaller();
        um.setSchema(schema);

        Object o = um.unmarshal(url);

        if(o instanceof Catalog) {
            Catalog c = (Catalog)o;
            c.setCatalogURL(url);
            c.initialize(c);
            ValidationErrors errs = c.validate();
            System.out.println("Errors: " + errs.size() + " for " + url);
            return (Catalog) o;
        } else {
            return null;
        }
    }
	/**
	 * @param args
	 * @throws SAXException
	 * @throws InvalidConfigException
	 */
	public static void main(String[] args) throws IOException, TransformerException, JAXBException, SAXException, InvalidConfigException {
		String curDir = System.getProperty("user.dir");
		getCatalogFromName(new File("src/test/resources/WeaponsHire.xml").toURI().toURL());
		getCatalogFromName(new File("src/test/resources/WeaponsHireSmall.xml").toURI().toURL());

	}
	
	
}