/** * @param field * @param subFieldElements */ private void setSubFields(FieldObject field, List<Element> subFieldElements) { List<FieldObject> subFields = new ArrayList<FieldObject>(); fields.put(field.getType(), subFields); for (Element subElement : subFieldElements) { FieldObject subField = new FieldObject(); subField.setType(subElement.getAttributeValue("type")); subField.setClientType(subElement.getAttributeValue("clientType")); if (StringUtils.isEmpty(subField.getClientType())) { subField.setClientType(GeneratorHelper.getClientClassName(subField.getType()) + "Data"); } subField.setSmallName(subElement.getAttributeValue("name")); subField.setComment(subElement.getAttributeValue("comment")); if (subElement.getAttributeValue("list") != null) { subField.setList(subElement.getAttributeValue("list").equals("true") ? true : false); field.setHasListField(true); } subFields.add(subField); List<Element> subsubFieldElements = subElement.getChildren("field", NAME_SPACE); if (subsubFieldElements.size() > 0) { this.setSubFields(subField, subsubFieldElements); } // 如果不是系统定义的类型则说明是子消息 if (!new ArrayListWrapper(sysTypes).contains(subField.getType())) { subField.setIsNewType(true); if (subField.getType().indexOf("_") > 0) { subField.setType(GeneratorHelper.generateServerClassName(subField.getType())); } } } field.setSubFields(subFields); // 如果配置了子节点,而且类型中不存在包名,则说明是新定义的类型 if (field.getType().indexOf(".") == -1) { field.setNeedCreateType(true); } }
/** * 设置消息对象的字段 * * @param msgObj * @param msgElement */ private void setMsgObjFields( MessageObject msgObj, List fElements, boolean isClient, boolean isCppClient) { for (Iterator j = fElements.iterator(); j.hasNext(); ) { Element fElement = (Element) j.next(); FieldObject field = new FieldObject(); field.setType(fElement.getAttributeValue("type")); field.setClientType(fElement.getAttributeValue("clientType")); if (StringUtils.isEmpty(field.getClientType())) { field.setClientType(GeneratorHelper.getClientClassName(field.getType()) + "Data"); } field.setSmallName(fElement.getAttributeValue("name")); field.setComment(fElement.getAttributeValue("comment")); if (fElement.getAttribute("defaultValue") != null) { String defaultValue = fElement.getAttributeValue("defaultValue"); if (StringUtils.isEmpty(defaultValue)) { if (!TYPE_STRING.equals(field.getType())) { defaultValue = "0"; } else { defaultValue = ""; } } if (TYPE_STRING.equals(field.getType())) { defaultValue = "\"" + defaultValue + "\""; } field.setDefaultValue("=" + defaultValue); } else { field.setDefaultValue(""); } List<Element> subFieldElements = fElement.getChildren("field", NAME_SPACE); if (fElement.getAttributeValue("bytes") != null) { field.setBytes(fElement.getAttributeValue("bytes").equals("true") ? true : false); } if (fElement.getAttributeValue("list") != null) { field.setList(fElement.getAttributeValue("list").equals("true") ? true : false); msgObj.setHasListField(true); } boolean isSubMsg = "true".equalsIgnoreCase(fElement.getAttributeValue("subMsg")); field.setSubMsg(isSubMsg); if (isSubMsg) { field.setSubMsgType(GeneratorHelper.generateServerClassName(field.getType())); msgObj.setListMsg(true); if (isCppClient) { field.setList(true); msgObj.setHasListField(true); field.setType("_LZOBEX_" + field.getType()); } } // 如果不是系统定义的类型则说明是子消息 if (!new ArrayListWrapper(sysTypes).contains(field.getType())) { if (!isCppClient || !field.isSubMsg()) { field.setIsNewType(true); if (!isClient && field.getType().indexOf("_") > 0) { field.setType(GeneratorHelper.generateServerClassName(field.getType())); } } } if (subFieldElements.size() > 0) { this.setSubFields(field, subFieldElements); } else { if (isClient && field.getIsNewType()) { // 客户端生成消息的时候要为其补全 field.setSubFields(fields.get(field.getType())); } if (!isClient && field.getList() && field.getIsNewType()) { String type = field.getType(); field.setSubFields(msgs.get(type).getFields()); } } if (field.isSubMsg() && !isCppClient) { msgObj.addSubMsg(field); } else { msgObj.addField(field); } } }
/** * 生成客户端数据模型 * * @param messages */ private void createClientModel(List<MessageObject> messages, String module) { Map<String, FieldObject> newTypeFields = new HashMap<String, FieldObject>(); for (MessageObject msgObj : messages) { List<FieldObject> fields = msgObj.getFields(); for (FieldObject fieldObject : fields) { if (fieldObject.getIsNewType()) { // 先遍历它的子类型 for (FieldObject subfieldObject : fieldObject.getSubFields()) { if (subfieldObject.getIsNewType()) { // 先遍历子类型的子类型 for (FieldObject subsubfieldObject : subfieldObject.getSubFields()) { if (subsubfieldObject.getIsNewType()) { // 先遍历子类型的子类型 for (FieldObject sssfieldObject : subsubfieldObject.getSubFields()) { if (sssfieldObject.getIsNewType()) { sssfieldObject.setType(sssfieldObject.getClientType()); if (sssfieldObject.getClientType().contains(".")) { sssfieldObject.setType( GeneratorHelper.getClientClassName(sssfieldObject.getType())); continue; } newTypeFields.put(sssfieldObject.getClientType(), sssfieldObject); } } subsubfieldObject.setType(subsubfieldObject.getClientType()); if (subsubfieldObject.getClientType().contains(".")) { subsubfieldObject.setType( GeneratorHelper.getClientClassName(subsubfieldObject.getType())); continue; } newTypeFields.put(subsubfieldObject.getClientType(), subsubfieldObject); } } // subfieldObject.setType(subfieldObject.getClientType()); if (subfieldObject.getClientType().contains(".")) { subfieldObject.setType( GeneratorHelper.getClientClassName(subfieldObject.getType())); continue; } newTypeFields.put(subfieldObject.getClientType(), subfieldObject); } } fieldObject.setType(fieldObject.getClientType()); if (fieldObject.getClientType().contains(".")) { fieldObject.setType(GeneratorHelper.getClientClassName(fieldObject.getType())); continue; } newTypeFields.put(fieldObject.getClientType(), fieldObject); } } } for (FieldObject fieldObject : newTypeFields.values()) { VelocityContext context = new VelocityContext(); context.put("modulename", module); context.put("model", fieldObject); String outputFilePath; if (replaceDirectly) { outputFilePath = dataPath + fieldObject.getType() + ".as"; } else { outputFilePath = clientRootPath + File.separator + module + File.separator + "data" + File.separator + fieldObject.getType() + ".as"; } try { GeneratorHelper.generate(context, clientModelTemplate, outputFilePath); } catch (Exception e) { e.printStackTrace(); } } }