Browse Source

解析xml,返回json

sunyj 8 years ago
parent
commit
40dbb39fee

+ 24 - 1
kanban-console/pom.xml

@@ -16,7 +16,6 @@
 			<groupId>org.springframework.cloud</groupId>
 			<artifactId>spring-cloud-starter-config</artifactId>
 		</dependency>
-		<!-- spring boot -->
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-actuator</artifactId>
@@ -37,11 +36,35 @@
 			<groupId>org.mongodb</groupId>
 			<artifactId>mongo-java-driver</artifactId>
 		</dependency>
+		<dependency>
+			<groupId>me.chyxion</groupId>
+			<artifactId>newbie-jdbc</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.oracle</groupId>
+			<artifactId>ojdbc6</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.alibaba</groupId>
+			<artifactId>druid</artifactId>
+		</dependency>
 		<dependency>
 			<groupId>com.uas.kanban</groupId>
 			<artifactId>kanban-auth</artifactId>
 			<version>${kanban.auth.version}</version>
 		</dependency>
+		<dependency>
+			<groupId>dom4j</groupId>
+			<artifactId>dom4j</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>jaxen</groupId>
+			<artifactId>jaxen</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>de.odysseus.staxon</groupId>
+			<artifactId>staxon</artifactId>
+		</dependency>
 	</dependencies>
 
 	<build>

+ 10 - 0
kanban-console/src/main/java/com/uas/kanban/model/DataSource.java

@@ -2,6 +2,7 @@ package com.uas.kanban.model;
 
 import org.mongodb.morphia.annotations.Entity;
 
+import com.alibaba.druid.pool.DruidDataSource;
 import com.uas.kanban.annotation.FieldProperty;
 import com.uas.kanban.base.BaseEntity;
 
@@ -89,4 +90,13 @@ public class DataSource extends BaseEntity {
 				+ ", lastModified=" + lastModified + ", version=" + version + "]";
 	}
 
+	public DruidDataSource toDruidDataSource() {
+		DruidDataSource druidDataSource = new DruidDataSource();
+		druidDataSource.setDriverClassName(driverClassName);
+		druidDataSource.setUrl(url);
+		druidDataSource.setUsername(username);
+		druidDataSource.setPassword(password);
+		return druidDataSource;
+	}
+
 }

File diff suppressed because it is too large
+ 37 - 1
kanban-console/src/main/java/com/uas/kanban/service/impl/KanbanInstanceServiceImpl.java


+ 53 - 0
kanban-console/src/main/java/com/uas/kanban/support/FileHelper.java

@@ -0,0 +1,53 @@
+package com.uas.kanban.support;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import com.uas.kanban.util.StringUtils;
+
+public class FileHelper {
+
+	/**
+	 * 读取文件(绝对路径 > 用户路径config文件夹 > 类路径)
+	 * 
+	 * @param filePath
+	 *            文件路径
+	 * @return 文件输入流
+	 * @throws IOException
+	 */
+	public static InputStream readStream(String filePath) throws IOException {
+		if (StringUtils.isEmpty(filePath)) {
+			return null;
+		}
+		String userDir = System.getProperty("user.dir") + "/config";
+		File file = getFile(userDir, filePath);
+		if (file != null && file.exists()) {
+			return new FileInputStream(file);
+		}
+		// 用户路径下不存在,再在类路径下加载
+		return FileHelper.class.getResourceAsStream("/" + filePath);
+	}
+
+	/**
+	 * 获取文件(绝对路径 > 用户路径)
+	 * 
+	 * @param userDir
+	 *            用户路径
+	 * @param filePath
+	 *            文件路径
+	 * @return 文件
+	 * @throws IOException
+	 */
+	private static File getFile(String userDir, String filePath) throws IOException {
+		File file = new File(filePath);
+		// 如果是绝对路径
+		if (file.isAbsolute()) {
+			return file;
+		}
+
+		// 在用户路径下加载
+		return new File(userDir + "/" + filePath);
+	}
+}

+ 91 - 0
kanban-console/src/main/java/com/uas/kanban/support/TranslateHelper.java

@@ -0,0 +1,91 @@
+package com.uas.kanban.support;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.uas.kanban.annotation.NotEmpty;
+
+import de.odysseus.staxon.json.JsonXMLConfig;
+import de.odysseus.staxon.json.JsonXMLConfigBuilder;
+import de.odysseus.staxon.json.JsonXMLOutputFactory;
+
+/**
+ * 翻译转换
+ * 
+ * @author sunyj
+ * @since 2017年1月12日 下午6:07:07
+ */
+public class TranslateHelper {
+
+	private static Logger logger = LoggerFactory.getLogger(TranslateHelper.class);
+
+	/**
+	 * xml转为json
+	 * 
+	 * @param xml
+	 *            xml字符串
+	 * @return json字符串
+	 */
+	public static String xmlToJson(@NotEmpty("xml") String xml) {
+		StringReader input = new StringReader(xml);
+		StringWriter output = new StringWriter();
+		JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).autoPrimitive(true).prettyPrint(true).build();
+		try {
+			XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(input);
+			XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(output);
+			writer.add(reader);
+			reader.close();
+			writer.close();
+		} catch (Exception e) {
+			logger.error("", e);
+		} finally {
+			try {
+				output.close();
+				input.close();
+			} catch (IOException e) {
+				logger.error("", e);
+			}
+		}
+		return output.toString();
+	}
+
+	/**
+	 * 利用 xsl 映射 xml
+	 * 
+	 * @param sourceXml
+	 *            源xml
+	 * @param mapRuleStream
+	 *            映射处理文件输入流
+	 * @return 映射后的xml
+	 * @throws TransformerException
+	 * @throws IOException
+	 */
+	public static String map(@NotEmpty("sourceXml") String sourceXml,
+			@NotEmpty("mapRuleStream") InputStream mapRuleStream) throws TransformerException, IOException {
+		try {
+			TransformerFactory factory = TransformerFactory.newInstance();
+			Transformer transformer = factory.newTransformer(new StreamSource(mapRuleStream));
+			StringWriter writer = new StringWriter();
+			StreamResult result = new StreamResult(writer);
+			transformer.transform(new StreamSource(new StringReader(sourceXml)), result);
+			return writer.toString();
+		} finally {
+			mapRuleStream.close();
+		}
+	}
+
+}

+ 189 - 0
kanban-console/src/main/resources/map-rule.xsl

@@ -0,0 +1,189 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+	version="1.0">
+	<xsl:template match="content">
+		<content>
+
+			<!-- form -->
+			<xsl:for-each select="form">
+				<items>
+					<type>form</type>
+					<config>
+						<!-- TODO -->
+						<fieldStyle />
+						<!-- TODO -->
+						<valueStyle />
+						<columns>
+							<xsl:value-of select="@columns" />
+						</columns>
+						<xsl:for-each select="field">
+							<data>
+								<field>
+									<xsl:value-of select="@name" />
+								</field>
+								<value>
+									<xsl:value-of select="@value" />
+								</value>
+							</data>
+						</xsl:for-each>
+					</config>
+					<xsl:call-template name="layout" />
+				</items>
+			</xsl:for-each>
+
+			<!-- grid (table) -->
+			<xsl:for-each select="grid">
+				<items>
+					<type>table</type>
+					<config>
+						<title>
+							<xsl:value-of select="@title" />
+						</title>
+						<cls>
+							<xsl:value-of select="@allcls" />
+						</cls>
+						<render>
+							<xsl:value-of select="@allrender" />
+						</render>
+						<xsl:for-each select="field">
+							<columns>
+								<title>
+									<xsl:value-of select="@name" />
+								</title>
+								<width>
+									<xsl:value-of select="@width" />
+								</width>
+								<sort>
+									<xsl:value-of select="@sort" />
+								</sort>
+								<cls>
+									<xsl:value-of select="@cls" />
+								</cls>
+								<dataIndex>
+									<xsl:value-of select="@dataindex" />
+								</dataIndex>
+								<render>
+									<xsl:value-of select="@rendername" />
+								</render>
+							</columns>
+						</xsl:for-each>
+						<xsl:copy-of select="data" />
+					</config>
+					<xsl:call-template name="layout" />
+				</items>
+			</xsl:for-each>
+
+			<!-- bar -->
+			<xsl:for-each select="bar">
+				<xsl:call-template name="barOrLine">
+					<xsl:with-param name="type">
+						<!-- 不直接输出字符串,是因为有空格的问题 -->
+						<xsl:value-of select="concat('bar')" />
+					</xsl:with-param>
+				</xsl:call-template>
+			</xsl:for-each>
+
+			<!-- line -->
+			<xsl:for-each select="line">
+				<xsl:call-template name="barOrLine">
+					<xsl:with-param name="type">
+						<!-- 不直接输出字符串,是因为有空格的问题 -->
+						<xsl:value-of select="concat('line')" />
+					</xsl:with-param>
+				</xsl:call-template>
+			</xsl:for-each>
+
+			<!-- pie -->
+			<xsl:for-each select="pie">
+				<items>
+					<type>pie</type>
+					<config>
+						<title>
+							<xsl:value-of select="@maintitle" />
+						</title>
+						<subtitle>
+							<xsl:value-of select="@subtitle" />
+						</subtitle>
+						<xsl:for-each select="series">
+							<series>
+								<name>
+									<xsl:value-of select="@name" />
+								</name>
+								<data>
+									<xsl:value-of select="@data" />
+								</data>
+							</series>
+						</xsl:for-each>
+					</config>
+					<xsl:call-template name="layout" />
+				</items>
+			</xsl:for-each>
+		</content>
+	</xsl:template>
+
+	<!-- remove table's name in field -->
+	<xsl:template name="barOrLine">
+		<xsl:param name="type" />
+		<items>
+			<type>
+				<xsl:value-of select="$type" />
+			</type>
+			<config>
+				<title>
+					<xsl:value-of select="@maintitle" />
+				</title>
+				<subtitle>
+					<xsl:value-of select="@subtitle" />
+				</subtitle>
+				<xtitle>
+					<xsl:value-of select="@xtitle" />
+				</xtitle>
+				<xtype>
+					<xsl:value-of select="@xtype" />
+				</xtype>
+				<xfields>
+					<xsl:value-of select="@xfields" />
+				</xfields>
+				<ytitle>
+					<xsl:value-of select="@ytitle" />
+				</ytitle>
+				<ytype>
+					<xsl:value-of select="@ytype" />
+				</ytype>
+				<!-- TODO -->
+				<yfields />
+				<xsl:for-each select="series">
+					<series>
+						<name>
+							<xsl:value-of select="@name" />
+						</name>
+						<xsl:for-each select="data">
+							<data>
+								<xsl:value-of select="." />
+							</data>
+						</xsl:for-each>
+					</series>
+				</xsl:for-each>
+			</config>
+			<xsl:call-template name="layout" />
+		</items>
+	</xsl:template>
+
+	<!-- remove table's name in field -->
+	<xsl:template name="layout">
+		<layout>
+			<x>
+				<xsl:value-of select="@x" />
+			</x>
+			<y>
+				<xsl:value-of select="@y" />
+			</y>
+			<w>
+				<xsl:value-of select="@width" />
+			</w>
+			<h>
+				<xsl:value-of select="@height" />
+			</h>
+		</layout>
+	</xsl:template>
+</xsl:stylesheet>

+ 1 - 1
kanban-console/src/main/webapp/WEB-INF/views/console.html

@@ -21,7 +21,7 @@
 			<strong><li class="title">数据源</li></strong>
 			<ol>
 				<li><a target="_blank">datasource/save?json={"driverClassName": "driver_class_name","password": "password","url": "url","username": "username"}</a></li>
-				<li><a target="_blank">datasource/update?json={"driverClassName": "driver_class_name","code":"4EC2735D343","password": "password","url": "url","username": "username"}</a></li>
+				<li><a target="_blank">datasource/update?json={"driverClassName":"oracle.jdbc.driver.OracleDriver","code":"4EC3C69D011","password":"select!%23%25*(","url":"jdbc:oracle:thin:@192.168.253.6:1521:orcl","username":"UAS"}</a></li>
 				<li><a target="_blank">datasource/delete/all</a></li>
 				<li><a target="_blank">datasource/delete/4EC2735D343</a></li>
 				<li><a target="_blank">datasource/get/all</a></li>

+ 48 - 0
pom.xml

@@ -31,10 +31,17 @@
 		<servlet.version>3.1.0</servlet.version>
 		<mongo.java.version>3.5.0</mongo.java.version>
 		<morphia.version>1.3.2</morphia.version>
+		<newbie.version>0.0.1-RELEASE</newbie.version>
+		<oracle.jdbc.version>11.2.0</oracle.jdbc.version>
+		<druid.version>1.0.24</druid.version>
+		<dom4j.version>1.6.1</dom4j.version>
+		<jaxen.version>1.1.6</jaxen.version>
+		<staxon.version>1.3</staxon.version>
 	</properties>
 
 	<dependencyManagement>
 		<dependencies>
+			<!-- spring cloud -->
 			<dependency>
 				<groupId>org.springframework.cloud</groupId>
 				<artifactId>spring-cloud-starter-config</artifactId>
@@ -55,6 +62,8 @@
 				<artifactId>spring-web</artifactId>
 				<version>${spring.version}</version>
 			</dependency>
+
+			<!-- fastjson -->
 			<dependency>
 				<groupId>com.alibaba</groupId>
 				<artifactId>fastjson</artifactId>
@@ -70,6 +79,8 @@
 				<artifactId>javax.servlet-api</artifactId>
 				<version>${servlet.version}</version>
 			</dependency>
+
+			<!-- mongo -->
 			<dependency>
 				<groupId>org.mongodb</groupId>
 				<artifactId>mongo-java-driver</artifactId>
@@ -80,6 +91,43 @@
 				<artifactId>morphia</artifactId>
 				<version>${morphia.version}</version>
 			</dependency>
+
+			<!-- jdbc -->
+			<dependency>
+				<groupId>me.chyxion</groupId>
+				<artifactId>newbie-jdbc</artifactId>
+				<version>${newbie.version}</version>
+			</dependency>
+			<dependency>
+				<groupId>com.oracle</groupId>
+				<artifactId>ojdbc6</artifactId>
+				<version>${oracle.jdbc.version}</version>
+			</dependency>
+
+			<!-- druid -->
+			<dependency>
+				<groupId>com.alibaba</groupId>
+				<artifactId>druid</artifactId>
+				<version>${druid.version}</version>
+			</dependency>
+
+			<!-- xml操作 -->
+			<dependency>
+				<groupId>dom4j</groupId>
+				<artifactId>dom4j</artifactId>
+				<version>${dom4j.version}</version>
+			</dependency>
+			<dependency>
+				<groupId>jaxen</groupId>
+				<artifactId>jaxen</artifactId>
+				<version>${jaxen.version}</version>
+			</dependency>
+			<!-- json、xml转换 -->
+			<dependency>
+				<groupId>de.odysseus.staxon</groupId>
+				<artifactId>staxon</artifactId>
+				<version>${staxon.version}</version>
+			</dependency>
 		</dependencies>
 	</dependencyManagement>
 

Some files were not shown because too many files changed in this diff