Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.weld.xml;
  
  import static java.util.Arrays.asList;
  
  import java.net.URL;
  import java.util.List;
  import java.util.Set;
 
 
An implementation of the beans.xml parser written using SAX

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

Author(s):
Pete Muir
 
 public class BeansXmlHandler extends DefaultHandler {
 
     private static final String VALUE_ATTRIBUTE_QUALIFIED_NAME = "value";
     private static final String PATTERN_ATTRIBUTE_QUALIFIED_NAME = "pattern";
     private static final String NAME_ATTRIBUTE_QUALIFIED_NAME = "name";
 
     protected static String trim(String str) {
         if (str == null) {
             return null;
         } else {
             return str.trim();
         }
     }
 
     public abstract class Container {
 
         private final Set<Stringuris;
         private final String localName;
         private final Collection<StringnestedElements;
 
         public Container(Set<StringurisString localNameString... nestedElements) {
             this. = uris;
             this. = localName;
             this. = asList(nestedElements);
         }
 
         public String getLocalName() {
             return ;
         }
 
         public Set<StringgetUris() {
             return ;
         }

        
Called by startElement, the nested content is not available
 
         public void processStartChildElement(String uriString localNameString qNameAttributes attributes) {
         }

        
Called by endElement, the nested content is not available
 
         public void processEndChildElement(String uriString localNameString qNameString nestedText) {
         }
 
         public void handleMultiple() {
 
         }
 
         public Collection<StringgetNestedElements() {
             return ;
         }
 
         @Override
         public String toString() {
             return "<" +  + " />";
         }
 
         protected boolean isInNamespace(String namespace) {
             if (namespace.length() == 0) {
                return true;
            }
            return getUris().contains(namespace);
        }
    }
    private abstract class SpecContainer extends Container {
        public SpecContainer(String localNameString... nestedElements) {
            super(localNamenestedElements);
        }
    }
    public static final String JAVAEE_LEGACY_URI = "http://java.sun.com/xml/ns/javaee";
    public static final String JAVAEE_URI = "http://xmlns.jcp.org/xml/ns/javaee";
    public static final Set<StringJAVAEE_URIS = ImmutableSet.of();
    public static final String WELD_URI = "http://jboss.org/schema/weld/beans";
    public static final Set<StringSCANNING_URI = ImmutableSet.of();
    private static final String VERSION_ATTRIBUTE_NAME = "version";
    private static final String BEAN_DISCOVERY_MODE_ATTRIBUTE_NAME = "bean-discovery-mode";
    private static final String ROOT_ELEMENT_NAME = "beans";
    private static final String IF_CLASS_AVAILABLE = "if-class-available";
    private static final String IF_CLASS_NOT_AVAILABLE = "if-class-not-available";
    private static final String IF_SYSTEM_PROPERTY = "if-system-property";
    private static final String CLASS = "class";
    private static final String STEREOTYPE = "stereotype";
    private static final String INCLUDE = "include";
    private static final String EXCLUDE = "exclude";
    // See also http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#cvc-elt
    private static final String VALIDATION_ERROR_CODE_CVC_ELT_1 = "cvc-elt.1";
    /*
    * The containers we are parsing
    */
    private final Collection<Containercontainers;
    /*
    * Storage for parsed info
    */
    private final List<Metadata<String>> interceptors;
    private final List<Metadata<String>> decorators;
    private final List<Metadata<String>> alternativesClasses;
    private final List<Metadata<String>> alternativeStereotypes;
    private final List<Metadata<Filter>> includes;
    private final List<Metadata<Filter>> excludes;
    protected final URL file;
    private String version;
    /*
    * Parser State
    */
    private Container currentContainer;
    private StringBuilder buffer;
    private Locator locator;
    public BeansXmlHandler(final URL file) {
        this. = file;
        this. = new ArrayList<Metadata<String>>();
        this. = new ArrayList<Metadata<String>>();
        this. = new ArrayList<Metadata<String>>();
        this. = new ArrayList<Metadata<String>>();
        this. = new ArrayList<Metadata<Filter>>();
        this. = new ArrayList<Metadata<Filter>>();
        this. = new ArrayList<Container>();
        this. = new ArrayList<Container>();
        this. = .// this is the default value for a beans.xml file
        .add(new SpecContainer("interceptors") {
            @Override
            public void processEndChildElement(String uriString localNameString qNameString nestedText) {
                if (isInNamespace(uri) && .equals(localName)) {
                    .add(new XmlMetadata<String>(qNametrim(nestedText), file.getLineNumber()));
                }
            }
            @Override
            public void handleMultiple() {
                throw ..multipleInterceptors(file + "@" + .getLineNumber());
            }
        });
        .add(new SpecContainer("decorators") {
            @Override
            public void processEndChildElement(String uriString localNameString qNameString nestedText) {
                if (isInNamespace(uri) && .equals(localName)) {
                    .add(new XmlMetadata<String>(qNametrim(nestedText), file.getLineNumber()));
                }
            }
            @Override
            public void handleMultiple() {
                throw ..multipleDecorators(file + "@" + .getLineNumber());
            }
        });
        .add(new SpecContainer("alternatives") {
            @Override
            public void processEndChildElement(String uriString localNameString qNameString nestedText) {
                if (isInNamespace(uri) && .equals(localName)) {
                    .add(new XmlMetadata<String>(qNametrim(nestedText), file.getLineNumber()));
                } else if (isInNamespace(uri) && .equals(localName)) {
                    .add(new XmlMetadata<String>(qNametrim(nestedText), file.getLineNumber()));
                }
            }
            @Override
            public void handleMultiple() {
                throw ..multipleAlternatives(file + "@" + .getLineNumber());
            }
        });
        .add(new Container("scan") {
            String name;
            String pattern;
            @Override
            public void processStartChildElement(String uriString localNameString qNameAttributes attributes) {
                if (isFilterElement(urilocalName)) {
                     = interpolateAttributeValue(attributes);
                     = interpolateAttributeValue(attributes);
                     = new ArrayList<Metadata<SystemPropertyActivation>>();
                     = new ArrayList<Metadata<ClassAvailableActivation>>();
                } else if (isInNamespace(uri)) {
                    if (.equals(localName) || .equals(localName)) {
                        String className = interpolateAttributeValue(attributes);
                        Metadata<ClassAvailableActivationclassAvailableActivation = new XmlMetadata<ClassAvailableActivation>(qNamenew ClassAvailableActivationImpl(className.equals(localName)), file.getLineNumber());
                        .add(classAvailableActivation);
                    } else if (.equals(localName)) {
                        String systemPropertyName = interpolateAttributeValue(attributes);
                        String systemPropertyValue = interpolateAttributeValue(attributes);
                        Metadata<SystemPropertyActivationsystemPropertyActivation = new XmlMetadata<SystemPropertyActivation>(qNamenew SystemPropertyActivationImpl(systemPropertyNamesystemPropertyValue), file.getLineNumber());
                        .add(systemPropertyActivation);
                    }
                }
            }
            private String interpolateAttributeValue(Attributes attributesString qName) {
                String value = trim(attributes.getValue(qName));
                return value == null ? null : interpolate(value);
            }
            @Override
            public void processEndChildElement(String uriString localNameString qNameString nestedText) {
                if (isFilterElement(urilocalName)) {
                    Filter filter = null;
                    if (.equals(uri)) {
                        filter = new WeldFilterImpl();
                    } else {
                        filter = new FilterImpl();
                    }
                    Metadata<FilterfilterMetadata = new XmlMetadata<Filter>(qNamefilterfile.getLineNumber());
                    if (.equals(localName)) {
                        .add(filterMetadata);
                    } else if (.equals(localName)) {
                        .add(filterMetadata);
                    }
                    // reset
                     = null;
                     = null;
                     = null;
                     = null;
                }
            }
            private boolean isFilterElement(String uriString localName) {
                return isInNamespace(uri) && (.equals(localName) || .equals(localName));
            }
            @Override
            public void handleMultiple() {
                throw ..multipleScanning(file + "@" + .getLineNumber());
            }
        });
    }
    @Override
    public void startElement(String uriString localNameString qNameAttributes attributesthrows SAXException {
        if (localName.equals() && ("".equals(uri) || .contains(uri))) {
            processRootElement(attributes);
            return;
        }
        if ( == null) {
            Container container = getContainer(urilocalName);
            if (container != null) {
                if (.contains(container)) {
                    container.handleMultiple();
                }
                 = container;
            }
        } else {
            .processStartChildElement(urilocalNameqNameattributes);
            // The current container is interested in the content of this element
            if (.getNestedElements().contains(localName)) {
                 = new StringBuilder();
            }
        }
    }
    private void processRootElement(Attributes attributes) {
        String discoveryMode = attributes.getValue();
        if (discoveryMode != null) {
            this. = BeanDiscoveryMode.valueOf(discoveryMode.toUpperCase());
        }
        String version = attributes.getValue();
        if (version != null) {
            this. = version;
        }
    }
    @Override
    public void endElement(String uriString localNameString qNamethrows SAXException {
        if ( != null) {
            .processEndChildElement(urilocalNameqName != null ? .toString() : null);
            // The current container was interested in this element
            if (.getNestedElements().contains(localName)) {
                 = null;
            }
            Container container = getContainer(urilocalName);
            if (container != null) {
                // We are exiting the container, record it so we know it's already been declared (for error reporting)
                .add(container);
                // And stop work until we find another container of interest
                 = null;
            }
        }
    }
    private Container getContainer(String uriString localName) {
        return getContainer(urilocalName);
    }
    private static Container getContainer(String uriString localNameCollection<Containercontainers) {
        for (Container container : containers) {
            if (uri.length() == 0) {
                if (container.getLocalName().equals(localName)) {
                    return container;
                }
            } else {
                if (container.getLocalName().equals(localName) && container.getUris().contains(uri)) {
                    return container;
                }
            }
        }
        return null;
    }
    public BeansXml createBeansXml() {
    }
    @Override
    public void characters(char[] chint startint lengththrows SAXException {
        if ( != null) {
            .append(chstartlength);
        }
    }
    @Override
    public void setDocumentLocator(Locator locator) {
        this. = locator;
    }
    @Override
    public void warning(SAXParseException ethrows SAXException {
    }
    @Override
    public void error(SAXParseException ethrows SAXException {
            // Ignore the errors we get when there is no schema defined
            return;
        }
    }
    protected String interpolate(String string) {
        return string// noop by default, integrators may override
    }
New to GrepCode? Check out our FAQ X