Author: DartPeng
Date: 2008-09-25 06:06:18 -0400 (Thu, 25 Sep 2008)
New Revision: 10474
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/libs/freemarker-2.3.8.jar
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/DOM2FreeMarkerTransformor.java
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/FreeMarkerContentReplacer.java
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/utils/
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/utils/XMLConstants.java
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/.classpath
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/META-INF/MANIFEST.MF
Log:
create two class for DOM/FreeMarker transformation
Modified: trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/.classpath
===================================================================
--- trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/.classpath 2008-09-25 10:04:24
UTC (rev 10473)
+++ trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/.classpath 2008-09-25 10:06:18
UTC (rev 10474)
@@ -1,8 +1,9 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins"/>
- <classpathentry kind="lib" path="libs/dom4j-1.6.1.jar"/>
- <classpathentry kind="output" path="bin"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="lib" path="libs/dom4j-1.6.1.jar"/>
+ <classpathentry kind="lib" path="libs/freemarker-2.3.8.jar"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
Modified: trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/META-INF/MANIFEST.MF
===================================================================
--- trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/META-INF/MANIFEST.MF 2008-09-25
10:04:24 UTC (rev 10473)
+++ trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/META-INF/MANIFEST.MF 2008-09-25
10:06:18 UTC (rev 10474)
@@ -16,3 +16,6 @@
Eclipse-LazyStart: true
Bundle-ClassPath: libs/dom4j-1.6.1.jar,
.
+Export-Package: org.jboss.tools.smooks.java2xml,
+ org.jboss.tools.smooks.java2xml.analyzer,
+ org.jboss.tools.smooks.java2xml.utils
Added: trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/libs/freemarker-2.3.8.jar
===================================================================
(Binary files differ)
Property changes on:
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/libs/freemarker-2.3.8.jar
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:mime-type
+ application/octet-stream
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/DOM2FreeMarkerTransformor.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/DOM2FreeMarkerTransformor.java
(rev 0)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/DOM2FreeMarkerTransformor.java 2008-09-25
10:06:18 UTC (rev 10474)
@@ -0,0 +1,91 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.smooks.java2xml.analyzer;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.dom4j.Attribute;
+import org.dom4j.Document;
+import org.dom4j.Element;
+import org.jboss.tools.smooks.java2xml.utils.XMLConstants;
+
+/**
+ * @author Dart Peng
+ * @Date : Sep 25, 2008
+ */
+public class DOM2FreeMarkerTransformor {
+
+ public String transformDOM(Document document) {
+ Element element = document.getRootElement();
+ StringBuffer buffer = new StringBuffer();
+ transform(element, buffer);
+ return buffer.toString();
+ }
+
+ protected void transform(Element element, StringBuffer buffer) {
+ String name = element.getName();
+ if (XMLConstants.REPLACE_FREEMARKER_FOR_EXPRESS_ELEMENT_NAME
+ .equals(name)) {
+ String desc = element
+ .attributeValue(XMLConstants.REPLACE_FREEMARKER_FOR_EXPRESS_ELEMENT_ATTRIBUTE);
+ name = "#" + desc;
+ }
+ String attributesString = "";
+ if(!isFreeMarkerExpression(name)){
+ attributesString = buildAttributeString(element);
+ }
+ List children = element.elements();
+ if (children.isEmpty()) {
+ buffer.append("<" + name + " " +attributesString +
"/>");
+ return;
+ } else {
+ buffer.append("<" + name + " " +attributesString +
"/>");
+ for (Iterator iterator = children.iterator(); iterator.hasNext();) {
+ Element child = (Element) iterator.next();
+ transform(child, buffer);
+ }
+ if(isFreeMarkerExpression(name)){
+ name = name.trim();
+ int index = name.indexOf(" ");
+ if(index != -1){
+ name = name.substring(0,index);
+ }
+ }
+ buffer.append("</" + name + ">");
+ }
+ }
+
+ private String buildAttributeString(Element element) {
+ List list = element.attributes();
+ StringBuffer buffer = new StringBuffer("");
+ for (Iterator iterator = list.iterator(); iterator.hasNext();) {
+ Attribute att = (Attribute) iterator.next();
+ String name = att.getName();
+ String value = att.getValue();
+ if(value == null) value = "";
+ if(name != null){
+ buffer.append(name);
+ buffer.append("=");
+ buffer.append("\"");
+ buffer.append(value);
+ buffer.append("\"");
+ buffer.append(" ");
+ }
+ }
+ return buffer.toString();
+ }
+
+ private boolean isFreeMarkerExpression(String str){
+ return str.startsWith("#");
+ }
+
+}
Property changes on:
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/DOM2FreeMarkerTransformor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/FreeMarkerContentReplacer.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/FreeMarkerContentReplacer.java
(rev 0)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/FreeMarkerContentReplacer.java 2008-09-25
10:06:18 UTC (rev 10474)
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.smooks.java2xml.analyzer;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Enumeration;
+
+import org.jboss.tools.smooks.java2xml.utils.XMLConstants;
+
+import freemarker.core.TemplateElement;
+import freemarker.core.TextBlock;
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+
+/**
+ * @author Dart Peng
+ * @Date : Sep 25, 2008
+ */
+public class FreeMarkerContentReplacer {
+
+ public String replaceFreeMarkerTemplate(Reader reader) throws IOException {
+
+ Configuration fmConfiguration = new Configuration();
+ fmConfiguration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
+ StringBuffer buffer = new StringBuffer();
+ Template template;
+ template = new Template("XML2Java_FreeMarker", reader, fmConfiguration);
+ TemplateElement root = template.getRootTreeNode();
+ visitTemplateElement(root, buffer);
+ return buffer.toString();
+ }
+
+ public String replaceFreeMarkerTemplate(String templateContents)
+ throws IOException {
+ Reader reader = new StringReader(templateContents);
+ return replaceFreeMarkerTemplate(reader);
+ }
+
+ private void visitTemplateElement(TemplateElement model, StringBuffer buffer) {
+
+ Enumeration children = ((TemplateElement) model).children();
+ if (!"MixedContent".equals(model.getNodeName())) {
+ buffer
+ .append("<"
+ + XMLConstants.REPLACE_FREEMARKER_FOR_EXPRESS_ELEMENT_NAME
+ + " "
+ + XMLConstants.REPLACE_FREEMARKER_FOR_EXPRESS_ELEMENT_ATTRIBUTE
+ + "= \"" + model.getDescription() + "\">");
+ }
+ while (children.hasMoreElements()) {
+ Object obj = children.nextElement();
+ if (!(obj instanceof TextBlock)) {
+ TemplateElement child = (TemplateElement) obj;
+ String str = child.getSource();
+ if (child.isLeaf()) {
+ if (child.getNodeName().equals("DollarVariable")) {
+ buffer.append(child.getSource());
+ } else {
+ buffer.append(child.getDescription());
+ }
+ continue;
+ }
+ visitTemplateElement(child, buffer);
+ } else {
+ buffer.append(obj);
+ }
+ }
+ if (!"MixedContent".equals(model.getNodeName())) {
+ buffer.append("</"
+ + XMLConstants.REPLACE_FREEMARKER_FOR_EXPRESS_ELEMENT_NAME
+ + ">");
+ }
+ }
+}
Property changes on:
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/analyzer/FreeMarkerContentReplacer.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/utils/XMLConstants.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/utils/XMLConstants.java
(rev 0)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/utils/XMLConstants.java 2008-09-25
10:06:18 UTC (rev 10474)
@@ -0,0 +1,22 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.smooks.java2xml.utils;
+
+/**
+ * @author Dart Peng
+ * @Date : Sep 25, 2008
+ */
+public class XMLConstants {
+ public static final String REPLACE_FREEMARKER_FOR_EXPRESS_ELEMENT_NAME =
"adfafdafafda_dfafda";
+
+ public static final String REPLACE_FREEMARKER_FOR_EXPRESS_ELEMENT_ATTRIBUTE =
"desc";
+
+}
Property changes on:
trunk/smooks/plugins/org.jboss.tools.smooks.java2xml/src/org/jboss/tools/smooks/java2xml/utils/XMLConstants.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain