Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) FuseSource, Inc. http://fusesource.com 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.fusesource.fabric.dosgi.util;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.osgi.framework.BundleContext;
 
 public class Utils {
 
     public static final String FRAMEWORK_UUID = "org.osgi.framework.uuid";
 
     public static String getUUID(BundleContext bundleContext) {
         String uuid = bundleContext.getProperty();
         if (uuid == null) {
             synchronized () {
                 uuid = bundleContext.getProperty();
                 if (uuid == null) {
                     uuid = UuidGenerator.getUUID();
                     System.setProperty(uuid);
                 }
             }
         }
         return uuid;
     }
 
     public static Set<Stringnormalize(Object object) {
         Set<Stringstrings = new HashSet<String>();
         if (object instanceof String) {
             strings.add((Stringobject);
         } else if (object instanceof String[]) {
             for (String s : (String[]) object) {
                 strings.add(s);
             }
         } else if (object instanceof Collection) {
             for (Object o : (Collectionobject) {
                 if (o instanceof String) {
                     strings.add((Stringo);
                 }
             }
         }
         return strings;
     }
 
     private static final String REMOTE_SERVICES_ADMIN_NS = "http://www.osgi.org/xmlns/rsa/v1.0.0";
     private static final String ENDPOINT_DESCRIPTIONS = "endpoint-descriptions";
     private static final String ENDPOINT_DESCRIPTION = "endpoint-description";
     private static final String PROPERTY = "property";
     private static final String ARRAY = "array";
     private static final String SET = "set";
     private static final String LIST = "list";
     private static final String NAME = "name";
     private static final String VALUE = "value";
     private static final String VALUE_TYPE = "value-type";
 
     public static String getEndpointDescriptionXML(EndpointDescription endpointthrows XMLStreamException {
         Map<StringObjectproperties = endpoint.getProperties();
         StringWriter writer = new StringWriter();
         XMLStreamWriter xml = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
 
         xml.writeStartDocument();
         xml.writeNamespace("");
 
         for (Map.Entry<StringObjectentry : properties.entrySet()) {
             String key = entry.getKey();
            Object val = entry.getValue();
            xml.writeAttribute(key);
            if (val.getClass().isArray()) {
                setValueType(xmlval.getClass().getComponentType().getName());
                xml.writeStartElement();
                for (int i = 0, l = Array.getLength(val); i < li++) {
                    xml.writeStartElement();
                    xml.writeCharacters(Array.get(vali).toString());
                    xml.writeEndElement();
                }
                xml.writeEndElement();
            } else if (val instanceof List) {
                xml.writeStartElement();
                handleCollectionValue(xml, (Collectionval);
                xml.writeEndElement();
            } else if (val instanceof Set) {
                xml.writeStartElement();
                handleCollectionValue(xml, (Collectionval);
                xml.writeEndElement();
            } else {
                xml.writeAttribute(val.toString());
                setValueType(xmlval.getClass().getName());
            }
            xml.writeEndElement();
        }
        xml.writeEndElement();
        xml.writeEndElement();
        xml.writeEndDocument();
        xml.close();
        return writer.toString();
    }
    private static void handleCollectionValue(XMLStreamWriter xmlCollection valthrows XMLStreamException {
        for (Object o : val) {
            xml.writeStartElement();
            setValueType(xmlo.getClass().getName());
            xml.writeCharacters(o.toString());
            xml.writeEndElement();
        }
    }
    private static void setValueType(XMLStreamWriter xmlString dataTypethrows XMLStreamException {
        if (dataType.equals(String.class.getName())) {
            return;
        }
        if (dataType.startsWith("java.lang.")) {
            dataType = dataType.substring("java.lang.".length());
        }
        xml.writeAttribute(dataType);
    }
    public static EndpointDescription getEndpointDescription(String datathrows XMLStreamException {
        List<EndpointDescriptionendpoints = getEndpointDescriptions(data);
        if (endpoints == null || endpoints.size() != 1) {
            throw new IllegalArgumentException();
        }
        return endpoints.get(0);
    }
    public static List<EndpointDescriptiongetEndpointDescriptions(String datathrows XMLStreamException {
        List<EndpointDescriptionendpoints = new ArrayList<EndpointDescription>();
        Map<StringObjectproperties = null;
        String key = null;
        String type = null;
        String value = null;
        Object val = null;
        String txt = null;
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(data));
        for (;;) {
            int code = reader.next();
            switch (code) {
                case .:
                    break;
                case .:
                    if (.equals(reader.getLocalName())) {
                        properties = new HashMap<StringObject>();
                    } else if (.equals(reader.getLocalName())) {
                        key = reader.getAttributeValue(null);
                        type = reader.getAttributeValue(null);
                        value = reader.getAttributeValue(null);
                        val = null;
                    } else if (.equals(reader.getLocalName())) {
                        val = Array.newInstance(.get(type == null ? "String" : type), 0);
                    } else if (.equals(reader.getLocalName())) {
                        val = new HashSet<Object>();
                    } else if (.equals(reader.getLocalName())) {
                        val = new ArrayList<Object>();
                    } else if (.equals(reader.getLocalName())) {
                        txt = null;
                    }
                    break;
                case .:
                    if (txt == null) {
                        txt = reader.getText();
                    } else {
                        txt += reader.getText();
                    }
                    break;
                case .:
                    if (.equals(reader.getLocalName())) {
                        endpoints.add(new EndpointDescription(properties));
                        properties = null;
                    } else if (.equals(reader.getLocalName())) {
                        if (key == null || (val == null && value == null) || (val != null && value != null)) {
                            throw new IllegalArgumentException();
                        }
                        if (value != null) {
                            val = instantiate(type == null ? "String" : typevalue);
                        }
                        properties.put(keyval);
                    } else if (.equals(reader.getLocalName())) {
                        if (val.getClass().isArray()) {
                            int len = Array.getLength(val);
                            Object a = Array.newInstance(.get(type == null ? "String" : type), len + 1);
                            System.arraycopy(val, 0, a, 0, len);
                            Array.set(aleninstantiate(type == null ? "String" : typetxt));
                            val = a;
                        } else {
                            ((Collectionval).add(instantiate(type == null ? "String" : typetxt));
                        }
                    }
                    break;
                case .:
                    return endpoints;
            }
        }
    }
    private static final Map<StringClassTYPES;
    static {
        Map<StringClasstypes = new HashMap<StringClass>();
        types.put("long"long.class);
        types.put("Long"Long.class);
        types.put("double"double.class);
        types.put("Double"Double.class);
        types.put("float"float.class);
        types.put("Float"Float.class);
        types.put("int"int.class);
        types.put("Integer"Integer.class);
        types.put("byte"byte.class);
        types.put("Byte"Byte.class);
        types.put("char"char.class);
        types.put("Character"Character.class);
        types.put("boolean"boolean.class);
        types.put("Double"Double.class);
        types.put("short"short.class);
        types.put("Short"Short.class);
        types.put("String"String.class);
         = types;
    }
    private static Object instantiate(String typeString value) {
        if ("String".equals(type)) {
            return value;
        }
        value = value.trim();
        String boxedType = null;
        if ("long".equals(type)) {
            boxedType = "Long";
        } else if ("double".equals(type)) {
            boxedType = "Double";
        } else if ("float".equals(type)) {
            boxedType = "Float";
        } else if ("int".equals(type)) {
            boxedType = "Integer";
        } else if ("byte".equals(type)) {
            boxedType = "Byte";
        } else if ("char".equals(type)) {
            boxedType = "Character";
        } else if ("boolean".equals(type)) {
            boxedType = "Boolean";
        } else if ("short".equals(type)) {
            boxedType = "Short";
        }
        if (boxedType == null) {
            boxedType = type;
        }
        String javaType = "java.lang." + boxedType;
        if (boxedType.equals("Character")) {
            return value.charAt(0);
        }
        try {
            Class<?> cls = ClassLoader.getSystemClassLoader().loadClass(javaType);
            Constructor<?> ctor = cls.getConstructor(String.class);
            return ctor.newInstance(value);
        } catch (Exception e) {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X