下面列出了javax.servlet.http.HttpUtils#org.w3c.dom.NodeList 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int computeLength(IIOMetadataNode root) {
NodeList list = root.getChildNodes();
int length = 0;
for (int i = 0; i < list.getLength(); i++) {
IIOMetadataNode node = (IIOMetadataNode)list.item(i);
String name = node.getNodeName();
if (format.isLeaf(name))
length += new Integer((String)Box.getAttribute(node, "Length")).intValue();
else
length += computeLength(node);
}
return length + (root.getNodeName().startsWith("JPEG2000") ? 8 : 0) ;
}
protected T buildModel(Element root) {
B builder = builder();
builder
.withName(root.getAttribute("name"))
.withNamespace(root.getAttribute("namespace"))
.withVersion(root.getAttribute("version"));
NodeList nodes = root.getElementsByTagNameNS(schemaURI, "description");
if (nodes.getLength() > 0)
{
Element description = (Element)nodes.item(0);
builder.withDescription(readDescription(description));
}
else {
logger.warn("No description was given for the capability {}", root.getAttribute("name"));
}
populateDefinitionSpecificData(builder, root);
builder.withMethods(buildMethods(root.getElementsByTagNameNS(schemaURI, "method")));
builder.withEvents(buildEvents(root.getElementsByTagNameNS(schemaURI, "event")));
return builder.build();
}
@Override
public void deserialize(Model aModel, Document aDocument) {
// Now, parse tables
NodeList theElements = aDocument.getElementsByTagName(TABLE);
for (int i = 0; i < theElements.getLength(); i++) {
Element theElement = (Element) theElements.item(i);
Table theTable = new Table();
theTable.setOwner(aModel);
deserializeProperties(theElement, theTable);
deserializeCommentElement(theElement, theTable);
getXMLModelSerializer().getXMLAttributeSerializer().deserialize(aModel, theTable, theElement);
getXMLModelSerializer().getXMLIndexSerializer().deserialize(theTable, theElement);
aModel.getTables().add(theTable);
}
}
public static void resolveLeafNodeValue(Node node) {
if (node != null) {
Element element = (Element) node;
NodeList childNodeList = element.getChildNodes();
for (int j = 0; j < childNodeList.getLength(); j++) {
Node chileNode = childNodeList.item(j);
if (!chileNode.hasChildNodes()) {
String nodeValue = resolveSystemProperty(chileNode.getTextContent());
childNodeList.item(j).setTextContent(nodeValue);
} else {
resolveLeafNodeValue(chileNode);
}
}
}
}
/**
* Method itemUnknownElement
*
* @param i index
* @return the element number of the unknown elements
*/
public Element itemUnknownElement(int i) {
NodeList nl = this.constructionElement.getChildNodes();
int res = 0;
for (int j = 0; j < nl.getLength(); j++) {
Node current = nl.item(j);
/**
* $todo$ using this method, we don't see unknown Elements
* from Signature NS; revisit
*/
if ((current.getNodeType() == Node.ELEMENT_NODE)
&& current.getNamespaceURI().equals(Constants.SignatureSpecNS)) {
res++;
if (res == i) {
return (Element) current;
}
}
}
return null;
}
void updateFromNativeNode(Node node, boolean fromScratch)
throws IIOInvalidTreeException {
NamedNodeMap attrs = node.getAttributes();
int value = getAttributeValue(node, attrs, "process", 0, 2, false);
tag = (value != -1) ? value+JPEG.SOF0 : tag;
// If samplePrecision is present, it must be 8.
// This just checks. We don't bother to assign the value.
value = getAttributeValue(node, attrs, "samplePrecision", 8, 8, false);
value = getAttributeValue(node, attrs, "numLines", 0, 65535, false);
numLines = (value != -1) ? value : numLines;
value = getAttributeValue(node, attrs, "samplesPerLine", 0, 65535, false);
samplesPerLine = (value != -1) ? value : samplesPerLine;
int numComponents = getAttributeValue(node, attrs, "numFrameComponents",
1, 4, false);
NodeList children = node.getChildNodes();
if (children.getLength() != numComponents) {
throw new IIOInvalidTreeException
("numFrameComponents must match number of children", node);
}
componentSpecs = new ComponentSpec [numComponents];
for (int i = 0; i < numComponents; i++) {
componentSpecs[i] = new ComponentSpec(children.item(i));
}
}
/**
* Find the value entry in a node
*
* @param n The node
* @return The value entry as a string
*/
public static String getNodeValue( Node n ) {
if ( n == null ) {
return null;
}
// Find the child-nodes of this Node n:
NodeList children = n.getChildNodes();
for ( int i = 0; i < children.getLength(); i++ ) {
// Try all children
Node childnode = children.item( i );
String retval = childnode.getNodeValue();
if ( retval != null ) { // We found the right value
return retval;
}
}
return null;
}
private void mergeStandardTextNode(Node node)
throws IIOInvalidTreeException {
// Convert to comments. For the moment ignore the encoding issue.
// Ignore keywords, language, and encoding (for the moment).
// If compression tag is present, use only entries with "none".
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
NamedNodeMap attrs = child.getAttributes();
Node comp = attrs.getNamedItem("compression");
boolean copyIt = true;
if (comp != null) {
String compString = comp.getNodeValue();
if (!compString.equals("none")) {
copyIt = false;
}
}
if (copyIt) {
String value = attrs.getNamedItem("value").getNodeValue();
COMMarkerSegment com = new COMMarkerSegment(value);
insertCOMMarkerSegment(com);
}
}
}
/**
* The set:distinct function returns a subset of the nodes contained in the node-set NS passed
* as the first argument. Specifically, it selects a node N if there is no node in NS that has
* the same string value as N, and that precedes N in document order.
*
* @param nl NodeList for the node-set.
* @return a NodeList with nodes from nl containing distinct string values.
* In other words, if more than one node in nl contains the same string value,
* only include the first such node found.
*
* @see <a href="http://www.exslt.org/">EXSLT</a>
*/
public static NodeList distinct(NodeList nl)
{
NodeSet dist = new NodeSet();
dist.setShouldCacheNodes(true);
Map<String, Node> stringTable = new HashMap<>();
for (int i = 0; i < nl.getLength(); i++)
{
Node currNode = nl.item(i);
String key = toString(currNode);
if (key == null)
dist.addElement(currNode);
else if (!stringTable.containsKey(key))
{
stringTable.put(key, currNode);
dist.addElement(currNode);
}
}
return dist;
}
/**
* Count nodes with a certain tag
*
* @param n The node to look in
* @param tag The tags to count
* @return The number of nodes found with a certain tag
*/
public static int countNodes( Node n, String tag ) {
NodeList children;
Node childnode;
int count = 0;
if ( n == null ) {
return 0;
}
children = n.getChildNodes();
for ( int i = 0; i < children.getLength(); i++ ) {
childnode = children.item( i );
if ( childnode.getNodeName().equalsIgnoreCase( tag ) ) {
// <file>
count++;
}
}
return count;
}
public void serializeBody(Object element, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
NodeList childNodes = ((Element)element).getChildNodes();
int len = childNodes.getLength();
for( int i=0; i<len; i++ ) {
Node child = childNodes.item(i);
switch(child.getNodeType()) {
case Node.CDATA_SECTION_NODE:
case Node.TEXT_NODE:
target.text(child.getNodeValue(),null);
break;
case Node.ELEMENT_NODE:
target.writeDom((Element)child,domHandler,null,null);
break;
}
}
}
private Set<PluginPackageDependency> parsePackageDependencies(NodeList packageDependencyNodes,
PluginPackage pluginPackage) throws XPathExpressionException {
Set<PluginPackageDependency> pluginPackageDependencies = new LinkedHashSet<>();
for (int i = 0; i < packageDependencyNodes.getLength(); i++) {
Node pluginPackageDependencyNode = packageDependencyNodes.item(i);
PluginPackageDependency pluginPackageDependency = new PluginPackageDependency();
pluginPackageDependency.setPluginPackage(pluginPackage);
pluginPackageDependency.setDependencyPackageName(
getNonNullStringAttribute(pluginPackageDependencyNode, "./@name", "Package dependency name"));
pluginPackageDependency.setDependencyPackageVersion(
getNonNullStringAttribute(pluginPackageDependencyNode, "./@version", "Package dependency version"));
pluginPackageDependency.setPluginPackage(pluginPackage);
pluginPackageDependencies.add(pluginPackageDependency);
}
return pluginPackageDependencies;
}
void updateFromNativeNode(Node node, boolean fromScratch)
throws IIOInvalidTreeException {
NamedNodeMap attrs = node.getAttributes();
int value = getAttributeValue(node, attrs, "process", 0, 2, false);
tag = (value != -1) ? value+JPEG.SOF0 : tag;
// If samplePrecision is present, it must be 8.
// This just checks. We don't bother to assign the value.
value = getAttributeValue(node, attrs, "samplePrecision", 8, 8, false);
value = getAttributeValue(node, attrs, "numLines", 0, 65535, false);
numLines = (value != -1) ? value : numLines;
value = getAttributeValue(node, attrs, "samplesPerLine", 0, 65535, false);
samplesPerLine = (value != -1) ? value : samplesPerLine;
int numComponents = getAttributeValue(node, attrs, "numFrameComponents",
1, 4, false);
NodeList children = node.getChildNodes();
if (children.getLength() != numComponents) {
throw new IIOInvalidTreeException
("numFrameComponents must match number of children", node);
}
componentSpecs = new ComponentSpec [numComponents];
for (int i = 0; i < numComponents; i++) {
componentSpecs[i] = new ComponentSpec(children.item(i));
}
}
public Configuration parseConfiguration(Element rootNode)
throws XMLParserException {
Configuration configuration = new Configuration();
NodeList nodeList = rootNode.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node childNode = nodeList.item(i);
if (childNode.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
if ("properties".equals(childNode.getNodeName())) {
parseProperties(childNode);
} else if ("classPathEntry".equals(childNode.getNodeName())) {
parseClassPathEntry(configuration, childNode);
} else if ("context".equals(childNode.getNodeName())) {
parseContext(configuration, childNode);
}
}
return configuration;
}
public static Range<Integer> parseIntegerRange(Element xmlElement, String tagName) {
NodeList items = xmlElement.getElementsByTagName(tagName);
if (items.getLength() == 0)
return null;
Element tag = (Element) items.item(0);
items = tag.getElementsByTagName("min");
if (items.getLength() == 0)
return null;
Element min = (Element) items.item(0);
items = tag.getElementsByTagName("max");
if (items.getLength() == 0)
return null;
Element max = (Element) items.item(0);
String minText = min.getTextContent();
String maxText = max.getTextContent();
Range<Integer> r = Range.closed(Integer.valueOf(minText), Integer.valueOf(maxText));
return r;
}
/**
* Get the value of a tag in a node
*
* @param n The node to look in
* @param tag The tag to look for
* @return The value of the tag or null if nothing was found.
*/
public static String getTagValue( Node n, String tag ) {
NodeList children;
Node childnode;
if ( n == null ) {
return null;
}
children = n.getChildNodes();
for ( int i = 0; i < children.getLength(); i++ ) {
childnode = children.item( i );
if ( childnode.getNodeName().equalsIgnoreCase( tag ) ) {
if ( childnode.getFirstChild() != null ) {
return childnode.getFirstChild().getNodeValue();
}
}
}
return null;
}
void updateFromNativeNode(Node node, boolean fromScratch)
throws IIOInvalidTreeException {
NamedNodeMap attrs = node.getAttributes();
int value = getAttributeValue(node, attrs, "process", 0, 2, false);
tag = (value != -1) ? value+JPEG.SOF0 : tag;
// If samplePrecision is present, it must be 8.
// This just checks. We don't bother to assign the value.
value = getAttributeValue(node, attrs, "samplePrecision", 8, 8, false);
value = getAttributeValue(node, attrs, "numLines", 0, 65535, false);
numLines = (value != -1) ? value : numLines;
value = getAttributeValue(node, attrs, "samplesPerLine", 0, 65535, false);
samplesPerLine = (value != -1) ? value : samplesPerLine;
int numComponents = getAttributeValue(node, attrs, "numFrameComponents",
1, 4, false);
NodeList children = node.getChildNodes();
if (children.getLength() != numComponents) {
throw new IIOInvalidTreeException
("numFrameComponents must match number of children", node);
}
componentSpecs = new ComponentSpec [numComponents];
for (int i = 0; i < numComponents; i++) {
componentSpecs[i] = new ComponentSpec(children.item(i));
}
}
private static Node getNodeByXpath(String xml, String xPathString) throws Exception {
DocumentBuilder builder;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xml)));
} catch (Exception e) {
logger.warning("Failed to parse xml." + e.getMessage());
}
XPath xpathCompiler = XPathFactory.newInstance().newXPath();
NodeList nodes =
(NodeList) xpathCompiler.compile(xPathString).evaluate(doc, XPathConstants.NODESET);
if (nodes.getLength() == 0) {
return null;
}
return nodes.item(0);
}
private static boolean isNewFormatNode(Node node) {
// check for new node format - if the first non-whitespace node
// is an XML comment, and the comment includes
// one of the old element tags,
// then it is a generated node
NodeList children = node.getChildNodes();
int length = children.getLength();
for (int i = 0; i < length; i++) {
Node childNode = children.item(i);
if (childNode != null && childNode.getNodeType() == Node.COMMENT_NODE) {
String commentData = ((Comment) childNode).getData();
return MergeConstants.comentContainsTag(commentData);
}
}
return false;
}
/**
* Return the string value of a Node
*
* @param n The Node.
* @return The string value of the Node
*/
protected static String toString(Node n)
{
if (n instanceof DTMNodeProxy)
return ((DTMNodeProxy)n).getStringValue();
else
{
String value = n.getNodeValue();
if (value == null)
{
NodeList nodelist = n.getChildNodes();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < nodelist.getLength(); i++)
{
Node childNode = nodelist.item(i);
buf.append(toString(childNode));
}
return buf.toString();
}
else
return value;
}
}
/** Constructs a <code>FileTypeBox</code> from
* <code>org.w3c.dom.Node</code>.
*/
public FileTypeBox(Node node) throws IIOInvalidTreeException {
super(node);
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
String name = child.getNodeName();
if ("Brand".equals(name)) {
brand = Box.getIntElementValue(child);
}
if ("MinorVersion".equals(name)) {
minorVersion = Box.getIntElementValue(child);
}
if ("CompatibilityList".equals(name)) {
compatibility = Box.getIntArrayElementValue(child);
}
}
}
/**
* Creates a new XML Schema element of the given local name
* and insert it as the first child of the given parent node.
*
* @return
* Newly create element.
*/
private Element insertXMLSchemaElement( Element parent, String localName ) {
// use the same prefix as the parent node to avoid modifying
// the namespace binding.
String qname = parent.getTagName();
int idx = qname.indexOf(':');
if(idx==-1) qname = localName;
else qname = qname.substring(0,idx+1)+localName;
Element child = parent.getOwnerDocument().createElementNS( WellKnownNamespace.XML_SCHEMA, qname );
NodeList children = parent.getChildNodes();
if( children.getLength()==0 )
parent.appendChild(child);
else
parent.insertBefore( child, children.item(0) );
return child;
}
/**
* Get the value of a tag in a node
*
* @param n The node to look in
* @param tag The tag to look for
* @return The value of the tag or null if nothing was found.
*/
public static String getTagValueWithAttribute( Node n, String tag, String attribute ) {
NodeList children;
Node childnode;
if ( n == null ) {
return null;
}
children = n.getChildNodes();
for ( int i = 0; i < children.getLength(); i++ ) {
childnode = children.item( i );
if ( childnode.getNodeName().equalsIgnoreCase( tag )
&& childnode.getAttributes().getNamedItem( attribute ) != null ) {
if ( childnode.getFirstChild() != null ) {
return childnode.getFirstChild().getNodeValue();
}
}
}
return null;
}
protected void validateIdAttributes(Document doc) throws Exception {
String idAttribute = new String(idIdentifier.getText());
if( idAttribute == null || idAttribute.isEmpty() ) {
return;
}
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("descendant-or-self::*/@" + idAttribute);
NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
if(nodeList != null && nodeList.getLength() > 0) {
for(int j = 0; j < nodeList.getLength(); j++) {
Attr attr = (Attr)nodeList.item(j);
((Element)attr.getOwnerElement()).setIdAttributeNode(attr,true);
}
}
}
/**
* Get nodes with a certain tag one level down
*
* @param n The node to look in
* @param tag The tags to count
* @return The list of nodes found with the specified tag
*/
public static List<Node> getNodes( Node n, String tag ) {
NodeList children;
Node childnode;
List<Node> nodes = new ArrayList<Node>();
if ( n == null ) {
return nodes;
}
children = n.getChildNodes();
for ( int i = 0; i < children.getLength(); i++ ) {
childnode = children.item( i );
if ( childnode.getNodeName().equalsIgnoreCase( tag ) ) {
// <file>
nodes.add( childnode );
}
}
return nodes;
}
protected void parseJavaModelGenerator(Context context, Node node) {
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
context
.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
Properties attributes = parseAttributes(node);
String targetPackage = attributes.getProperty("targetPackage");
String targetProject = attributes.getProperty("targetProject");
javaModelGeneratorConfiguration.setTargetPackage(targetPackage);
javaModelGeneratorConfiguration.setTargetProject(targetProject);
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node childNode = nodeList.item(i);
if (childNode.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
if ("property".equals(childNode.getNodeName())) {
parseProperty(javaModelGeneratorConfiguration, childNode);
}
}
}
private Set<PluginPackageMenu> parseMenus(NodeList menuNodes, PluginPackage pluginPackage)
throws XPathExpressionException {
Set<PluginPackageMenu> pluginPackageMenus = new LinkedHashSet<>();
for (int i = 0; i < menuNodes.getLength(); i++) {
Node pluginPackageMenuNode = menuNodes.item(i);
PluginPackageMenu pluginPackageMenu = new PluginPackageMenu();
pluginPackageMenu
.setCode(getNonNullStringAttribute(pluginPackageMenuNode, "./@code", "Plugin package menu code"));
pluginPackageMenu.setCategory(
getNonNullStringAttribute(pluginPackageMenuNode, "./@cat", "Plugin package menu category"));
String menuDisplayName = getNonNullStringAttribute(pluginPackageMenuNode, "./@displayName",
"Plugin package menu display name");
pluginPackageMenu.setDisplayName(menuDisplayName);
String localDisplayName = getStringAttribute(pluginPackageMenuNode, "./@localDisplayName");
if (StringUtils.isNotBlank(localDisplayName)) {
pluginPackageMenu.setLocalDisplayName(localDisplayName);
} else {
pluginPackageMenu.setLocalDisplayName(menuDisplayName);
}
pluginPackageMenu.setPath(pluginPackageMenuNode.getTextContent());
pluginPackageMenu.setSource(pluginPackage.getId());
pluginPackageMenu.setPluginPackage(pluginPackage);
pluginPackageMenus.add(pluginPackageMenu);
}
return pluginPackageMenus;
}
/**
* Removes whitespaces from nodes
* @param node Root node
*/
public static void trimWhitespace(Node node) {
NodeList children = node.getChildNodes();
for(int i = 0; i < children.getLength(); ++i) {
Node child = children.item(i);
if(child.getNodeType() == Node.TEXT_NODE) {
child.setTextContent(child.getTextContent().trim());
}
trimWhitespace(child);
}
}
/**
* Load local xml data for case-value mapping, transform info.
*
* @return
* @throws URISyntaxException
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
private static Node loadTransformXmlMetadata( String fileName ) throws URISyntaxException, ParserConfigurationException, SAXException, IOException {
String PKG = SwitchCaseTest.class.getPackage().getName().replace( ".", "/" );
PKG = PKG + "/";
URL url = SwitchCaseTest.class.getClassLoader().getResource( PKG + fileName );
File file = new File( url.toURI() );
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse( file );
NodeList nList = doc.getElementsByTagName( "transform" );
return nList.item( 0 );
}
private void parse(Document doc) {
NodeList nodeList = doc.getChildNodes();
for (int count = 0; count < nodeList.getLength(); count++) {
Node node = nodeList.item(count);
if (node.getNodeType() == Node.ELEMENT_NODE
&& node.hasChildNodes()) {
parseAttrList(node.getChildNodes());
}
}
}