1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.editors.layout.gle2; 18 19 import com.android.annotations.NonNull; 20 import com.android.ide.common.api.INode.IAttribute; 21 22 import java.util.regex.Matcher; 23 import java.util.regex.Pattern; 24 25 /** 26 * Represents one XML attribute in a {@link SimpleElement}. 27 * <p/> 28 * The attribute is always represented by a namespace URI, a name and a value. 29 * The name cannot be empty. 30 * The namespace URI can be empty for an attribute without a namespace but is never null. 31 * The value can be empty but cannot be null. 32 * <p/> 33 * For a more detailed explanation of the purpose of this class, 34 * please see {@link SimpleXmlTransfer}. 35 */ 36 public class SimpleAttribute implements IAttribute { 37 private final String mName; 38 private final String mValue; 39 private final String mUri; 40 41 /** 42 * Creates a new {@link SimpleAttribute}. 43 * <p/> 44 * Any null value will be converted to an empty non-null string. 45 * However it is a semantic error to use an empty name -- no assertion is done though. 46 * 47 * @param uri The URI of the attribute. 48 * @param name The XML local name of the attribute. 49 * @param value The value of the attribute. 50 */ 51 public SimpleAttribute(String uri, String name, String value) { 52 mUri = uri == null ? "" : uri; 53 mName = name == null ? "" : name; 54 mValue = value == null ? "" : value; 55 } 56 57 /** 58 * Returns the namespace URI of the attribute. 59 * Can be empty for an attribute without a namespace but is never null. 60 */ 61 @Override 62 public @NonNull String getUri() { 63 return mUri; 64 } 65 66 /** Returns the XML local name of the attribute. Cannot be null nor empty. */ 67 @Override 68 public @NonNull String getName() { 69 return mName; 70 } 71 72 /** Returns the value of the attribute. Cannot be null. Can be empty. */ 73 @Override 74 public @NonNull String getValue() { 75 return mValue; 76 } 77 78 // reader and writer methods 79 80 @Override 81 public String toString() { 82 return String.format("@%s:%s=%s\n", //$NON-NLS-1$ 83 mName, 84 mUri, 85 mValue); 86 } 87 88 private static final Pattern REGEXP = 89 Pattern.compile("[^@]*@([^:]+):([^=]*)=([^\n]*)\n*"); //$NON-NLS-1$ 90 91 static SimpleAttribute parseString(String value) { 92 Matcher m = REGEXP.matcher(value); 93 if (m.matches()) { 94 return new SimpleAttribute(m.group(2), m.group(1), m.group(3)); 95 } 96 97 return null; 98 } 99 100 @Override 101 public boolean equals(Object obj) { 102 if (obj instanceof SimpleAttribute) { 103 SimpleAttribute sa = (SimpleAttribute) obj; 104 105 return mName.equals(sa.mName) && 106 mUri.equals(sa.mUri) && 107 mValue.equals(sa.mValue); 108 } 109 return false; 110 } 111 112 @Override 113 public int hashCode() { 114 long c = mName.hashCode(); 115 // uses the formula defined in java.util.List.hashCode() 116 c = 31*c + mUri.hashCode(); 117 c = 31*c + mValue.hashCode(); 118 if (c > 0x0FFFFFFFFL) { 119 // wrap any overflow 120 c = c ^ (c >> 32); 121 } 122 return (int)(c & 0x0FFFFFFFFL); 123 } 124 } 125