/** * @param context * @param textNode * @throws IOException */ private void handleJSTextNode(ReplayParseContext context, TextNode textNode) throws IOException { boolean alreadyInsertedHead = (context.getData(FERRET_HEAD_INSERTED) != null); context.incJSBlockCount(); if (alreadyInsertedHead) { textNode.setText(jsBlockTrans.transform(context, textNode.getText())); } emit(context, null, textNode, null); }
/** * Set the enclosed <code>PARAM<code> children. * @param newObjectParams The new parameters. */ public void setObjectParams(HashMap newObjectParams) { NodeList kids; Node node; Tag tag; String paramName; String paramValue; List attributes; TextNode string; kids = getChildren(); if (null == kids) kids = new NodeList(); else // erase objectParams from kids for (int i = 0; i < kids.size(); ) { node = kids.elementAt(i); if (node instanceof Tag) if (((Tag) node).getTagName().equals("PARAM")) { kids.remove(i); // remove whitespace too if (i < kids.size()) { node = kids.elementAt(i); if (node instanceof TextNode) { string = (TextNode) node; if (0 == string.getText().trim().length()) kids.remove(i); } } } else i++; else i++; } // add newObjectParams to kids for (Iterator e = newObjectParams.entrySet().iterator(); e.hasNext(); ) { Map.Entry entry = (Entry) e.next(); attributes = new ArrayList(); // should the tag copy the attributes? paramName = (String) entry.getKey(); paramValue = (String) entry.getValue(); attributes.add(new Attribute("PARAM", null)); attributes.add(new Attribute(" ")); attributes.add(new Attribute("VALUE", paramValue, '"')); attributes.add(new Attribute(" ")); attributes.add(new Attribute("NAME", paramName.toUpperCase(), '"')); tag = new TagNode(null, 0, 0, attributes); kids.add(tag); } // set kids as new children setChildren(kids); }
/** * 生成预览内容 * * @param html * @param max_count * @return */ public static String preview(String html, int max_count) { if (html.length() <= max_count * 1.1) return html; Parser parser = new Parser(); StringBuffer prvContent = new StringBuffer(); try { parser.setEncoding(Globals.ENC_8859_1); parser.setInputHTML(html); parser.setNodeFactory(factory); NodeList nodes = parser.extractAllNodesThatMatch(nfilter); Node node = null; for (int i = 0; i < nodes.size(); i++) { if (prvContent.length() >= max_count) { if (node instanceof TagNode) { TagNode tmp_node = (TagNode) node; boolean isEnd = tmp_node.isEndTag(); if (!isEnd) { prvContent.setLength(prvContent.length() - tmp_node.getText().length() - 2); } } // 补齐所有未关闭的标签 Node parent = node; // System.out.println("current node is . "+parent.getText()); do { // System.out.println(parent.getClass().getName()+":"+parent.getText()); parent = parent.getParent(); // System.out.println("parent = "+parent); if (parent == null) break; if (!(parent instanceof TagNode)) continue; // System.out.println("Parent node is no ended. "+parent.getText()); prvContent.append(((TagNode) parent).getEndTag().toHtml()); } while (true); break; } node = nodes.elementAt(i); if (node instanceof TagNode) { TagNode tag = (TagNode) node; prvContent.append('<'); prvContent.append(tag.getText()); prvContent.append('>'); // System.out.println("TAG: " + '<'+tag.getText()+'>'); } else if (node instanceof TextNode) { int space = max_count - prvContent.length(); if (space > 10) { TextNode text = (TextNode) node; if (text.getText().length() < 10) prvContent.append(text.getText()); else prvContent.append( StringUtils.abbreviate(text.getText(), max_count - prvContent.length())); // System.out.println("TEXT: " + text.getText()); } } } return prvContent.toString(); } catch (ParserException e) { e.printStackTrace(); } finally { parser = null; } return html; }
/** * @param context * @param textNode * @throws IOException */ private void handleCSSTextNode(ReplayParseContext context, TextNode textNode) throws IOException { textNode.setText(cssBlockTrans.transform(context, textNode.getText())); emit(context, null, textNode, null); }