01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.ivy.util.extendable;
19:
20: import java.util.Arrays;
21: import java.util.Collection;
22: import java.util.HashMap;
23: import java.util.Iterator;
24: import java.util.Map;
25:
26: import org.xml.sax.Attributes;
27:
28: public final class ExtendableItemHelper {
29: private ExtendableItemHelper() {
30: }
31:
32: public static Map getExtraAttributes(Attributes attributes,
33: String prefix) {
34: Map ret = new HashMap();
35: for (int i = 0; i < attributes.getLength(); i++) {
36: if (attributes.getQName(i).startsWith(prefix)) {
37: ret.put(attributes.getQName(i).substring(
38: prefix.length()), attributes.getValue(i));
39: }
40: }
41: return ret;
42: }
43:
44: public static Map getExtraAttributes(Attributes attributes,
45: String[] ignoredAttNames) {
46: Map ret = new HashMap();
47: Collection ignored = Arrays.asList(ignoredAttNames);
48: for (int i = 0; i < attributes.getLength(); i++) {
49: if (!ignored.contains(attributes.getQName(i))) {
50: ret.put(attributes.getQName(i), attributes.getValue(i));
51: }
52: }
53: return ret;
54: }
55:
56: public static void fillExtraAttributes(DefaultExtendableItem item,
57: Attributes attributes, String[] ignoredAttNames) {
58: Map att = getExtraAttributes(attributes, ignoredAttNames);
59: for (Iterator iter = att.keySet().iterator(); iter.hasNext();) {
60: String attName = (String) iter.next();
61: String attValue = (String) att.get(attName);
62: item.setExtraAttribute(attName, attValue);
63: }
64: }
65:
66: }
|