Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed 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.jboss.weld.xml;
 
 import static org.jboss.weld.bootstrap.spi.BeansXml.EMPTY_BEANS_XML;
 
 import java.net.URL;
 import java.util.List;
 
 
 
Simple parser for beans.xml

This class is NOT threadsafe, and should only be called in a single thread

Author(s):
Pete Muir
Ales Justin
 
 public class BeansXmlParser {
 
     private static final InputSource[] EMPTY_INPUT_SOURCE_ARRAY = new InputSource[0];
 
     private Function<URLBeansXmlURL_TO_BEANS_XML_FUNCTION = new Function<URLBeansXml>() {
         @Override
         public BeansXml apply(URL url) {
             return parse(url);
         }
     };
 
         @Override
         public BeansXml apply(BeanDeploymentArchive archive) {
             if (archive == null) {
                 return null;
             }
             return archive.getBeansXml();
         }
     };
 
     private static Function<BeansXmlBeansXmlBEANS_XML_IDENTITY_FUNCTION = new Function<BeansXmlBeansXml>() {
         @Override
         public BeansXml apply(BeansXml beansXml) {
             return beansXml;
         }
     };
 
     public BeansXml parse(final URL beansXml) {
         SAXParserFactory factory = SAXParserFactory.newInstance();
         factory.setNamespaceAware(true);
         if (beansXml == null) {
             throw ..loadError("unknown"null);
         }
         SAXParser parser;
         try {
             parser = factory.newSAXParser();
         } catch (SAXException e) {
             throw ..configurationError(e);
         } catch (ParserConfigurationException e) {
             throw ..configurationError(e);
         }
         InputStream beansXmlInputStream = null;
        try {
            beansXmlInputStream = beansXml.openStream();
            InputSource source = new InputSource(beansXmlInputStream);
            if (source.getByteStream().available() == 0) {
                // The file is just acting as a marker file
                return ;
            }
            BeansXmlHandler handler = getHandler(beansXml);
            try {
                parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage""http://www.w3.org/2001/XMLSchema");
                parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource"loadXsds());
            } catch (IllegalArgumentException e) {
                // No op, we just don't validate the XML
            } catch (SAXNotRecognizedException e) {
                // No op, we just don't validate the XML
            } catch (SAXNotSupportedException e) {
                // No op, we just don't validate the XML
            }
            parser.parse(sourcehandler);
            return handler.createBeansXml();
        } catch (IOException e) {
            throw ..loadError(beansXmle);
        } catch (SAXException e) {
            throw ..parsingError(beansXmle);
        } finally {
            if (beansXmlInputStream != null) {
                try {
                    beansXmlInputStream.close();
                } catch (IOException e) {
                    throw new IllegalStateException(e);
                }
            }
        }
    }
    public BeansXml parse(Iterable<URLurls) {
        return parse(urlsfalse);
    }
    public BeansXml parse(Iterable<URLurlsboolean removeDuplicates) {
        return merge(urlsremoveDuplicates);
    }
    private <T> BeansXml merge(Iterable<? extends T> itemsFunction<T, BeansXmlfunctionboolean removeDuplicates) {
        List<Metadata<String>> alternatives = new ArrayList<Metadata<String>>();
        List<Metadata<String>> alternativeStereotypes = new ArrayList<Metadata<String>>();
        List<Metadata<String>> decorators = new ArrayList<Metadata<String>>();
        List<Metadata<String>> interceptors = new ArrayList<Metadata<String>>();
        List<Metadata<Filter>> includes = new ArrayList<Metadata<Filter>>();
        List<Metadata<Filter>> excludes = new ArrayList<Metadata<Filter>>();
        URL beansXmlUrl = null;
        for (T item : items) {
            BeansXml beansXml = function.apply(item);
            addTo(alternativesbeansXml.getEnabledAlternativeClasses(), removeDuplicates);
            addTo(alternativeStereotypesbeansXml.getEnabledAlternativeStereotypes(), removeDuplicates);
            addTo(decoratorsbeansXml.getEnabledDecorators(), removeDuplicates);
            addTo(interceptorsbeansXml.getEnabledInterceptors(), removeDuplicates);
            includes.addAll(beansXml.getScanning().getIncludes());
            excludes.addAll(beansXml.getScanning().getExcludes());
            /*
             * provided we are merging the content of multiple XML files, getBeansXml() returns an
             * InputStream representing the last one
             */
            beansXmlUrl = beansXml.getUrl();
        }
        return new BeansXmlImpl(alternativesalternativeStereotypesdecoratorsinterceptorsnew ScanningImpl(includesexcludes), beansXmlUrl.null);
    }
    private void addTo(List<Metadata<String>> listList<Metadata<String>> listToAddboolean removeDuplicates) {
        if (removeDuplicates) {
            List<Metadata<String>> filteredListToAdd = new ArrayList<Metadata<String>>(listToAdd.size());
            for (Metadata<Stringmetadata : listToAdd) {
                if (!alreadyAdded(metadatalist)) {
                    filteredListToAdd.add(metadata);
                }
            }
            listToAdd = filteredListToAdd;
        }
        list.addAll(listToAdd);
    }
    private boolean alreadyAdded(Metadata<StringmetadataList<Metadata<String>> list) {
        for (Metadata<Stringexisting : list) {
            if (existing.getValue().equals(metadata.getValue())) {
                return true;
            }
        }
        return false;
    }
    private static InputSource[] loadXsds() {
        List<InputSourcexsds = new ArrayList<InputSource>();
        for (XmlSchema schema : XmlSchema.values()) {
            InputSource source = loadXsd(schema.getFileName(), schema.getClassLoader());
            if (source != null) {
                xsds.add(source);
            }
        }
        return xsds.toArray();
    }
    private static InputSource loadXsd(String nameClassLoader classLoader) {
        InputStream in = classLoader.getResourceAsStream(name);
        if (in == null) {
            return null;
        } else {
            return new InputSource(in);
        }
    }
    protected BeansXmlHandler getHandler(final URL beansXml) {
        return new BeansXmlHandler(beansXml);
    }
    public BeansXml mergeExisting(final Iterable<? extends BeanDeploymentArchivebeanArchivesfinal boolean removeDuplicates) {
        return merge(beanArchivesremoveDuplicates);
    }
    public BeansXml mergeExistingDescriptors(final Iterable<BeansXmlbeanArchivesfinal boolean removeDuplicates) {
        return merge(beanArchivesremoveDuplicates);
    }
New to GrepCode? Check out our FAQ X