📄 attributehelper.java
字号:
/* Copyright (c) 2006 Google Inc. * * 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 com.google.gdata.data;import com.google.gdata.util.ParseException;import org.xml.sax.Attributes;import java.util.HashMap;import java.util.Map;import java.util.Set;import java.util.HashSet;/** * Helps accessing tag attributes. * * The helper only checks attributes in the default namespace ("") and * rejects unknown attributes. * * The idea is to remove (consume) attributes as they are read * from the list and at the end make sure that all attributes have * been read, to detect whether unknown attributes have been * specified. This is done by the method {@link #assertAllConsumed()} usually * called from * {@link com.google.gdata.util.XmlParser.ElementHandler#processEndElement()}. * * */public class AttributeHelper { /** Maps attribute local name to string value. */ protected final Map<String, String> attrs = new HashMap<String, String>(); /** set of attributes that are duplicated */ private Set<String> dups = new HashSet<String>(); /** if the content has been consumed */ private boolean contentConsumed = false; /** element's text content or {@code null} for no text content */ private String content = null; /** * Creates a helper tied to a specific set of SAX attributes. * * @param attrs the SAX attributes to be processed */ public AttributeHelper(Attributes attrs) { // attributes for (int i = 0; i < attrs.getLength(); i++) { if (attrs.getURI(i).length() != 0) { String attrLocalName = attrs.getLocalName(i); if (this.attrs.put(attrLocalName, attrs.getValue(i)) != null) { dups.add(attrLocalName); } } else { this.attrs.put(attrs.getQName(i), attrs.getValue(i)); } } } /** * Gets the element's text content and removes it from the list. * * @param required indicates attributes is required * @return element's text content or {@code null} for no text content * @exception ParseException if required is set and the text content * is not defined */ public String consumeContent(boolean required) throws ParseException { if (content == null && required) { throw new ParseException("Missing required text content"); } contentConsumed = true; return content; } /** * Gets the value of an attribute and remove it from the list. * * @param name attribute name * @param required indicates attributes is required * @return attribute value or null if not available * @exception ParseException if required is set and the attribute * is not defined */ public String consume(String name, boolean required) throws ParseException { String value = attrs.get(name); if (value == null) { if (required) { throw new ParseException("Missing attribute: '" + name + "'"); } return null; } attrs.remove(name); return value; } /** * Gets the value of an integer attribute and remove it from the list. * * @param name attribute name * @param required indicates attribute is required * @param defaultValue the default value for an optional attribute (used * if not present) * @return the integer value of this attribute * @exception ParseException if required is set and the attribute * is not defined, or if the attribute value is not a valid integer */ public int consumeInteger(String name, boolean required, int defaultValue) throws ParseException { String value = consume(name, required); if (value == null) { return defaultValue; } try { return Integer.parseInt(value); } catch (NumberFormatException e) { throw new ParseException("Invalid integer value for attribute: '" + name + "'"); } } /** * Gets the value of an integer attribute and remove it from the list. * * @param name attribute name * @param required indicates attribute is required * @return the integer value of this attribute, 0 by default * @exception ParseException if required is set and the attribute * is not defined, or if the attribute value is not a valid integer */ public int consumeInteger(String name, boolean required) throws ParseException { return consumeInteger(name, required, 0); } /** * Gets the value of a long attribute and remove it from the list. * * @param name attribute name * @param required indicates attribute is required * @param defaultValue the default value for an optional attribute (used * if not present) * @return the long value of this attribute * @exception ParseException if required is set and the attribute * is not defined, or if the attribute value is not a valid long */ public long consumeLong(String name, boolean required, long defaultValue) throws ParseException { String value = consume(name, required); if (value == null) { return defaultValue; } try { return Long.parseLong(value); } catch (NumberFormatException e) { throw new ParseException("Invalid long value for attribute: '" + name + "'", e); } } /** * Gets the value of a long attribute and remove it from the list. * * @param name attribute name * @param required indicates attribute is required * @return the long value of this attribute, 0 by default * @exception ParseException if required is set and the attribute * is not defined, or if the attribute value is not a valid long */ public long consumeLong(String name, boolean required) throws ParseException { return consumeLong(name, required, 0); } /** * Gets the value of a double attribute and remove it from the list. * * @param name attribute name * @param required indicates attribute is required * @param defaultValue the default value for an optional attribute (used if * not present) * @return the double value of this attribute * @throws ParseException if required is set and the attribute is not defined, * or if the attribute value is not a valid double */ public double consumeDouble( String name, boolean required, double defaultValue) throws ParseException { String value = consume(name, required); if (value == null) { return defaultValue; } try { return Double.parseDouble(value); } catch (NumberFormatException e) { throw new ParseException("Invalid double value for attribute: '" + name + "'", e); } } /** * Gets the value of a double attribute and remove it from the list. * * @param name attribute name * @param required indicates attribute is required * @return the double value of this attribute, 0 by default * @throws ParseException if required is set and the attribute is not defined, * or if the attribute value is not a valid double */ public double consumeDouble(String name, boolean required) throws ParseException { return consumeDouble(name, required, 0); } /** * Gets the value of a float attribute and remove it from the list. * * @param name attribute name * @param required indicates attribute is required * @param defaultValue the default value for an optional attribute (used if * not present) * @return the float value of this attribute * @throws ParseException if required is set and the attribute is not defined, * or if the attribute value is not a valid float */ public float consumeFloat( String name, boolean required, float defaultValue) throws ParseException { String value = consume(name, required); if (value == null) { return defaultValue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -