📄 mxutils.java
字号:
{ BufferedImage result = null; if (w > 0 && h > 0) { // Checks if there is enough memory for allocating the buffer Runtime runtime = Runtime.getRuntime(); long maxMemory = runtime.maxMemory(); long allocatedMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); long totalFreeMemory = (freeMemory + (maxMemory - allocatedMemory)) / 1024; int bytes = 4; // 1 if indexed long memoryRequired = w * h * bytes / 1024; if (memoryRequired <= totalFreeMemory) { int type = (background != null) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; result = new BufferedImage(w, h, type); // Clears background Graphics2D g2 = (Graphics2D) result.createGraphics(); clearRect(g2, new Rectangle(w, h), background); g2.dispose(); } } return result; } /** * Returns a new, empty DOM document. * * @return Returns a new DOM document. */ public static Document createDocument() { try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); return parser.newDocument(); } catch (Exception e) { System.out.println(e.getMessage()); } return null; } /** * */ public static Document createSvgDocument(int width, int height) { Document document = createDocument(); Element root = document.createElement("svg"); String w = String.valueOf(width); String h = String.valueOf(height); root.setAttribute("width", w); root.setAttribute("height", h); root.setAttribute("viewBox", "0 0 " + w + " " + h); root.setAttribute("version", "1.1"); root.setAttribute("xmlns", "http://www.w3.org/2000/svg"); root.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"); document.appendChild(root); return document; } /** * */ public static Document createVmlDocument() { Document document = createDocument(); Element root = document.createElement("html"); root.setAttribute("xmlns:v", "urn:schemas-microsoft-com:vml"); root.setAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office"); document.appendChild(root); Element head = document.createElement("head"); Element style = document.createElement("style"); style.setAttribute("type", "text/css"); style .appendChild(document .createTextNode("<!-- v\\:* {behavior: url(#default#VML);} -->")); head.appendChild(style); root.appendChild(head); Element body = document.createElement("body"); root.appendChild(body); return document; } /** * */ public static Document createHtmlDocument() { Document document = createDocument(); Element root = document.createElement("html"); document.appendChild(root); Element head = document.createElement("head"); root.appendChild(head); Element body = document.createElement("body"); root.appendChild(body); return document; } /** * Returns a new, empty DOM document. * * @return Returns a new DOM document. */ public static HTMLDocument createHtmlDocument(Hashtable style) { return createHtmlDocument(style, 1); } /** * Returns a new, empty DOM document. * * @return Returns a new DOM document. */ public static HTMLDocument createHtmlDocument(Hashtable style, double scale) { // Applies the font settings HTMLDocument document = new HTMLDocument(); StringBuffer rule = new StringBuffer("body {"); rule.append(" font-family: " + getString(style, mxConstants.STYLE_FONTFAMILY, mxConstants.DEFAULT_FONTFAMILIES) + " ; "); rule.append(" font-size: " + (int) (getInt(style, mxConstants.STYLE_FONTSIZE, mxConstants.DEFAULT_FONTSIZE) * scale) + " pt ;"); String color = mxUtils.getString(style, mxConstants.STYLE_FONTCOLOR); if (color != null) { rule.append("color: " + color + " ; "); } int fontStyle = mxUtils.getInt(style, mxConstants.STYLE_FONTSTYLE); if ((fontStyle & mxConstants.FONT_BOLD) == mxConstants.FONT_BOLD) { rule.append(" font-weight: bold ; "); } if ((fontStyle & mxConstants.FONT_ITALIC) == mxConstants.FONT_ITALIC) { rule.append(" font-style: italic ; "); } if ((fontStyle & mxConstants.FONT_UNDERLINE) == mxConstants.FONT_UNDERLINE) { rule.append(" text-decoration: underline ; "); } String align = getString(style, mxConstants.STYLE_ALIGN, mxConstants.ALIGN_LEFT); if (align.equals(mxConstants.ALIGN_CENTER)) { rule.append(" text-align: center ; "); } else if (align.equals(mxConstants.ALIGN_RIGHT)) { rule.append(" text-align: right ; "); } rule.append(" } "); document.getStyleSheet().addRule(rule.toString()); return document; } /** * Creates a table for the given text using the given document to create * the DOM nodes. Returns the outermost table node. */ public static Element createTable(Document document, String text, int x, int y, int w, int h, double scale, Hashtable style) { // Does not use a textbox as this must go inside another VML shape Element table = document.createElement("table"); if (text != null && text.length() > 0) { Element tr = document.createElement("tr"); Element td = document.createElement("td"); table.setAttribute("cellspacing", "0"); table.setAttribute("border", "0"); td.setAttribute("align", "center"); String fontColor = getString(style, mxConstants.STYLE_FONTCOLOR, "black"); String fontFamily = getString(style, mxConstants.STYLE_FONTFAMILY, mxConstants.DEFAULT_FONTFAMILIES); int fontSize = (int) (getInt(style, mxConstants.STYLE_FONTSIZE, mxConstants.DEFAULT_FONTSIZE) * scale); String s = "position:absolute;" + "left:" + String.valueOf(x) + "px;" + "top:" + String.valueOf(y) + "px;" + "width:" + String.valueOf(w) + "px;" + "height:" + String.valueOf(h) + "px;" + "font-size:" + String.valueOf(fontSize) + "px;" + "font-family:" + fontFamily + ";" + "color:" + fontColor + ";"; // Applies the background color String background = getString(style, mxConstants.STYLE_LABEL_BACKGROUNDCOLOR); if (background != null) { s += "background:" + background + ";"; } // Applies the border color String border = getString(style, mxConstants.STYLE_LABEL_BORDERCOLOR); if (border != null) { s += "border:" + border + " solid 1pt;"; } // Applies the opacity float opacity = getFloat(style, mxConstants.STYLE_TEXT_OPACITY, 100); if (opacity < 100) { // Adds all rules (first for IE) s += "filter:alpha(opacity=" + opacity + ");"; s += "opacity:" + (opacity / 100) + ";"; } td.setAttribute("style", s); String[] lines = text.split("\n"); for (int i = 0; i < lines.length; i++) { td.appendChild(document.createTextNode(lines[i])); td.appendChild(document.createElement("br")); } tr.appendChild(td); table.appendChild(tr); } return table; } /** * */ public static Image loadImage(String url) { Image img = null; URL realUrl = null; try { realUrl = new URL(url); } catch (Exception e) { realUrl = mxUtils.class.getResource(url); } if (url != null) { try { img = ImageIO.read(realUrl); } catch (IOException e1) { // ignore } } return img; } /** * Returns a new DOM document for the given URI. * * @param uri URI to parse into the document. * @return Returns a new DOM document for the given URI. */ public static Document loadDocument(String uri) { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); return docBuilder.parse(uri); } catch (Exception e) { e.printStackTrace(); } return null; } /** * Returns a new document for the given XML string. * * @param xml String that represents the XML data. * @return Returns a new XML document. */ public static Document parse(String xml) { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); return docBuilder.parse(new InputSource(new StringReader(xml))); } catch (Exception e) { e.printStackTrace(); } return null; } /** * Evaluates a Java expression to a class member using mxCodecRegistry. * The range of supported expressions is limited to static class * members such as mxEdgeStyle.ElbowConnector. * * @param expression * @return */ public static Object eval(String expression) { int dot = expression.lastIndexOf("."); if (dot > 0) { Class clazz = mxCodecRegistry.getClassForName(expression.substring( 0, dot)); if (clazz != null) { try { return clazz.getField(expression.substring(dot + 1)).get( null); } catch (Exception e) { // ignore } } } return expression; } /** * Returns a single node that matches the given XPath expression. * * @param doc Document that contains the nodes. * @param expression XPath expression to be matched. * @return Returns a single node matching the given expression. */ public static Node selectSingleNode(Document doc, String expression) { try { XPath xpath = XPathFactory.newInstance().newXPath(); return (Node) xpath.evaluate(expression, doc, XPathConstants.NODE); } catch (XPathExpressionException e) { // ignore } return null; } /** * Converts the ampersand, quote, prime, less-than and greater-than characters * to their corresponding HTML entities in the given string. */ public static String htmlEntities(String text) { return text.replaceAll("&", "&").replaceAll("\"", """) .replaceAll("'", "′").replaceAll("<", "<").replaceAll( ">", ">"); } /** * Returns a string that represents the given node. * * @param node Node to return the XML for. * @return Returns an XML string. */ public static String getXml(Node node) { try { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(node); ByteArrayOutputStream stream = new ByteArrayOutputStream(); Result dest = new StreamResult(stream); aTransformer.transform(src, dest); return stream.toString("UTF-8"); } catch (Exception e) { // ignore } return ""; } /** * Returns a pretty-printed XML string for the given node. * * @param node Node to return the XML for. * @return Returns a formatted XML string. */ public static String getPrettyXml(Node node) { return getPrettyXml(node, " ", ""); } /** * Returns a pretty-printed XML string for the given node. * * @param node Node to return the XML for. * @param tab String to be used for indentation of inner nodes. * @param indent Current indentation for the node. * @return Returns a formatted XML string. */ public static String getPrettyXml(Node node, String tab, String indent) { StringBuffer result = new StringBuffer(); if (node != null) { if (node.getNodeType() == Node.TEXT_NODE) { result.append(node.getNodeValue()); } else { result.append(indent + "<" + node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { // TODO: htmlEntities String value = attrs.item(i).getNodeValue(); result.append(" " + attrs.item(i).getNodeName() + "=\"" + value + "\""); } } Node tmp = node.getFirstChild(); if (tmp != null) { result.append(">\n"); while (tmp != null) { result.append(getPrettyXml(tmp, tab, indent + tab)); tmp = tmp.getNextSibling(); } result.append(indent + "</" + node.getNodeName() + ">\n"); } else { result.append("/>\n"); } } } return result.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -