public Map<Long, WeiboData> gexfDecoder(Resources res, String Filename) { SAXReader saxReader = new SAXReader(); Document document = null; Map<Long, WeiboData> map = new HashMap<Long, WeiboData>(); try { InputStream inpt_strm = res.getAssets().open(Filename); document = saxReader.read(inpt_strm); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Element root = document.getRootElement(); Element graph = (Element) root.element("graph"); List<Element> nodes = graph.element("nodes").elements("node"); List<Element> edges = graph.element("edges").elements("edge"); // 读取节点信息 for (int i = 0; i < nodes.size(); i++) { Element node = nodes.get(i); // String sss = node.attribute("id").getText(); long id = Long.parseLong(node.attribute("id").getText()); String label = node.attribute("label").getText(); List<Element> viz = node.content(); Element color = viz.get(0); int color_b = Integer.parseInt(color.attribute("b").getText()); int color_g = Integer.parseInt(color.attribute("g").getText()); int color_r = Integer.parseInt(color.attribute("r").getText()); int color_int = Color.argb(255, color_r, color_g, color_b); Element position = viz.get(1); float x = Float.parseFloat(position.attribute("x").getText()); float y = Float.parseFloat(position.attribute("y").getText()); float z = Float.parseFloat(position.attribute("z").getText()); Element size = viz.get(2); float value = Float.parseFloat(size.attribute("value").getText()); Element image = viz.get(3); String imageURI = image.attribute("uri").getText(); System.out.println(label); WeiboData weibodata = new WeiboData(x, y, z, label, id, color_int, value, imageURI); map.put(id, weibodata); } for (int i = 0; i < edges.size(); i++) { Element edge = edges.get(i); long source_id = Long.parseLong(edge.attribute("source").getText()); long target_id = Long.parseLong(edge.attribute("target").getText()); WeiboData source = map.get(source_id); WeiboData target = map.get(target_id); if (source.parent == null) // 即还没有找到母节点 { source.parent = target; target.childs.add(source); } else { target.parent = source; source.childs.add(target); } } return map; }
@Override public String exe(Element element, int position, List<RenderItem> items) { @SuppressWarnings("unchecked") List<AbstractNode> contentList = element.content(); int i = 0; for (int cursor = 0; i < contentList.size(); ++i) { Object unit = contentList.get(i); if (unit instanceof Element) { Element subEle = (Element) unit; Resolver resolver = ResolverFactory.create(subEle.getName()); String resolveResult = resolver.exe(subEle, position + cursor, items); contentList.set(i, new DefaultText(resolveResult)); } cursor += ((DefaultText) contentList.get(i)).getText().length(); } return element.getStringValue(); }
/** * TODO 针对较小的UL合并行时做的调整 * * @param element 当前节点 * @throws Exception */ @SuppressWarnings("unchecked") public void modifyUL(Element element, HashSet<String> hashSet, String enginId) throws Exception { List<Node> nodeList = element.content(); Node nc; Element ec; String name; for (int m = 0; m < nodeList.size(); m++) { nc = nodeList.get(m); if (nc.getNodeType() == Node.ELEMENT_NODE) { name = nc.getName(); ec = (Element) nc; // 如果该孩子节点标签不是BR,收集该孩子节点 if (BR.equalsIgnoreCase(name)) { nodeList.remove(m--); element.setContent(nodeList); } modifyUL(ec, hashSet, enginId); } } }
/** * Outputs the content of the given element. If whitespace trimming is enabled then all adjacent * text nodes are appended together before the whitespace trimming occurs to avoid problems with * multiple text nodes being created due to text content that spans parser buffers in a SAX * parser. * * @param element DOCUMENT ME! * @throws IOException DOCUMENT ME! */ protected void writeElementContent(Element element) throws IOException { boolean trim = format.isTrimText(); boolean oldPreserve = preserve; if (trim) { // verify we have to before more expensive test preserve = isElementSpacePreserved(element); trim = !preserve; } if (trim) { // concatenate adjacent text nodes together // so that whitespace trimming works properly Text lastTextNode = null; StringBuilder buff = null; boolean textOnly = true; for (Node node : element.content()) { if (node instanceof Text) { if (lastTextNode == null) { lastTextNode = (Text) node; } else { if (buff == null) { buff = new StringBuilder(lastTextNode.getText()); } buff.append((node).getText()); } } else { if (!textOnly && format.isPadText()) { // only add the PAD_TEXT if the text itself starts with // whitespace char firstChar = 'a'; if (buff != null) { firstChar = buff.charAt(0); } else if (lastTextNode != null) { firstChar = lastTextNode.getText().charAt(0); } if (Character.isWhitespace(firstChar)) { writer.write(PAD_TEXT); } } if (lastTextNode != null) { if (buff != null) { writeString(buff.toString()); buff = null; } else { writeString(lastTextNode.getText()); } if (format.isPadText()) { // only add the PAD_TEXT if the text itself ends // with whitespace char lastTextChar = 'a'; if (buff != null) { lastTextChar = buff.charAt(buff.length() - 1); } else if (lastTextNode != null) { String txt = lastTextNode.getText(); lastTextChar = txt.charAt(txt.length() - 1); } if (Character.isWhitespace(lastTextChar)) { writer.write(PAD_TEXT); } } lastTextNode = null; } textOnly = false; writeNode(node); } } if (lastTextNode != null) { if (!textOnly && format.isPadText()) { // only add the PAD_TEXT if the text itself starts with // whitespace char firstChar = 'a'; if (buff != null) { firstChar = buff.charAt(0); } else { firstChar = lastTextNode.getText().charAt(0); } if (Character.isWhitespace(firstChar)) { writer.write(PAD_TEXT); } } if (buff != null) { writeString(buff.toString()); buff = null; } else { writeString(lastTextNode.getText()); } lastTextNode = null; } } else { Node lastTextNode = null; for (Node node : element.content()) { if (node instanceof Text) { writeNode(node); lastTextNode = node; } else { if ((lastTextNode != null) && format.isPadText()) { // only add the PAD_TEXT if the text itself ends with // whitespace String txt = lastTextNode.getText(); char lastTextChar = txt.charAt(txt.length() - 1); if (Character.isWhitespace(lastTextChar)) { writer.write(PAD_TEXT); } } writeNode(node); // if ((lastTextNode != null) && format.isPadText()) { // writer.write(PAD_TEXT); // } lastTextNode = null; } } } preserve = oldPreserve; }