下面列出了org.w3c.dom.Attr#getValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Float getFloat(String key)
{
Attr attr = element.getAttributeNode(key);
if (attr == null)
{
return null;
}
String strValue = attr.getValue();
try
{
return new Float(strValue);
}
catch (NumberFormatException e)
{
CoreEPLPlugin.log("Memento problem - Invalid float for key: " //$NON-NLS-1$
+ key + " value: " + strValue, e); //$NON-NLS-1$
return null;
}
}
public static void test4() {
String name = "name";
String correctValue = "correct value";
String wrongValue = "wrong value";
IIOMetadataNode parent = new IIOMetadataNode("parent");
MyAttrNode attrNode1 = new MyAttrNode(name, wrongValue);
MyAttrNode attrNode2 = new MyAttrNode(name, correctValue);
parent.setAttributeNode(attrNode1);
parent.setAttributeNode(attrNode2);
Attr actAttr = parent.getAttributeNode(name);
String actValue = actAttr.getValue();
if (!actValue.equals(correctValue)) {
throw new RuntimeException("Test 4 failed: Return value is: " +
actValue);
}
}
public void registerNamespaces(Element e) {
for (Iterator iter = XmlUtil.getAllAttributes(e); iter.hasNext();) {
Attr a = (Attr) iter.next();
if (a.getName().equals(PREFIX_XMLNS)) {
// default namespace declaration
_nsSupport.declarePrefix("", a.getValue());
} else {
String prefix = XmlUtil.getPrefix(a.getName());
if (prefix != null && prefix.equals(PREFIX_XMLNS)) {
String nsPrefix = XmlUtil.getLocalPart(a.getName());
String uri = a.getValue();
_nsSupport.declarePrefix(nsPrefix, uri);
}
}
}
}
/**
* Returns the namespace URI bound by the given prefix.
*
* @param prefix
* Prefix to look up.
*/
String getNamespaceURI(String prefix) {
String nsDeclName = prefix.length()==0 ? "xmlns" : "xmlns:"+prefix;
for( Scope sp=this; sp!=null; sp=sp.parent ) {
for( int i=sp.currentNamespaces.size()-1; i>=0; i--) {
Attr a = sp.currentNamespaces.get(i);
if(a.getNodeName().equals(nsDeclName))
return a.getValue();
}
for( int i=sp.additionalNamespaces.size()-2; i>=0; i-=2 )
if(sp.additionalNamespaces.get(i).equals(prefix))
return sp.additionalNamespaces.get(i+1);
}
return null;
}
/**
* Returns the namespace URI bound by the given prefix.
*
* @param prefix
* Prefix to look up.
*/
String getNamespaceURI(@NotNull String prefix) {
String nsDeclName = prefix.length()==0 ? "xmlns" : "xmlns:"+prefix;
for( Scope sp=this; sp!=null; sp=sp.parent ) {
for( int i=sp.currentNamespaces.size()-1; i>=0; i--) {
Attr a = sp.currentNamespaces.get(i);
if(a.getNodeName().equals(nsDeclName))
return a.getValue();
}
for( int i=sp.additionalNamespaces.size()-2; i>=0; i-=2 )
if(sp.additionalNamespaces.get(i).equals(prefix))
return sp.additionalNamespaces.get(i+1);
}
return null;
}
/**
* Returns the name of the resource based a node's attributes.
* @param node the node.
* @return the name or null if it could not be inferred.
*/
static String getName(Node node) {
Attr attribute = (Attr) node.getAttributes().getNamedItemNS(null, ATTR_NAME);
if (attribute != null) {
return attribute.getValue();
}
return null;
}
/**
* This method will read a tree of elements and their attributes
*
* @param element the root element of the tree
*/
private void readXmlElement(
LinkedList<String> currentElementPath,
Element element ) {
//append this node element to the current path
currentElementPath.add(element.getNodeName());
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
readXmlElement(currentElementPath, (Element) childNode);
}
}
//read all attributes
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Attr attribute = (Attr) attributes.item(i);
String propertyName = getCurrentXmlElementPath(currentElementPath) + attribute.getName();
String propertyValue = attribute.getValue();
//put in the properties table
properties.put(propertyName, propertyValue);
log.debug("Added property with name '" + propertyName + "' and value '" + propertyValue + "'");
}
//after we are done with the node, remove it from the path
currentElementPath.removeLast();
}
/**
* Returns the name of the resource based a node's attributes.
* @param node the node.
* @return the name or null if it could not be inferred.
*/
static String getName(Node node) {
Attr attribute = (Attr) node.getAttributes().getNamedItemNS(null, ATTR_NAME);
if (attribute != null) {
return attribute.getValue();
}
return null;
}
@Override
public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) {
String value = attribute.getValue();
if (!value.isEmpty() && (value.charAt(0) != '@' && value.charAt(0) != '?')) {
// Make sure this is really one of the android: attributes
if (!ANDROID_URI.equals(attribute.getNamespaceURI())) {
return;
}
context.report(ISSUE, attribute, context.getLocation(attribute),
String.format("[I18N] Hardcoded string \"%1$s\", should use `@string` resource",
value));
}
}
public static String getAttribute(Element element, QName attName) {
Attr attr;
if (StringUtils.isEmpty(attName.getNamespaceURI())) {
attr = element.getAttributeNode(attName.getLocalPart());
} else {
attr = element.getAttributeNodeNS(attName.getNamespaceURI(), attName.getLocalPart());
}
return attr == null ? null : attr.getValue();
}
private boolean verifyIMSCOtherAttributes(Object content, Locator locator, VerifierContext context) {
boolean failed = false;
NamedNodeMap attributes = context.getXMLNode(content).getAttributes();
for (int i = 0, n = attributes.getLength(); i < n; ++i) {
boolean failedAttribute = false;
Node item = attributes.item(i);
if (!(item instanceof Attr))
continue;
Attr attribute = (Attr) item;
String nsUri = attribute.getNamespaceURI();
String localName = attribute.getLocalName();
if (localName == null)
localName = attribute.getName();
if (localName.indexOf("xmlns") == 0)
continue;
QName name = new QName(nsUri != null ? nsUri : "", localName);
Model model = getModel();
if (model.isNamespace(name.getNamespaceURI())) {
String nsLabel;
if (name.getNamespaceURI().indexOf(NAMESPACE_IMSC10_PREFIX) == 0)
nsLabel = "IMSC";
else if (name.getNamespaceURI().indexOf(NAMESPACE_EBUTT_PREFIX) == 0)
nsLabel = "EBUTT";
else
nsLabel = null;
if (nsLabel != null) {
Reporter reporter = context.getReporter();
String value = attribute.getValue();
if (!model.isGlobalAttribute(name)) {
reporter.logError(reporter.message(locator, "*KEY*", "Unknown attribute in {0} namespace ''{1}'' not permitted on ''{2}''.",
nsLabel, name, context.getBindingElementName(content)));
failedAttribute = true;
} else if (!model.isGlobalAttributePermitted(name, context.getBindingElementName(content))) {
reporter.logError(reporter.message(locator, "*KEY*", "{0} attribute ''{1}'' not permitted on ''{2}''.",
nsLabel, name, context.getBindingElementName(content)));
failedAttribute = true;
} else if (!verifyNonEmptyOrPadded(content, name, value, locator, context)) {
reporter.logError(reporter.message(locator, "*KEY*", "Invalid {0} value ''{1}''.", name, value));
failedAttribute = true;
} else if (nsLabel.equals("IMSC")) {
if (!verifyIMSCAttribute(content, locator, context, name, value)) {
reporter.logError(reporter.message(locator, "*KEY*", "Invalid {0} value ''{1}''.", name, value));
failedAttribute = true;
}
} else if (nsLabel.equals("EBUTT")) {
if (!verifyEBUTTAttribute(content, locator, context, name, value)) {
reporter.logError(reporter.message(locator, "*KEY*", "Invalid {0} value ''{1}''.", name, value));
failedAttribute = true;
}
}
}
}
if (failedAttribute)
failed = failedAttribute;
}
return !failed;
}
public ResourceResolverContext(Attr attr, String baseUri, boolean secureValidation) {
this.attr = attr;
this.baseUri = baseUri;
this.secureValidation = secureValidation;
this.uriToResolve = attr != null ? attr.getValue() : null;
}
/**
* Creates a <code>DOMKeyInfo</code> from XML.
*
* @param kiElem KeyInfo element
*/
public DOMKeyInfo(Element kiElem, XMLCryptoContext context,
Provider provider)
throws MarshalException
{
// get Id attribute, if specified
Attr attr = kiElem.getAttributeNodeNS(null, "Id");
if (attr != null) {
id = attr.getValue();
kiElem.setIdAttributeNode(attr, true);
} else {
id = null;
}
// get all children nodes
NodeList nl = kiElem.getChildNodes();
int length = nl.getLength();
if (length < 1) {
throw new MarshalException
("KeyInfo must contain at least one type");
}
List<XMLStructure> content = new ArrayList<XMLStructure>(length);
for (int i = 0; i < length; i++) {
Node child = nl.item(i);
// ignore all non-Element nodes
if (child.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
Element childElem = (Element)child;
String localName = childElem.getLocalName();
if (localName.equals("X509Data")) {
content.add(new DOMX509Data(childElem));
} else if (localName.equals("KeyName")) {
content.add(new DOMKeyName(childElem));
} else if (localName.equals("KeyValue")) {
content.add(DOMKeyValue.unmarshal(childElem));
} else if (localName.equals("RetrievalMethod")) {
content.add(new DOMRetrievalMethod(childElem,
context, provider));
} else if (localName.equals("PGPData")) {
content.add(new DOMPGPData(childElem));
} else { //may be MgmtData, SPKIData or element from other namespace
content.add(new javax.xml.crypto.dom.DOMStructure((childElem)));
}
}
keyInfoTypes = Collections.unmodifiableList(content);
}
/**
* Creates an <code>XMLObject</code> from an element.
*
* @param objElem an Object element
* @throws MarshalException if there is an error when unmarshalling
*/
public DOMXMLObject(Element objElem, XMLCryptoContext context,
Provider provider)
throws MarshalException
{
// unmarshal attributes
this.encoding = DOMUtils.getAttributeValue(objElem, "Encoding");
Attr attr = objElem.getAttributeNodeNS(null, "Id");
if (attr != null) {
this.id = attr.getValue();
objElem.setIdAttributeNode(attr, true);
} else {
this.id = null;
}
this.mimeType = DOMUtils.getAttributeValue(objElem, "MimeType");
NodeList nodes = objElem.getChildNodes();
int length = nodes.getLength();
List<XMLStructure> content = new ArrayList<XMLStructure>(length);
for (int i = 0; i < length; i++) {
Node child = nodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
Element childElem = (Element)child;
String tag = childElem.getLocalName();
if (tag.equals("Manifest")) {
content.add(new DOMManifest(childElem, context, provider));
continue;
} else if (tag.equals("SignatureProperties")) {
content.add(new DOMSignatureProperties(childElem, context));
continue;
} else if (tag.equals("X509Data")) {
content.add(new DOMX509Data(childElem));
continue;
}
//@@@FIXME: check for other dsig structures
}
content.add(new javax.xml.crypto.dom.DOMStructure(child));
}
if (content.isEmpty()) {
this.content = Collections.emptyList();
} else {
this.content = Collections.unmodifiableList(content);
}
this.objectElem = objElem;
}
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
if (element.getTagName().equals(TEXT_VIEW)) {
if (!element.hasAttributeNS(ANDROID_URI, ATTR_TEXT)
&& element.hasAttributeNS(ANDROID_URI, ATTR_ID)
&& !element.hasAttributeNS(ANDROID_URI, ATTR_TEXT_IS_SELECTABLE)
&& !element.hasAttributeNS(ANDROID_URI, ATTR_VISIBILITY)
&& !element.hasAttributeNS(ANDROID_URI, ATTR_ON_CLICK)
&& context.getMainProject().getTargetSdk() >= 11
&& context.isEnabled(SELECTABLE)) {
context.report(SELECTABLE, element, context.getLocation(element),
"Consider making the text value selectable by specifying " +
"`android:textIsSelectable=\"true\"`");
}
}
NamedNodeMap attributes = element.getAttributes();
for (int i = 0, n = attributes.getLength(); i < n; i++) {
Attr attribute = (Attr) attributes.item(i);
String name = attribute.getLocalName();
if (name == null || name.isEmpty()) {
// Attribute not in a namespace; we only care about the android: ones
continue;
}
boolean isEditAttribute = false;
switch (name.charAt(0)) {
case 'a': {
isEditAttribute = name.equals(ATTR_AUTO_TEXT);
break;
}
case 'b': {
isEditAttribute = name.equals(ATTR_BUFFER_TYPE) &&
attribute.getValue().equals(VALUE_EDITABLE);
break;
}
case 'p': {
isEditAttribute = name.equals(ATTR_PASSWORD)
|| name.equals(ATTR_PHONE_NUMBER)
|| name.equals(ATTR_PRIVATE_IME_OPTIONS);
break;
}
case 'c': {
isEditAttribute = name.equals(ATTR_CAPITALIZE)
|| name.equals(ATTR_CURSOR_VISIBLE);
break;
}
case 'd': {
isEditAttribute = name.equals(ATTR_DIGITS);
break;
}
case 'e': {
if (name.equals(ATTR_EDITABLE)) {
isEditAttribute = attribute.getValue().equals(VALUE_TRUE);
} else {
isEditAttribute = name.equals(ATTR_EDITOR_EXTRAS);
}
break;
}
case 'i': {
if (name.equals(ATTR_INPUT_TYPE)) {
String value = attribute.getValue();
isEditAttribute = !value.isEmpty() && !value.equals(VALUE_NONE);
} else {
isEditAttribute = name.equals(ATTR_INPUT_TYPE)
|| name.equals(ATTR_IME_OPTIONS)
|| name.equals(ATTR_IME_ACTION_LABEL)
|| name.equals(ATTR_IME_ACTION_ID)
|| name.equals(ATTR_INPUT_METHOD);
}
break;
}
case 'n': {
isEditAttribute = name.equals(ATTR_NUMERIC);
break;
}
}
if (isEditAttribute && ANDROID_URI.equals(attribute.getNamespaceURI()) && context.isEnabled(ISSUE)) {
Location location = context.getLocation(attribute);
String message;
String view = element.getTagName();
if (view.equals(TEXT_VIEW)) {
message = String.format(
"Attribute `%1$s` should not be used with `<TextView>`: " +
"Change element type to `<EditText>` ?", attribute.getName());
} else {
message = String.format(
"Attribute `%1$s` should not be used with `<%2$s>`: " +
"intended for editable text widgets",
attribute.getName(), view);
}
context.report(ISSUE, attribute, location, message);
}
}
}
@Override
public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) {
// Ignore tools:background and any other custom attribute that isn't actually the
// android View background attribute
if (!ANDROID_URI.equals(attribute.getNamespaceURI())) {
return;
}
// Only consider the root element's background
Element documentElement = attribute.getOwnerDocument().getDocumentElement();
if (documentElement == attribute.getOwnerElement()) {
// If the drawable is a non-repeated pattern then the overdraw might be
// intentional since the image isn't covering the whole screen
String background = attribute.getValue();
if (mValidDrawables != null && mValidDrawables.contains(background)) {
return;
}
if (background.equals(TRANSPARENT_COLOR) || background.equals(NULL_RESOURCE)) {
return;
}
if (background.startsWith("@android:drawable/")) { //$NON-NLS-1$
// We haven't had a chance to study the builtin drawables the way we
// check the project local ones in scanBitmap() and beforeCheckFile(),
// but many of these are not bitmaps, so ignore these
return;
}
String name = context.file.getName();
if (name.contains("list_") || name.contains("_item")) { //$NON-NLS-1$ //$NON-NLS-2$
// Canonical list_item layout name: don't warn about these, it's
// pretty common to want to paint custom list item backgrounds
return;
}
if (!context.getProject().getReportIssues()) {
// If this is a library project not being analyzed, ignore it
return;
}
Location location = context.getLocation(attribute);
location.setClientData(attribute);
if (mRootAttributes == null) {
mRootAttributes = new ArrayList<Pair<Location,String>>();
}
mRootAttributes.add(Pair.of(location, attribute.getValue()));
String activity = documentElement.getAttributeNS(TOOLS_URI, ATTR_CONTEXT);
if (activity != null && !activity.isEmpty()) {
if (activity.startsWith(".")) { //$NON-NLS-1$
activity = context.getProject().getPackage() + activity;
}
registerLayoutActivity(LintUtils.getLayoutName(context.file), activity);
}
}
}
/**
* Returns the Attr[]s to be output for the given element.
* <br>
* The code of this method is a copy of {@link #handleAttributes(Element,
* NameSpaceSymbTable)},
* whereas it takes into account that subtree-c14n is -- well --
* subtree-based.
* So if the element in question isRoot of c14n, it's parent is not in the
* node set, as well as all other ancestors.
*
* @param element
* @param ns
* @return the Attr[]s to be output
* @throws CanonicalizationException
*/
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
throws CanonicalizationException {
if (!element.hasAttributes() && !firstCall) {
return null;
}
// result will contain the attrs which have to be output
final SortedSet<Attr> result = this.result;
result.clear();
if (element.hasAttributes()) {
NamedNodeMap attrs = element.getAttributes();
int attrsLength = attrs.getLength();
for (int i = 0; i < attrsLength; i++) {
Attr attribute = (Attr) attrs.item(i);
String NUri = attribute.getNamespaceURI();
String NName = attribute.getLocalName();
String NValue = attribute.getValue();
if (!XMLNS_URI.equals(NUri)) {
// It's not a namespace attr node. Add to the result and continue.
result.add(attribute);
} else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
// The default mapping for xml must not be output.
Node n = ns.addMappingAndRender(NName, NValue, attribute);
if (n != null) {
// Render the ns definition
result.add((Attr)n);
if (C14nHelper.namespaceIsRelative(attribute)) {
Object exArgs[] = {element.getTagName(), NName, attribute.getNodeValue()};
throw new CanonicalizationException(
"c14n.Canonicalizer.RelativeNamespace", exArgs
);
}
}
}
}
}
if (firstCall) {
// It is the first node of the subtree
// Obtain all the namespaces defined in the parents, and added to the output.
ns.getUnrenderedNodes(result);
// output the attributes in the xml namespace.
xmlattrStack.getXmlnsAttr(result);
firstCall = false;
}
return result.iterator();
}
/**
* The latest version of XercesJ 2.9 returns an empty string for non existing
* attributes. To differentiate between empty attributes and non-existing
* attributes, this method returns a default value for non existing
* attributes.
*
* @param aElement
* the source element to get the attribute from. May not be
* <code>null</code>.
* @param sNamespaceURI
* The namespace URI of the attribute to retrieve. May be
* <code>null</code>.
* @param sAttrName
* the name of the attribute to query. May not be <code>null</code>.
* @param sDefault
* the value to be returned if the attribute is not present.
* @return the default value if the attribute does not exists, the string
* value otherwise
*/
@Nullable
public static String getAttributeValueNS (@Nonnull final Element aElement,
@Nullable final String sNamespaceURI,
@Nonnull final String sAttrName,
@Nullable final String sDefault)
{
final Attr aAttr = aElement.getAttributeNodeNS (sNamespaceURI, sAttrName);
return aAttr == null ? sDefault : aAttr.getValue ();
}
/**
* Returns the attribute value for the attribute with the specified name.
* Returns null if there is no such attribute, or
* the empty string if the attribute value is empty.
*
* <p>This works around a limitation of the DOM
* <code>Element.getAttributeNode</code> method, which does not distinguish
* between an unspecified attribute and an attribute with a value of
* "" (it returns "" for both cases).
*
* @param elem the element containing the attribute
* @param name the name of the attribute
* @return the attribute value (may be null if unspecified)
*/
public static String getAttributeValue(Element elem, String name) {
Attr attr = elem.getAttributeNodeNS(null, name);
return (attr == null) ? null : attr.getValue();
}
/**
* Returns the attribute value for the attribute with the specified name.
* Returns null if there is no such attribute, or
* the empty string if the attribute value is empty.
*
* <p>This works around a limitation of the DOM
* <code>Element.getAttributeNode</code> method, which does not distinguish
* between an unspecified attribute and an attribute with a value of
* "" (it returns "" for both cases).
*
* @param elem the element containing the attribute
* @param name the name of the attribute
* @return the attribute value (may be null if unspecified)
*/
public static String getAttributeValue(Element elem, String name) {
Attr attr = elem.getAttributeNodeNS(null, name);
return (attr == null) ? null : attr.getValue();
}