001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.xbean.blueprint.context.impl;
022    
023    import java.net.URL;
024    import java.util.Collections;
025    import java.util.Set;
026    
027    import javax.xml.namespace.QName;
028    import org.apache.aries.blueprint.NamespaceHandler;
029    import org.apache.aries.blueprint.ParserContext;
030    import org.osgi.service.blueprint.reflect.ComponentMetadata;
031    import org.osgi.service.blueprint.reflect.Metadata;
032    import org.w3c.dom.Element;
033    import org.w3c.dom.Node;
034    import org.w3c.dom.NodeList;
035    
036    /**
037     * @version $Rev: 897559 $ $Date: 2010-01-09 17:01:34 -0500 (Sat, 09 Jan 2010) $
038     */
039    public class QNameNamespaceHandler implements NamespaceHandler {
040    
041        private static final Set<Class> MANAGED_CLASS = Collections.<Class>singleton(QName.class);
042        private final QNameHelper qNameHelper = new QNameHelper();
043    
044        public URL getSchemaLocation(String namespace) {
045            return null;
046        }
047    
048        public Set<Class> getManagedClasses() {
049            return MANAGED_CLASS;
050        }
051    
052        public Metadata parse(Element element, ParserContext context) {
053            if ("qname".equals(element.getLocalName())) {
054                return QNameHelper.createQNameMetadata(element, getElementText(element), context);
055            }
056            return null;
057        }
058    
059        public ComponentMetadata decorate(Node node, ComponentMetadata component, ParserContext context) {
060            return component;
061        }
062    
063        private String getElementText(Element element) {
064            StringBuilder buffer = new StringBuilder();
065            NodeList nodeList = element.getChildNodes();
066            for (int i = 0, size = nodeList.getLength(); i < size; i++) {
067                Node node = nodeList.item(i);
068                if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
069                    buffer.append(node.getNodeValue());
070                }
071            }
072            return buffer.toString();
073        }
074    
075    }