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

org.apache.cxf.jaxrs.utils.schemas.SchemaHandler Maven / Gradle / Ivy

There is a newer version: 2.7.18
Show newest version
/**
 * 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.jaxrs.utils.schemas;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.common.WSDLConstants;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.jaxrs.utils.ResourceUtils;

public class SchemaHandler {

    private static final Logger LOG = LogUtils.getL7dLogger(SchemaHandler.class);
    
    private Schema schema;
    private Bus bus;
    
    public SchemaHandler() {
        
    }
    
    public void setBus(Bus b) {
        bus = b;
    }
    
    public void setSchemas(List locations) {
        schema = createSchema(locations, bus == null ? BusFactory.getThreadDefaultBus() : bus);
    }
    
    public Schema getSchema() {
        return schema;
    }
    
    public static Schema createSchema(List locations, Bus bus) {
        
        SchemaFactory factory = SchemaFactory.newInstance(WSDLConstants.NS_SCHEMA_XSD);
        Schema s = null;
        try {
            List sources = new ArrayList();
            for (String loc : locations) {
                URL url = ResourceUtils.getResourceURL(loc, bus);
                if (url == null) {
                    return null;
                }
                Reader r = new BufferedReader(
                               new InputStreamReader(url.openStream(), "UTF-8"));
                StreamSource source = new StreamSource(r);
                source.setSystemId(url.toString());
                sources.add(source);
            }
            s = factory.newSchema(sources.toArray(new Source[]{}));
        } catch (Exception ex) {
            LOG.warning("Validation will be disabled, failed to create schema : " + ex.getMessage());
        }
        return s;
        
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy