Skip to content

Commit

Permalink
First cut
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Henderson committed Apr 2, 2014
1 parent e634767 commit a3cdcc2
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="main/java/|test/java/" kind="src" path="src"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>bs3-eclipse-snippets</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
13 changes: 13 additions & 0 deletions eclipse-snippets/snippets-skeleton.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

<snippets>
<category filters="*" id="category_1396470350151" initial_state="0" label="Bootstrap 3" largeicon="" smallicon="">
<description><![CDATA[A set of snippets of various Bootstrap 3 items]]></description>
<!-- <item category="category_1396470350151" id="item_1396470503758" label="Sample">
<description><![CDATA[Sample Description]]></description>
<content><![CDATA[<div class="sample">${param1}</div>]]></content>
<variable default="Default Value" id="name_1" name="param1">
<description><![CDATA[param1 Description]]></description>
</variable>
</item> -->
</category>
</snippets>
13 changes: 13 additions & 0 deletions output-snippets.xml

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bs3-eclipse-snippets</groupId>
<artifactId>bs3-eclipse-snippets</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.4</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package com.azudio.bootstrap3snippetgenerator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Bootstrap3SnippetGenerator {

private Document d, s;
private String categoryId;
private Long categoryNumber;
private Long itemNumber;
private Integer variableNameNumber;

public static void main(String[] args) throws DocumentException, IOException {
Bootstrap3SnippetGenerator g = new Bootstrap3SnippetGenerator();

g.start();
}

private void start() throws DocumentException, IOException {

init();
findFiles();

System.out.println(d.asXML());

write(d);

}

Element cat;

private void init() throws MalformedURLException, DocumentException {
d = parse(new URL("file:eclipse-snippets/snippets-skeleton.xml"));

cat = (Element) d.selectSingleNode("//category");

setCategoryId(cat.attribute("id").getValue());

categoryNumber = Long.parseLong(getCategoryId().substring(getCategoryId().indexOf("_") + 1, getCategoryId().length()));
setItemNumber(categoryNumber);

}

private void findFiles() throws DocumentException {
String rootPath = ".";
File[] domains = new File(rootPath).listFiles();
for (File domain : domains) {
if (domain.isDirectory()) {
File[] files = new File(domain.getAbsolutePath()).listFiles();
for (File file : files) {
if (file.isFile() && file.getAbsolutePath().endsWith(".sublime-snippet")) {
System.out.println(file.getAbsolutePath());
try {
String simple = file.getName().replaceAll("\\..*", "");
System.out.println(simple);
System.out.println(file.toURI().toURL().toString());

s = parse(file.toURI().toURL());

Element content = (Element) s.selectSingleNode("//content");

cat.add(createItem(simple, simple + " Description", content.getTextTrim()));

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}

private Element createItem(String label, String description, String content) {
Element e = DocumentHelper.createElement("item");
e.addAttribute("category", getCategoryId());
e.addAttribute("id", "item_" + ++itemNumber);
e.addAttribute("label", label);

e.addElement("description").addCDATA(description);

for (Variable v : parseVariables(content)) {
Element variableElement = e.addElement("variable");
variableElement.addAttribute("default", v.getDefaultValue());
variableElement.addAttribute("id", v.getName());
variableElement.addAttribute("name", v.getName());
variableElement.addElement("description").addCDATA(v.getDescription());
}

e.addElement("content").addCDATA(swapVariableNames(content));

System.out.println(e.asXML());

return e;
}

private String swapVariableNames(String content) {
Pattern p = Pattern.compile("(\\$\\{(\\d):(.*?)\\})");
Matcher m = p.matcher(content);

variableNameNumber = 1;

while (m.find()) {
content = content.replace(m.group(0), "${name_" + variableNameNumber + "}");
variableNameNumber++;
}

System.out.println(content);

return content;
}

private List<Variable> parseVariables(String content) {
List<Variable> l = new ArrayList<Variable>();

Pattern p = Pattern.compile("(\\$\\{(\\d):(.*?)\\})");
Matcher m = p.matcher(content);

variableNameNumber = 1;

while (m.find()) {
// System.out.println("0" + m.group(0));
// System.out.println("1" + m.group(1));
System.out.println("Var Num: " + variableNameNumber);
System.out.println("Param No: " + m.group(2));
System.out.println("Default Value: " + m.group(3));
l.add(new Variable("name_" + variableNameNumber, "", m.group(3)));

variableNameNumber++;
}

System.out.println(content);

return l;
}

private Document parse(URL url) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(url);

return document;
}

private Document styleDocument(Document document, String stylesheet) throws Exception {

// load the transformer using JAXP
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));

// now lets style the given document
DocumentSource source = new DocumentSource(document);
DocumentResult result = new DocumentResult();
transformer.transform(source, result);

// return the transformed document
Document transformedDoc = result.getDocument();
return transformedDoc;
}

public String getCategoryId() {
return categoryId;
}

public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}

public Long getItemNumber() {
return itemNumber;
}

public void setItemNumber(Long itemNumber) {
this.itemNumber = itemNumber;
}

public void write(Document document) throws IOException {

// lets write to a file
XMLWriter writer = new XMLWriter(new FileWriter("output-snippets.xml"));
writer.write(document);
writer.close();

// Pretty print the document to System.out
OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter(System.out, format);
writer.write(document);

// Compact format to System.out
format = OutputFormat.createCompactFormat();
writer = new XMLWriter(System.out, format);
writer.write(document);
}
}
81 changes: 81 additions & 0 deletions src/com/azudio/bootstrap3snippetgenerator/Variable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.azudio.bootstrap3snippetgenerator;

public class Variable {

private String defaultValue;
private String name;
private String description;

/**
* @param name
*/
public Variable(String name) {
this(name, "");
}

/**
* @param defaultValue
* @param name
* @param description
*/
public Variable(String name, String description) {
this(name, description, null);
}

/**
* @param defaultValue
* @param name
* @param description
*/
public Variable(String name, String description, String defaultValue) {
this.name = name;
this.description = description;
this.defaultValue = defaultValue;
}

/**
* @return the defaultValue
*/
public String getDefaultValue() {
return defaultValue;
}

/**
* @param defaultValue
* the defaultValue to set
*/
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the description
*/
public String getDescription() {
return description;
}

/**
* @param description
* the description to set
*/
public void setDescription(String description) {
this.description = description;
}

}
Loading

0 comments on commit a3cdcc2

Please sign in to comment.