Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.tuscany.sca.contribution.processor;
 
 import java.io.Reader;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
 
Default implementation of an XMLInputFactory that creates validating XMLStreamReaders.

Version:
$Rev: 831467 $ $Date: 2009-10-30 22:08:25 +0000 (Fri, 30 Oct 2009) $
 
     private ExtensionPointRegistry registry;
     private XMLInputFactory inputFactory;
     private DOMImplementationLS ls;
     private MonitorFactory monitorFactory;
     private boolean initialized;
     private boolean hasSchemas;
     private Schema aggregatedSchema;
     private StAXHelper helper;
 
         this. = registry;
         FactoryExtensionPoint factoryExtensionPoint = registry.getExtensionPoint(FactoryExtensionPoint.class);
         this. = factoryExtensionPoint.getFactory(XMLInputFactory.class);
         this. = factoryExtensionPoint.getFactory(DocumentBuilderFactory.class);
         this. = registry.getExtensionPoint(ValidationSchemaExtensionPoint.class);
         this. =
            registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(MonitorFactory.class);
        this. = StAXHelper.getInstance(registry);
    }

    
Constructs a new XMLInputFactory.

Parameters:
inputFactory
schemas
    // FOR Test only
        this. = inputFactory;
        this. = schemas;
        this. = new DefaultExtensionPointRegistry();
    }
    

    
Report a exception.

Parameters:
problems
message
model
    private void error(Monitor monitorString messageObject modelThrowable ex) {
        Monitor.error(monitorthis"contribution-validation-messages"messageex);
    }
    private void warn(Monitor monitorString messageObject modelThrowable ex) {
        Monitor.warning(monitorthis"contribution-validation-messages"messageex);
    }
    
    public static final QName XSD = new QName(."schema");
    private Collection<? extends Sourceaggregate(URL... urlsthrows IOExceptionXMLStreamException {
        if (urls.length == 1) {
            return Collections.singletonList(new SAXSource(XMLDocumentHelper.getInputSource(urls[0])));
        }
        Map<StringCollection<URL>> map = new HashMap<StringCollection<URL>>();
        for (URL url : urls) {
            String tns = .readAttribute(url"targetNamespace");
            Collection<URLcollection = map.get(tns);
            if (collection == null) {
                collection = new HashSet<URL>();
                map.put(tnscollection);
            }
            collection.add(url);
        }
        List<Sourcesources = new ArrayList<Source>();
        for (Map.Entry<StringCollection<URL>> e : map.entrySet()) {
            if (e.getValue().size() == 1) {
                sources.add(new SAXSource(XMLDocumentHelper.getInputSource(e.getValue().iterator().next())));
            } else {
                StringBuffer xsd = new StringBuffer("<schema xmlns=\"http://www.w3.org/2001/XMLSchema\"");
                if (e.getKey() != null) {
                    xsd.append(" targetNamespace=\"").append(e.getKey()).append("\"");
                }
                xsd.append(">");
                for (URL url : e.getValue()) {
                    xsd.append("<include schemaLocation=\"").append(url).append("\"/>");
                }
                xsd.append("</schema>");
                SAXSource source = new SAXSource(new InputSource(new StringReader(xsd.toString())));
                sources.add(source);
            }
        }
        return sources;
    }

    
Initialize the registered schemas and create an aggregated schema for validation.

Parameters:
monitor TODO
    private synchronized void initializeSchemas(Monitor monitor) {
        if () {
            return;
        }
         = true;
        // Load the XSDs registered in the validation schema extension point
        try {
            List<Stringuris = .getSchemas();
            int n = uris.size();
            if (n ==0) {
                return;
            } else {
                 = true;
            }
            
            URL[] urls = new URL[uris.size()];
            for (int i = 0; i < urls.lengthi++) {
                urls[i] = new URL(uris.get(i));
            }
            final Collection<? extends Sourcesources = aggregate(urls);            
            final SchemaFactory schemaFactory = newSchemaFactory();
            DOMImplementation impl = null;
            try {
                impl = .newDocumentBuilder().getDOMImplementation();
            } catch (ParserConfigurationException e) {
                // Ignore
            }
            if (impl instanceof DOMImplementationLS) {
                 = (DOMImplementationLS)impl;
                schemaFactory.setResourceResolver(this);
            }
            // Allow privileged access to check files. Requires FilePermission
            // in security policy.
            try {
                 = AccessController.doPrivileged(new PrivilegedExceptionAction<Schema>() {
                    public Schema run() throws SAXException {
                        return schemaFactory.newSchema(sources.toArray(new Source[sources.size()]));
                    }
                });
            } catch (PrivilegedActionException e) {
            	warn(monitor"PrivilegedActionException"schemaFactory, (SAXException)e.getException());
            	 = false;
                throw (SAXException)e.getException();
            }
        } catch (SAXException e) {
//            IllegalStateException ie = new IllegalStateException(e);
//            error("IllegalStateException", schemas, ie);
//            throw ie;
        } catch (Throwable e) {
            //FIXME Log this, some old JDKs don't support XMLSchema validation
            warn(monitore.getMessage(), e);
             = false;
        }
    }

    
For OSGi: Create a SchemaFactory in the context of service provider classloaders

Returns:
    private SchemaFactory newSchemaFactory() {
        ClassLoader cl =
            ClassLoaderContext.setContextClassLoader(getClass().getClassLoader(),
                                                     .getServiceDiscovery(),
                                                     SchemaFactory.class,
                                                     TransformerFactory.class,
                                                     SAXParserFactory.class,
                                                     DocumentBuilderFactory.class
                                                     );
        try {
            // Create an aggregated validation schemas from all the XSDs
            return SchemaFactory.newInstance(.);
        } finally {
            if (cl != null) {
                Thread.currentThread().setContextClassLoader(cl);
            }
        }
    }
    @Override
        return .createFilteredReader(arg0arg1);
    }
    @Override
        return .createFilteredReader(arg0arg1);
    }
    @Override
    public XMLEventReader createXMLEventReader(InputStream arg0String arg1throws XMLStreamException {
        return .createXMLEventReader(arg0arg1);
    }
    @Override
        return .createXMLEventReader(arg0);
    }
    @Override
        return .createXMLEventReader(arg0);
    }
    @Override
        return .createXMLEventReader(arg0);
    }
    @Override
    public XMLEventReader createXMLEventReader(String arg0InputStream arg1throws XMLStreamException {
        return .createXMLEventReader(arg0arg1);
    }
    @Override
    public XMLEventReader createXMLEventReader(String arg0Reader arg1throws XMLStreamException {
        return .createXMLEventReader(arg0arg1);
    }
    @Override
        return .createXMLEventReader(arg0);
    }
    @Override
        Monitor monitor = .getContextMonitor();
        initializeSchemas(monitor);
        if () {
            return new ValidatingXMLStreamReader(.createXMLStreamReader(arg0arg1), monitor);
        }else {
            return .createXMLStreamReader(arg0arg1);
        }
    }
    @Override
        Monitor monitor = .getContextMonitor();
        initializeSchemas(monitor);
        if () {
            return new ValidatingXMLStreamReader(.createXMLStreamReader(arg0), monitor);
        } else {
            return .createXMLStreamReader(arg0);
        }
    }
    @Override
        Monitor monitor = .getContextMonitor();
        initializeSchemas(monitor);
        if () {
            return new ValidatingXMLStreamReader(.createXMLStreamReader(arg0), monitor);
        } else {
            return .createXMLStreamReader(arg0);
        }
    }
    @Override
        Monitor monitor = .getContextMonitor();
        initializeSchemas(monitor);
        if () {
            return new ValidatingXMLStreamReader(.createXMLStreamReader(arg0), monitor);
        } else {
            return .createXMLStreamReader(arg0);
        }
    }
    @Override
        Monitor monitor = .getContextMonitor();
        initializeSchemas(monitor);
        if () {
            return new ValidatingXMLStreamReader(.createXMLStreamReader(arg0arg1), monitor);
        } else {
            return .createXMLStreamReader(arg0arg1);
        }
    }
    @Override
    public XMLStreamReader createXMLStreamReader(String arg0Reader arg1throws XMLStreamException {
        Monitor monitor = .getContextMonitor();
        initializeSchemas(monitor);
        if () {
            return new ValidatingXMLStreamReader(.createXMLStreamReader(arg0arg1), monitor);
        } else {
            return .createXMLStreamReader(arg0arg1);
        }
    }
    @Override
        return .getEventAllocator();
    }
    @Override
    public Object getProperty(String arg0throws IllegalArgumentException {
        return .getProperty(arg0);
    }
    @Override
    public XMLReporter getXMLReporter() {
        return .getXMLReporter();
    }
    @Override
    public XMLResolver getXMLResolver() {
        return .getXMLResolver();
    }
    @Override
    public boolean isPropertySupported(String arg0) {
        return .isPropertySupported(arg0);
    }
    @Override
    public void setEventAllocator(XMLEventAllocator arg0) {
        .setEventAllocator(arg0);
    }
    @Override
    public void setProperty(String arg0Object arg1throws IllegalArgumentException {
        .setProperty(arg0arg1);
    }
    @Override
    public void setXMLReporter(XMLReporter arg0) {
        .setXMLReporter(arg0);
    }
    @Override
    public void setXMLResolver(XMLResolver arg0) {
        .setXMLResolver(arg0);
    }
    public LSInput resolveResource(String typeString namespaceURIString publicIdString systemIdString baseURI) {
        String key = null;
        if("http://www.w3.org/2001/XMLSchema".equals(type)) {
            key = namespaceURI;
        } else if("http://www.w3.org/TR/REC-xml".equals(type)) {
            key = publicId;
        }
        URL url = ..get(key);
        if (url != null && !..equals(namespaceURI)) {
            systemId = url.toString();
        } else if (url != null && systemId == null) {
            systemId = url.toString();
        } 
        LSInput input = .createLSInput();
        input.setBaseURI(baseURI);
        input.setPublicId(publicId);
        input.setSystemId(systemId);
        return input;
    }
New to GrepCode? Check out our FAQ X