Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.solr.common.util;
import static org.apache.solr.common.params.CommonParams.NAME;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.solr.common.ConfigNode;
import org.apache.solr.common.SolrException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/** */
public class DOMUtil {
public static final String XML_RESERVED_PREFIX = "xml";
public static final Set NL_TAGS =
Set.of("str", "int", "long", "float", "double", "bool", "null");
public static Map toMap(NamedNodeMap attrs) {
return toMapExcept(attrs);
}
public static Map toMap(ConfigNode node) {
return toMapExcept(node);
}
public static Map toMapExcept(ConfigNode node, String... exclusions) {
Map args = new HashMap<>();
node.attributes()
.forEach(
(k, v) -> {
for (String ex : exclusions) if (ex.equals(k)) return;
args.put(k, v);
});
return args;
}
public static Map toMapExcept(NamedNodeMap attrs, String... exclusions) {
Map args = new HashMap<>();
outer:
for (int j = 0; j < attrs.getLength(); j++) {
Node attr = attrs.item(j);
// automatically exclude things in the xml namespace, ie: xml:base
if (XML_RESERVED_PREFIX.equals(attr.getPrefix())) continue outer;
String attrName = attr.getNodeName();
for (String ex : exclusions) if (ex.equals(attrName)) continue outer;
String val = attr.getNodeValue();
args.put(attrName, val);
}
return args;
}
public static Node getChild(Node node, String name) {
if (!node.hasChildNodes()) return null;
NodeList lst = node.getChildNodes();
if (lst == null) return null;
for (int i = 0; i < lst.getLength(); i++) {
Node child = lst.item(i);
if (name.equals(child.getNodeName())) return child;
}
return null;
}
public static String getAttr(NamedNodeMap attrs, String name) {
return getAttr(attrs, name, null);
}
public static String getAttr(Node nd, String name) {
return getAttr(nd.getAttributes(), name);
}
public static String getAttrOrDefault(Node nd, String name, String def) {
String attr = getAttr(nd.getAttributes(), name);
return attr == null ? def : attr;
}
public static String getAttr(NamedNodeMap attrs, String name, String missing_err) {
Node attr = attrs == null ? null : attrs.getNamedItem(name);
if (attr == null) {
if (missing_err == null) return null;
throw new RuntimeException(missing_err + ": missing mandatory attribute '" + name + "'");
}
String val = attr.getNodeValue();
return val;
}
public static String getAttr(ConfigNode node, String name, String missing_err) {
String attr = node.attributes().get(name);
if (attr == null) {
if (missing_err == null) return null;
throw new RuntimeException(missing_err + ": missing mandatory attribute '" + name + "'");
}
return attr;
}
public static String getAttr(Node node, String name, String missing_err) {
return getAttr(node.getAttributes(), name, missing_err);
}
//////////////////////////////////////////////////////////
// Routines to parse XML in the syntax of the Solr query
// response schema.
// Should these be moved to Config? Should all of these things?
//////////////////////////////////////////////////////////
public static NamedList