Created
November 13, 2012 22:00
-
-
Save andrewrlee/4068700 to your computer and use it in GitHub Desktop.
Json Publisher for Substeps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.technophobia.JsonPublisher; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.util.Comparator; | |
import java.util.List; | |
import com.google.common.collect.Ordering; | |
import com.google.common.collect.TreeMultimap; | |
import com.google.common.io.Files; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.technophobia.substeps.glossary.GlossaryPublisher; | |
import com.technophobia.substeps.glossary.StepDescriptor; | |
import com.technophobia.substeps.glossary.StepImplementationsDescriptor; | |
public class JsonSubstepsPublisher implements GlossaryPublisher { | |
/** | |
* @parameter default-value = stepimplementations.json | |
*/ | |
private File outputFile; | |
private Gson gson = new GsonBuilder().create(); | |
private Comparator<StepDescriptor> expressionComparator = new Comparator<StepDescriptor>() { | |
public int compare(final StepDescriptor s1, final StepDescriptor s2) { | |
return s1.getExpression().compareTo(s2.getExpression()); | |
} | |
}; | |
public void publish(final List<StepImplementationsDescriptor> stepimplementationDescriptors) { | |
TreeMultimap<String, StepDescriptor> sections = TreeMultimap.create(Ordering.natural(), expressionComparator); | |
for (final StepImplementationsDescriptor descriptor : stepimplementationDescriptors) { | |
for (final StepDescriptor step : descriptor.getExpressions()) { | |
sections.put(getSection(step), step); | |
} | |
} | |
writeToFile(gson.toJson(sections.asMap())); | |
} | |
private String getSection(StepDescriptor stepTag) { | |
boolean noTag = stepTag.getSection() == null || stepTag.getSection().isEmpty(); | |
return noTag ? "Miscellaneous" : stepTag.getSection(); | |
} | |
private void writeToFile(final String html) { | |
try { | |
outputFile.delete(); | |
if (outputFile.createNewFile()) { | |
Files.write(html, outputFile, Charset.defaultCharset()); | |
} else { | |
throw new IOException("Couldn't create file: " + outputFile.getAbsolutePath()); | |
} | |
} catch (final IOException e) { | |
throw new RuntimeException("Problem writing out file: " + outputFile.getAbsolutePath(), e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment