/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF 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 org.apache.cxf.jibx.tools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.w3c.dom.Element;

import org.xml.sax.InputSource;

import org.apache.cxf.common.xmlschema.SchemaCollection;
import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.resolver.URIResolver;
import org.jibx.schema.ISchemaResolver;

/**
 * A Wrapper class that acts as a wrapper when passing schema instances to JiBX code generation framework.
 */
public class JibxSchemaResolver implements ISchemaResolver {
    private String id;
    private String name;
    private XmlSchema schema;
    private Element element;
    private SchemaCollection collection;

    public JibxSchemaResolver(String id, XmlSchema schema, SchemaCollection collection, Element element) {
        this.id = id;
        setName(id);
        this.schema = schema;
        this.collection = collection;
        this.element = element;
    }

    public InputStream getContent() throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        schema.write(bos);
        return new ByteArrayInputStream(bos.toByteArray());
    }

    public String getId() {
        return id;
    }
    
    public Element getElement() {
        return element;
    }

    private void setName(String uri) {
        this.name =  uri;
        //this.name = uri.substring(uri.lastIndexOf('/') + 1);
    }
    
    public String getName() {
        return name;
    }

    public ISchemaResolver resolve(String loc, String tns) throws IOException {
        URIResolver resolver = collection.getXmlSchemaCollection().getSchemaResolver();
        InputSource source = resolver.resolveEntity(tns, loc, id);
        
        SchemaCollection schemaCol = new SchemaCollection();
        schemaCol.setSchemaResolver(resolver);
        XmlSchema read = schemaCol.getXmlSchemaCollection().read(source);
        
        String uri = loc;
        try {
            URL url = new URL(new URL(id), loc);
            uri = url.toURI().toString();
        } catch (Exception e) {
            // do nothing
        }
        return new JibxSchemaResolver(uri, read, schemaCol, null);
    }
}
