groovy.util.Node类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(262)

本文整理了Java中groovy.util.Node类的一些代码示例,展示了Node类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node类的具体详情如下:
包路径:groovy.util.Node
类名称:Node

Node介绍

[英]Represents an arbitrary tree node which can be used for structured metadata or any arbitrary XML-like tree. A node can have a name, a value and an optional Map of attributes. Typically the name is a String and a value is either a String or a List of other Nodes, though the types are extensible to provide a flexible structure, e.g. you could use a QName as the name which includes a namespace URI and a local name. Or a JMX ObjectName etc. So this class can represent metadata like {foo a=1 b="abc"} or nested metadata like {foo a=1 b="123" { bar x=12 text="hello" }}
[中]表示任意树节点,该节点可用于结构化元数据或任意类似XML的树。节点可以有名称、值和可选的属性映射。通常,名称是字符串,值是字符串或其他节点的列表,尽管类型可以扩展以提供灵活的结构,例如,您可以使用QName作为名称,其中包括名称空间URI和本地名称。或者一个JMX对象名等。所以这个类可以表示像{foo a=1 b="abc"}这样的元数据,或者像{foo a=1 b="123" { bar x=12 text="hello" }}这样的嵌套元数据

代码示例

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Replaces the current node with the supplied node.
 *
 * @param n the new Node
 * @return the original now replaced node
 */
public Node replaceNode(Node n) {
  if (parent() == null) {
    throw new UnsupportedOperationException("Replacing the root node is not supported");
  }
  List tail = getTail();
  parent().appendNode(n.name(), n.attributes(), n.value());
  parent().children().addAll(tail);
  getParentList(parent()).remove(this);
  this.setParent(null);
  return this;
}

代码示例来源:origin: groovy/groovy-core

private boolean isEmptyElement(Node node) {
  if (node == null) {
    throw new IllegalArgumentException("Node must not be null!");
  }
  if (!node.children().isEmpty()) {
    return false;
  }
  return node.text().length() == 0;
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Creates a new node as a child of the current node.
 *
 * @param name the name of the new node
 * @param attributes the attributes of the new node
 * @param value the value of the new node
 * @return the newly created <code>Node</code>
 */
public Node appendNode(Object name, Map attributes, Object value) {
  return new Node(this, name, attributes, value);
}

代码示例来源:origin: org.codehaus.groovy/groovy

private void appendNodes(Closure c) {
  List tail = getTail();
  for (Node child : buildChildrenFromClosure(c)) {
    parent().appendNode(child.name(), child.attributes(), child.value());
  }
  parent().children().addAll(tail);
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Adds sibling nodes (defined using builder-style notation via a Closure) after the current node.
 *
 * @param c A Closure defining the new sibling nodes to add using builder-style notation.
 */
public void plus(Closure c) {
  if (parent() == null) {
    throw new UnsupportedOperationException("Adding sibling nodes to the root node is not supported");
  }
  appendNodes(c);
}

代码示例来源:origin: me.tatarka.gradle/pom

/**
 * Apply unique ids for all nodes.
 * Remove '_' prefix from nodes (workaround for clashes with method names).
 *
 * @param root  current node
 * @param topId id to apply
 */
private static void prepareTree(Node root, String topId) {
  root.attributes().put(NID_ATTR, topId);
  String name = (String) root.name();
  if (name.startsWith("_")) {
    Node replace = new Node(null, name.substring(1));
    replace.setValue(root.value());
    root.replaceNode(replace);
  }
  int pos = 0;
  if (!isLeaf(root)) {
    List<Node> list = root.children();
    for (int i = 0, size = list.size(); i < size; i++) {
      pos += 1;
      prepareTree(list.get(i), topId + "_" + pos);
    }
  }
}

代码示例来源:origin: takari/polyglot-maven

private void merge(Properties props, Node node, String prefix) {
  assert props != null;
  assert node != null;
  assert prefix != null;

  String name = prefix + node.name();

  Object value = node.value();
  if (value instanceof String) {
   props.setProperty(name, String.valueOf(value));
  }

  Map attrs = node.attributes();
  for (Object key : attrs.keySet()) {
   props.setProperty(name + "." + key, String.valueOf(attrs.get(key)));
  }

  for (Object child : node.children()) {
   if (child instanceof Node) {
    merge(props, (Node) child, name + ".");
   }
  }
 }
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Provides lookup of elements by non-namespaced name
 *
 * @param key the name (or shortcut key) of the node(s) of interest
 * @return the nodes which match key
 */
public Object get(String key) {
  if (key != null && key.charAt(0) == '@') {
    String attributeName = key.substring(1);
    return attributes().get(attributeName);
  }
  if ("..".equals(key)) {
    return parent();
  }
  if ("*".equals(key)) {
    return children();
  }
  if ("**".equals(key)) {
    return depthFirst();
  }
  return getByName(key);
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Provides lookup of elements by QName.
 *
 * @param name the QName of interest
 * @return the nodes matching name
 */
public NodeList getAt(QName name) {
  NodeList answer = new NodeList();
  for (Object child : children()) {
    if (child instanceof Node) {
      Node childNode = (Node) child;
      Object childNodeName = childNode.name();
      if (name.matches(childNodeName)) {
        answer.add(childNode);
      }
    }
  }
  return answer;
}

代码示例来源:origin: org.codehaus.groovy/groovy

private static List getParentList(Node parent) {
  Object parentValue = parent.value();
  List parentList;
  if (parentValue instanceof List) {
    parentList = (List) parentValue;
  } else {
    parentList = new NodeList();
    parentList.add(parentValue);
    parent.setValue(parentList);
  }
  return parentList;
}

代码示例来源:origin: diffplug/goomph

@SuppressWarnings("unchecked")
@Override
protected void applyOnce(Project project) {
  extension = project.getExtensions().create(ResourceFiltersExtension.NAME, ResourceFiltersExtension.class);
  EclipseProjectPlugin.modifyEclipseProject(project, eclipseModel -> {
    eclipseModel.getProject().getFile().getXmlTransformer().addAction(xmlProvider -> {
      Node rootNode = (Node) xmlProvider.asNode();
      // remove the old filteredResources
      List<Node> toRemove = ((List<Node>) rootNode.children()).stream()
          .filter(Objects::nonNull)
          .filter(node -> FILTERED_RESOURCES.equals(node.name()))
          .collect(Collectors.toList());
      toRemove.forEach(rootNode::remove);
      // now add ours
      Node filteredResources = rootNode.appendNode(FILTERED_RESOURCES);
      for (ResourceFilter toExclude : extension.filters) {
        toExclude.appendToFilteredResources(filteredResources);
      }
    });
  });
}

代码示例来源:origin: diffplug/goomph

/** Creates an XML node representing all the repos in this model. */
private Node sourceNode(Node parent) {
  Node source = new Node(parent, "source");
  @SuppressWarnings("unchecked")
  BiConsumer<Iterable<String>, Consumer<Map<String, String>>> addRepos = (urls, repoAttributes) -> {
    for (String url : urls) {
      Node repository = source.appendNode("repository");
      repository.attributes().put("location", url);
      repoAttributes.accept(repository.attributes());
    }
  };
  addRepos.accept(repos, Consumers.doNothing());
  addRepos.accept(metadataRepos, repoAttr -> repoAttr.put("kind", "metadata"));
  addRepos.accept(artifactRepos, repoAttr -> repoAttr.put("kind", "artifact"));
  return source;
}

代码示例来源:origin: diffplug/goomph

Iterator<Node> classpathEntries = classpathNode.children().iterator();
while (classpathEntries.hasNext()) {
  Node entry = classpathEntries.next();
  String path = (String) entry.attributes().get("path");
  if (path != null && !path.isEmpty()) {
    if (path.endsWith(".jar")) {
  Node entry = classpathNode.appendNode("classpathentry");
  entry.attributes().put("combineaccessrules", "true");
  entry.attributes().put("exported", "true");
  entry.attributes().put("kind", "src");
  entry.attributes().put("path", "/" + projectDep);
};
    classpathEntries = classpathNode.children().iterator();
    while (classpathEntries.hasNext()) {
      Node entry = classpathEntries.next();
      String path = (String) entry.attributes().get("path");
      if (path != null && path.endsWith(jar)) {
        classpathEntries.remove();

代码示例来源:origin: org.codehaus.groovy/groovy

@Override
public Object getAttribute(final Object object, final String attribute) {
  Node n = (Node) object;
  return n.get("@" + attribute);
}

代码示例来源:origin: org.codehaus.groovy/groovy

@Override
public void setAttribute(final Object object, final String attribute, final Object newValue) {
  for (Object o : (NodeList) object) {
    Node node = (Node) o;
    node.attributes().put(attribute, newValue);
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

public void print(Node node) {
  out.printIndent();
  printName(node);
  Map attributes = node.attributes();
  boolean hasAttributes = attributes != null && !attributes.isEmpty();
  if (hasAttributes) {
    printAttributes(attributes);
  }
  Object value = node.value();
  if (value instanceof List) {
    if (!hasAttributes) {
      out.print("()");
    }
    printList((List) value);
  } else {
    if (value instanceof String) {
      out.print("('");
      out.print((String) value);
      out.println("')");
    } else {
      out.println("()");
    }
  }
  out.flush();
}

代码示例来源:origin: groovy/groovy-core

protected void printName(Node node, NamespaceContext ctx, boolean begin, boolean preserve) {
  if (node == null) {
    throw new NullPointerException("Node must not be null.");
  }
  Object name = node.name();
  if (name == null) {
    throw new NullPointerException("Name must not be null.");
  }
  if (!preserve || begin) printLineBegin();
  out.print("<");
  if (!begin) {
    out.print("/");
  }
  out.print(getName(node));
  if (ctx != null) {
    printNamespace(node, ctx);
  }
  if (begin) {
    printNameAttributes(node.attributes(), ctx);
  }
  out.print(">");
  if (!preserve || !begin) printLineEnd();
}

代码示例来源:origin: groovy/groovy-core

protected boolean printSpecialNode(Node node) {
    Object name = node.name();
    if (name != null && name instanceof QName) {
      QName qn = (QName) name;
      // check uri and for legacy cases just check prefix name (not recommended)
      if (qn.getNamespaceURI().equals("http://groovy.codehaus.org/2005/gsp") || qn.getPrefix().equals("gsp")) {
        String s = qn.getLocalPart();
        if (s.length() == 0) {
          throw new RuntimeException("No local part after 'gsp:' given in node " + node);
        }
        printGroovyTag(s, node.text());
        return true;
      }
    }
    return false;
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

protected void printName(Node node) {
  Object name = node.name();
  if (name != null) {
    out.print(name.toString());
  } else {
    out.print("null");
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Converts the text of this GPathResult to a Double object.
 *
 * @return the GPathResult, converted to a <code>Double</code>
 */
public Double toDouble() {
  if(textIsEmptyOrNull()){
    return null;
  }
  return StringGroovyMethods.toDouble((CharSequence)text());
}

相关文章