nbsputility.java

来自「随书的代码」· Java 代码 · 共 39 行

JAVA
39
字号
import org.w3c.dom.*;public class NBSPUtility {    // Recursively descend the tree replacing all non-breaking  // spaces with &nbsp;  public static void addEntityReferences(Node node) {        int type = node.getNodeType();    if (type == Node.TEXT_NODE) {                 // the only type with attributes      Text text = (Text) node;      String s = text.getNodeValue();      int nbsp = s.indexOf('\u00A0'); // finds the first A0      if (nbsp != -1) {        Text middle = text.splitText(nbsp);        Text end = middle.splitText(1);        Node parent = text.getParentNode();        Document factory = text.getOwnerDocument();        EntityReference ref = factory.createEntityReference("nbsp");        parent.replaceChild(ref, middle);        addEntityReferences(end); // finds any subsequent A0s        System.out.println("Added");      }    } // end if         else if (node.hasChildNodes()) {      NodeList children = node.getChildNodes();      for (int i = 0; i < children.getLength(); i++) {        Node child = children.item(i);        addEntityReferences(child);      } // end for    } // end if       }  // end addEntityReferences()  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?