📄 printtask.java
字号:
} else {
subnode.getParentNode().appendChild(node2ReplaceWith);
subnode.getParentNode().removeChild(subnode);
}
}
replaceNode(node2Replace, nodeAttr2Replace,
node2ReplaceWith, subnode);
}
}
} catch (Exception e) {
logger.warn("Unable to find subnode", e);
}
}
private void expandAllTocNodes(Collection tocNodes) {
for (Iterator<TocNode> i = tocNodes.iterator(); i.hasNext();) {
TocNode tocNode = i.next();
tocNode.setAllExpanded(true, -1);
expandAllTocNodes(tocNode.getChildren());
}
}
private void initTocFunctionalities(WebToc webToc) {
List<TocFunctionality> funcs = webToc.getTocFunctionalities();
for (int i = 0; i < funcs.size(); i++) {
TocFunctionality func = funcs.get(i);
if (func instanceof GraphicsTocFunctionality) {
continue;
}
webToc.init(func);
}
}
private void readNodesExpanded(Collection tocNodes,
Map<String, Boolean> map, String tocPath) {
for (Iterator<TocNode> i = tocNodes.iterator(); i.hasNext();) {
TocNode tocNode = i.next();
String sKey = addToTocPath(tocPath, tocNode.getContent().getText());
map.put(sKey, new Boolean(tocNode.isExpanded()));
readNodesExpanded(tocNode.getChildren(), map, sKey);
}
delFromTocPath(tocPath);
}
private void applyNodesExpansion(Collection tocNodes,
Map<String, Boolean> map, String tocPath) {
for (Iterator<TocNode> i = tocNodes.iterator(); i.hasNext();) {
TocNode tocNode = i.next();
String sKey = addToTocPath(tocPath, tocNode.getContent().getText());
Boolean value = map.get(sKey);
tocNode.setExpanded((value != null) ? value.booleanValue() : false);
applyNodesExpansion(tocNode.getChildren(), map, sKey);
}
delFromTocPath(tocPath);
}
private String addToTocPath(String tocPath, String tocKey) {
return (!tocPath.equals("")) ? (tocPath + "/" + tocKey) : tocKey;
}
private String delFromTocPath(String tocPath) {
if (!tocPath.equals("")) {
int i = tocPath.lastIndexOf('/');
tocPath = (i == -1) ? "" : tocPath.substring(0, i);
return tocPath;
}
return "";
}
/**
* Create print map event
*/
public void createPrintPage(TaskEvent event) {
try {
setResultUrl("");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(this.getTemplate()));
WebMap webMap = event.getWebContext().getWebMap();
int width = webMap.getWidth();
int height = webMap.getHeight();
int mdpi = Integer.parseInt(this.getResolution());
webMap.setDpi(mdpi);
int mwidth = width;
int mheight = height;
if (!this.getMapWidth().equalsIgnoreCase("default")) {
mwidth = Integer.parseInt(this.getResolution()) * Integer.parseInt(this.getMapWidth());
mheight = (int) ((height * mwidth) / width);
}
Node titleNode = doc.createTextNode(this.getTitle());
replaceNode("JITK:TextElement", "Title", titleNode,
(Node) doc.getDocumentElement());
Node imgNode = doc.createElement("img");
Attr attr = doc.createAttribute("src");
attr.setNodeValue(WebUtil.getRequestContextPath() +
"/exportImageServlet?width=" + mwidth + "&height=" + mheight +
"&format=PNG&northArrow=true&graphicResources=true&scaleBar=true");
imgNode.getAttributes().setNamedItem(attr);
Attr style = doc.createAttribute("style");
style.setNodeValue("display: none;");
imgNode.getAttributes().setNamedItem(style);
Attr onload = doc.createAttribute("onload");
onload.setNodeValue("this.style.display = 'block';");
imgNode.getAttributes().setNamedItem(onload);
replaceNode("JITK:MapImage", null, imgNode, (Node) doc.getDocumentElement());
// Add legend
if (doc.getElementsByTagName("JITK:Legend").getLength() > 0) {
WebToc webToc = event.getWebContext().getWebToc();
Map<String, Boolean> NodesExpansion = new Hashtable<String, Boolean>();
readNodesExpanded(webToc.getRootNodes(), NodesExpansion, "");
webToc.setExpandLevel(-1);
expandAllTocNodes(webToc.getRootNodes());
initTocFunctionalities(webToc);
replaceNode("JITK:Legend", null,
doc.importNode(createLegend(webToc), true),
(Node) doc.getDocumentElement());
applyNodesExpansion(webToc.getRootNodes(), NodesExpansion, "");
}
String pid = ADFDownloadServlet.generateId();
pid = "pr_" + pid;
String xhtml = ElementToString(doc.getDocumentElement());
WebUtil.getExternalContext().getSessionMap().put(pid, xhtml);
setResultUrl(WebUtil.getRequestContextPath() + "/printServlet?id=" +
pid);
} catch (Exception e) {
logger.warn("Unable to create print map", e);
this.renderResourceMessage(ERROR_MSG);
}
}
private Node createLegend(WebToc toc) {
StringBuffer sb = new StringBuffer();
sb.append("<table class=\"legendTable\">\n");
for (Iterator<TocNode> i = toc.getRootNodes().iterator(); i.hasNext();) {
TocNode node = i.next();
Map<String, String> layers = new Hashtable<String, String>();
createServiceLegend(node, layers);
sb.append(outputServiceLegend(node.getContent().getText(), layers));
}
sb.append("</table>\n");
Document doc = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
doc = docBuilder.parse(new ByteArrayInputStream(
sb.toString().getBytes()));
} catch (Exception ex) {
}
return doc.getDocumentElement();
}
private StringBuffer outputServiceLegend(String sServiceName,
Map<String, String> layers) {
StringBuffer sb = new StringBuffer();
if (!layers.isEmpty()) {
sb.append("<tr><td colspan=\"6\" class=\"legendServiceName\">")
.append(sServiceName).append("</td></tr>\n");
int i = 0;
sb.append("<tr>\n");
for (Iterator<String> j = layers.keySet().iterator(); j.hasNext();
i++) {
String sLayerName = j.next();
if ((i != 0) && ((i % 3) == 0)) {
sb.append("</tr>\n<tr>\n");
}
sb.append(layers.get(sLayerName));
}
sb.append("</tr>\n");
}
return sb;
}
private void createServiceLegend(TocNode node, Map<String, String> layers) {
if (node.getChildren().size() == 0) {
return;
} else if (isOutputLayer(node)) {
String sLayerName = chkStr(node.getContent().getText(), "");
if (sLayerName.length() > 0) {
layers.put(sLayerName,
outputLayerItems(node.getChildren(), sLayerName).toString());
}
return;
} else {
// recursion
for (Iterator<TocNode> i = node.getChildren().iterator();
i.hasNext();) {
createServiceLegend(i.next(), layers);
}
}
}
private boolean isOutputLayer(TocNode node) {
for (Iterator<TocNode> i = node.getChildren().iterator(); i.hasNext();) {
TocNode subNode = i.next();
if (subNode.getChildren().size() > 0) { //|| subNode.getContent().getImageUrl() == null
return false;
}
}
return true;
}
private StringBuffer outputLayerItems(Collection<TocNode> list,
String sLayerName) {
StringBuffer sb = new StringBuffer();
if (chkStr(sLayerName, "").length() != 0) {
sb.append("<td class=\"legendLayerName\" valign=\"top\">\n");
sb.append("<strong>").append(sLayerName).append("</strong>\n");
sb.append("<div class=\"legendLayerImage\">\n");
for (Iterator<TocNode> i = list.iterator(); i.hasNext();) {
sb.append(outputLayerItem(i.next()));
}
sb.append("</div>\n").append("</td>\n");
}
return sb;
}
private StringBuffer outputLayerItem(TocNode node) {
StringBuffer sb = new StringBuffer();
if (node != null) {
TocNodeContent content = node.getContent();
String sUrl = chkStr(content.getImageUrl(), "");
if (sUrl.length() > 0) {
sb.append("<img src=\"").append(escape(sUrl)).append("\" /> ");
}
String sVal = chkStr(content.getText(), "_");
sb.append(escape(sVal)).append("<br />\n");
}
return sb;
}
private String chkStr(String s, String dflt) {
return ((s != null) && (s.length() > 0)) ? s : dflt;
}
/**
* Escapes special xml characters within a string.
* <p> < > & " are escaped. The single quote character is not escaped.
* @param s the string to escape
* @return the escaped string
*/
public String escape(String s) {
StringBuffer sb = new StringBuffer();
if ((s != null) && (s.length() > 0)) {
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c == '&') {
sb.append("&");
} else if (c == '<') {
sb.append("<");
} else if (c == '>') {
sb.append(">");
} else if (c == '"') {
sb.append(""");
} else {
sb.append(c);
}
}
}
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -