001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.validator;
018
019 import java.io.InputStream;
020 import java.util.Map;
021
022 import javax.xml.transform.stream.StreamSource;
023
024 import org.w3c.dom.ls.LSResourceResolver;
025
026 import org.apache.camel.Endpoint;
027 import org.apache.camel.impl.DefaultComponent;
028 import org.apache.camel.impl.ProcessorEndpoint;
029 import org.apache.camel.processor.validation.ValidatingProcessor;
030 import org.apache.camel.util.ResourceHelper;
031 import org.slf4j.Logger;
032 import org.slf4j.LoggerFactory;
033
034 /**
035 * The <a href="http://camel.apache.org/validation.html">Validator Component</a>
036 * for validating XML against some schema
037 */
038 public class ValidatorComponent extends DefaultComponent {
039
040 private static final transient Logger LOG = LoggerFactory.getLogger(ValidatorComponent.class);
041
042 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
043 final String resourceUri = remaining;
044 InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), resourceUri);
045 StreamSource source = new StreamSource(is);
046
047 ValidatingProcessor validator = new ValidatingProcessor();
048 validator.setSchemaSource(source);
049 LOG.debug("{} using schema resource: {}", this, resourceUri);
050 configureValidator(validator, uri, remaining, parameters);
051
052 // force loading of schema at create time otherwise concurrent
053 // processing could cause thread safe issues for the javax.xml.validation.SchemaFactory
054 validator.loadSchema();
055
056 return new ProcessorEndpoint(uri, this, validator);
057 }
058
059 protected void configureValidator(ValidatingProcessor validator, String uri, String remaining, Map<String, Object> parameters) throws Exception {
060 LSResourceResolver resourceResolver = resolveAndRemoveReferenceParameter(parameters, "resourceResolver", LSResourceResolver.class);
061 if (resourceResolver != null) {
062 validator.setResourceResolver(resourceResolver);
063 } else {
064 validator.setResourceResolver(new DefaultLSResourceResolver(getCamelContext(), remaining));
065 }
066
067 setProperties(validator, parameters);
068 }
069 }