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.IOException;
020    import java.io.InputStream;
021    import java.io.Reader;
022    
023    import org.w3c.dom.ls.LSInput;
024    import org.w3c.dom.ls.LSResourceResolver;
025    
026    import org.apache.camel.CamelContext;
027    import org.apache.camel.util.FileUtil;
028    import org.apache.camel.util.ObjectHelper;
029    import org.apache.camel.util.ResourceHelper;
030    
031    /**
032     * Default {@link LSResourceResolver} which can included schema resources.
033     */
034    public class DefaultLSResourceResolver implements LSResourceResolver {
035    
036        private final CamelContext camelContext;
037        private final String resourceUri;
038        private final String resourcePath;
039    
040        public DefaultLSResourceResolver(CamelContext camelContext, String resourceUri) {
041            this.camelContext = camelContext;
042            this.resourceUri = resourceUri;
043            this.resourcePath = FileUtil.onlyPath(resourceUri);
044        }
045    
046        @Override
047        public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
048            // systemId should be mandatory
049            if (systemId == null) {
050                throw new IllegalArgumentException(String.format("Resource: %s refers an invalid resource without SystemId."
051                        + " Invalid resource has type: %s, namespaceURI: %s, publicId: %s, systemId: %s, baseURI: %s", resourceUri, type, namespaceURI, publicId, systemId, baseURI));
052            }
053            return new DefaultLSInput(publicId, systemId, baseURI);
054        }
055        
056        private final class DefaultLSInput implements LSInput {
057            
058            private final String publicId;
059            private final String systemId;
060            private final String baseURI;
061            private final String uri;
062    
063            private DefaultLSInput(String publicId, String systemId, String baseURI) {
064                this.publicId = publicId;
065                this.systemId = systemId;
066                this.baseURI = baseURI;
067                
068                if (resourcePath != null) {
069                    uri = resourcePath + "/" + systemId;
070                } else {
071                    uri = systemId;
072                }
073            }
074    
075            @Override
076            public Reader getCharacterStream() {
077                InputStream is = getByteStream();
078                return camelContext.getTypeConverter().convertTo(Reader.class, is);
079            }
080    
081            @Override
082            public void setCharacterStream(Reader reader) {
083                // noop
084            }
085    
086            @Override
087            public InputStream getByteStream() {
088                try {
089                    return ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext.getClassResolver(), uri);
090                } catch (IOException e) {
091                    throw ObjectHelper.wrapRuntimeCamelException(e);
092                }
093            }
094    
095            @Override
096            public void setByteStream(InputStream inputStream) {
097                // noop
098            }
099    
100            @Override
101            public String getStringData() {
102                InputStream is = getByteStream();
103                return camelContext.getTypeConverter().convertTo(String.class, is);
104            }
105    
106            @Override
107            public void setStringData(String stringData) {
108                // noop
109            }
110    
111            @Override
112            public String getSystemId() {
113                return systemId;
114            }
115    
116            @Override
117            public void setSystemId(String systemId) {
118                // noop
119            }
120    
121            @Override
122            public String getPublicId() {
123                return publicId;
124            }
125    
126            @Override
127            public void setPublicId(String publicId) {
128                // noop
129            }
130    
131            @Override
132            public String getBaseURI() {
133                return baseURI;
134            }
135    
136            @Override
137            public void setBaseURI(String baseURI) {
138                // noop
139            }
140    
141            @Override
142            public String getEncoding() {
143                return null;
144            }
145    
146            @Override
147            public void setEncoding(String encoding) {
148                // noop
149            }
150    
151            @Override
152            public boolean getCertifiedText() {
153                return false;
154            }
155    
156            @Override
157            public void setCertifiedText(boolean certifiedText) {
158                // noop
159            }
160    
161            @Override
162            public String toString() {
163                return "DefaultLSInput[" + uri + "]";
164            }
165        }
166        
167    }