Java tutorial
/* * Copyright (C) 2000-2012 InfoChamp System Corporation * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.gk.engine.client.utils; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import jfreecode.gwt.event.client.bus.EventObject; import jfreecode.gwt.event.client.bus.EventProcess; import jfreecode.gwt.event.client.bus.JsonConvert; import jfreecode.gwt.event.client.bus.obj.Info; import org.gk.engine.client.Engine; import org.gk.engine.client.IEngine; import org.gk.engine.client.logging.EngineLogger; import org.gk.ui.client.gkComponent; import com.extjs.gxt.ui.client.core.FastMap; import com.extjs.gxt.ui.client.core.FastSet; import com.extjs.gxt.ui.client.core.XDOM; import com.extjs.gxt.ui.client.js.JsUtil; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArrayString; import com.google.gwt.http.client.Request; import com.google.gwt.http.client.RequestBuilder; import com.google.gwt.http.client.RequestCallback; import com.google.gwt.http.client.RequestException; import com.google.gwt.http.client.Response; import com.google.gwt.http.client.URL; import com.google.gwt.xml.client.Document; import com.google.gwt.xml.client.Element; import com.google.gwt.xml.client.NamedNodeMap; import com.google.gwt.xml.client.Node; import com.google.gwt.xml.client.NodeList; /** * ? * * <pre> * ????? * ??? key,value?? * ????rendergul, * gul?nodereplace * <?/>attach??Document * ? * </pre> * * @author I21890 * @since 2011/4/19 */ public class ComLibrary { private static Map<String, String> library = new FastMap(); private static boolean ready; /** * ? * * @return boolean */ public static boolean isReady() { return ready; } /** * content/Serverpage ( .jsp , .htm) * * @param nodeName * @return boolean */ public static boolean isServerPage(String nodeName) { return getContent(nodeName).charAt(0) == '/'; } public static String getContent(String key) { return library.get(key) == null ? "" : library.get(key); } /** * ?? * * @param nodeName * @return boolean */ public static boolean contains(String nodeName) { return library.containsKey(nodeName); } /** * ?(${...}) * * @param nodeName * @return JavaScriptObject */ public static JavaScriptObject getLibraryAttributes(String nodeName) { String gul = getContent(nodeName); JsArrayString list = (JsArrayString) findReplaceAttributes(gul); Set libAttr = new FastSet(); for (int i = 0; i < list.length(); i++) { String el = replaceToEL(list.get(i)); if (matchELPattern(el)) { String key = retriveELParameter(el).trim(); libAttr.add(key); } else { libAttr.add(el); } } return JsUtil.toJavaScriptArray(libAttr.toArray()); } /** * ? * * @param node * @param gul * @return Map */ private static Map createMappingTable(Node node, String gul) { Map<String, String> nodeAttr = new FastMap(); // node?Map NamedNodeMap map = node.getAttributes(); for (int index = 0; index < map.getLength(); index++) { Node attrNode = map.item(index); nodeAttr.put("${" + attrNode.getNodeName() + "}", attrNode.getNodeValue()); } gul = replaceExtraBackslash(gul); // ?Map JsArrayString list = (JsArrayString) findReplaceAttributes(gul); Map<String, String> libAttr = new FastMap(); for (int i = 0; i < list.length(); i++) { String attr = list.get(i); if (!libAttr.containsKey(attr)) { String el = replaceToEL(attr); if (matchELPattern(el)) { String key = retriveELParameter(el).trim(); String value = nodeAttr.get("${" + key + "}"); libAttr.put(attr, execEL(el, key, value == null ? "" : value)); } else { if (!nodeAttr.containsKey(attr)) { libAttr.put(attr, XDOM.getUniqueId()); } } } } nodeAttr.putAll(libAttr); return nodeAttr; } /** * ? * * @param nodeName * @param originNode * @return NodeList */ public static NodeList replaceNode(String nodeName, Node originNode) { String gul = getContent(nodeName); return replaceNode(originNode, gul); } public static NodeList replaceNode(Node originNode, String gul) { gul = Engine.beforeParser(gul); Map mapping = createMappingTable(originNode, gul); for (Iterator it = mapping.entrySet().iterator(); it.hasNext();) { Entry<String, String> entry = (Entry) it.next(); gul = gul.replace(entry.getKey(), entry.getValue()); } Document doc = NodeUtils.parseGUL(gul); return doc.getFirstChild().getChildNodes(); } /** * id${xxx}??uniqueId * * @param gul * @return String */ public static String replaceAnonymousId(String gul) { JsArrayString list = (JsArrayString) findReplaceAttributes(gul); for (int i = 0; i < list.length(); i++) { gul = gul.replace(list.get(i), XDOM.getUniqueId()); } return gul; } /** * ?Expression Language ?? ${val1==''?'':val1} ? val1 * * @param el * @return String */ private static native String retriveELParameter(String el)/*-{ return el.substring(0, el.search(/(\==|\!=|\>|\<|\>=|\<=)/i)); }-*/; /** * ??ELregExp * * @param el * @return boolean */ private static native boolean matchELPattern(String el)/*-{ var reg = /(\w*|\W*)(={2}|\!=|\>|\<|\>\=|\<\=)(\s*)(\'\w*\'|\w*|\'\W*\'|\W*)(\s*)(\?)/i; return el.match(reg) != null; }-*/; private static String replaceToEL(String el) { return el.replaceAll("\\$\\{|\\}", "").replaceAll("<", "<"); } /** * ELbackslash(\)findReplaceAttributes?el * * @param gul * @return String */ private static String replaceExtraBackslash(String gul) { gul = gul.replace("=\\\"", "=\"").replace("}\\", "}"); return gul; } private static native String execEL(String el, String key, String value)/*-{ var reg = new RegExp("={2}|\!=|\>\=|\<\=|\>|\<", "i"); var vars = []; var symbol = el.match(reg); var symbolLen = symbol[0].length; vars[0] = el.substring(0, el.search(reg)); vars[1] = el.substring(el.search(reg) + symbolLen, el.search(/(\?)/i)); vars[2] = el.substring(el.search(/(\?)/i) + 1, el.search(/:/)); vars[3] = el.substring(el.search(/:/) + 1, el.length); for ( var i = 0; i < vars.length; i++) { if (vars[i] == key) { vars[i] = "'" + value + "'"; } } var newEL = vars[0] + symbol[0] + vars[1] + "?" + vars[2] + ":" + vars[3]; return $wnd.eval(newEL); }-*/; /** * GUL??? * * @param gul * @return JavaScriptObject */ private static native JavaScriptObject findReplaceAttributes(String gul)/*-{ var replaces = []; // ="??"??$?? var splitEL = gul.split(/=\"|\"|\(|(?=\$)/); for (i = 0; i < splitEL.length; i++) { // ? var mapper = splitEL[i].replace(/^\s*|\s*$/g, ''); // ?${...}??Array if (mapper.match(/\$\{.*\}$/) != null) { replaces.push(mapper); } } // ?='??'??$?? splitEL = gul.split(/='|'|\(|(?=\$)/); for (i = 0; i < splitEL.length; i++) { // ? var mapper = splitEL[i].replace(/^\s*|\s*$/g, ''); // ?${...}??Array if (mapper.match(/\$\{.*\}$/) != null) { replaces.push(mapper); } } return replaces; }-*/; /** * ??gkEngineGULLibrary * * @param allGULSyntax */ public static void setLibrary(String allGULSyntax) { if (allGULSyntax != null && allGULSyntax.length() > 0 && JsonConvert.isJSONString(allGULSyntax)) { library.clear(); Map lib = (Map) JsonConvert.jsonString2Object(allGULSyntax); library.putAll(lib); } } /** * * * @param url */ public static void loadingLibrary(String url) { if (url.equals("")) { ready = true; return; } requestGet(url); } public static void requestGet(String url) { RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url)); builder.setCallback(new RequestCallback() { @Override public void onError(Request request, Throwable exception) { EngineLogger.log(exception); } @Override public void onResponseReceived(Request request, Response response) { // responseOK??set if (response.getStatusCode() == Response.SC_OK) { setLibrary(response.getText()); ready = true; } else { loadingLibraryByEventBus(); } } }); try { builder.send(); } catch (RequestException e) { EngineLogger.log(e); } } /** * ??loadLibrary.bsh */ public static void loadingLibraryByEventBus() { String url = gkComponent.getURL(); if (url.startsWith("http:")) { IEngine.bus.publishRemote(new EventObject("loadLibrary.bsh"), new EventProcess() { @Override public void execute(String eventId, EventObject eo) { if (eo.getInfoType().equals(Info.MAP)) { Map infoMap = eo.getInfoMap(); for (Iterator it = infoMap.entrySet().iterator(); it.hasNext();) { Entry entry = (Entry) it.next(); library.put((String) entry.getKey(), (String) entry.getValue()); } } ready = true; } }); } } /** * node??override tag * * @param nName * @return boolean */ public static boolean checkParserTag(String nName) { if (nName.endsWith("#text") || nName.endsWith("#comment") || nName.endsWith("import") || nName.endsWith("js") || nName.endsWith("#cdata-section")) { return false; } return true; } /** * ?Override * * @param node * @param nName * @param strOverride * @param overrideType * @return String */ public static String getLibContent(Node node, String nName, String strOverride, String overrideType) { // ? String defaultLib = defaultLib(nName, overrideType); // override // false:?override // true:? // :override??,???override? if (strOverride != null && !strOverride.equals("")) { if (strOverride.equals("false")) { return ""; } else if (strOverride.equals("true")) { } else { defaultLib = strOverride; } } return getContent(defaultLib); } /** * Override * * @param node * @return Node */ public static Node overrideNode(Node node) { String nName = node.getNodeName(); if (!checkParserTag(nName)) { return node; } // node?Map Map nodeMap = NodeUtils.getAttributes(node); String strOverride = (String) empty(nodeMap.get("override")); String overrideType = (String) empty(nodeMap.get("type")); String libgul = ""; // ? libgul = getLibContent(node, nName, strOverride, overrideType); // ??override if (libgul.equals("") || libgul.equals("undefined")) { return node; } // ?Override node = updateOverrideAttr(libgul, node); return node; } /** * ?Override * * @param libgul * @param node * @return Node */ private static Node updateOverrideAttr(String libgul, Node node) { Document doc = NodeUtils.parseGUL(libgul); NodeList nodeList = doc.getChildNodes(); // ?override? NamedNodeMap libNodeMap = null; Map libMap = null; for (int i = 0; i < nodeList.getLength(); i++) { libNodeMap = nodeList.item(i).getAttributes(); // <page>....</page> if (nodeList.item(i).getNodeName().equals("page")) { Node libNode = nodeList.item(i).getFirstChild(); libNodeMap = libNode.getAttributes(); } } libMap = NodeUtils.getAttributes(node); // ?nodeMap Node attrNode; for (int i = 0; i < libNodeMap.getLength(); i++) { attrNode = libNodeMap.item(i); // type?update if (attrNode.getNodeName().equals("type")) continue; libMap.put(attrNode.getNodeName(), attrNode.getNodeValue()); } return node = updateNodeAttribute(node, libMap); } /** * node * * @param node * @param allAttr * @return Node */ private static Node updateNodeAttribute(Node node, Map allAttr) { Element el = (Element) node; Iterator userMap = allAttr.entrySet().iterator(); while (userMap.hasNext()) { Map.Entry entry = (Map.Entry) userMap.next(); el.setAttribute(entry.getKey().toString(), entry.getValue().toString()); } return el; } /** * x[nodeName]_[nodeType] : xfield_txt;xfield_date;xform * * @param nodeName * @param nodeType * @return String */ private static String defaultLib(String nodeName, String nodeType) { if (nodeType.equals("") || nodeType.equals("undefined")) { return "x" + nodeName; } return "x" + nodeName + "_" + nodeType; } private static Object empty(Object o) { if (o == null || o.equals("") || o.equals("undefined")) { return ""; } return o; } }