use of java.io.Writer in project j2objc by google.
the class ToStream method DTDprolog.
/**
* A private helper method to output the
* @throws SAXException
* @throws IOException
*/
private void DTDprolog() throws SAXException, IOException {
final java.io.Writer writer = m_writer;
if (m_needToOutputDocTypeDecl) {
outputDocTypeDecl(m_elemContext.m_elementName, false);
m_needToOutputDocTypeDecl = false;
}
if (m_inDoctype) {
writer.write(" [");
writer.write(m_lineSep, 0, m_lineSepLen);
m_inDoctype = false;
}
}
use of java.io.Writer in project j2objc by google.
the class ToStream method attributeDecl.
/**
* Report an attribute type declaration.
*
* <p>Only the effective (first) declaration for an attribute will
* be reported. The type will be one of the strings "CDATA",
* "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY",
* "ENTITIES", or "NOTATION", or a parenthesized token group with
* the separator "|" and all whitespace removed.</p>
*
* @param eName The name of the associated element.
* @param aName The name of the attribute.
* @param type A string representing the attribute type.
* @param valueDefault A string representing the attribute default
* ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if
* none of these applies.
* @param value A string representing the attribute's default value,
* or null if there is none.
* @exception SAXException The application may raise an exception.
*/
public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) throws SAXException {
// Do not inline external DTD
if (m_inExternalDTD)
return;
try {
final java.io.Writer writer = m_writer;
DTDprolog();
writer.write("<!ATTLIST ");
writer.write(eName);
writer.write(' ');
writer.write(aName);
writer.write(' ');
writer.write(type);
if (valueDefault != null) {
writer.write(' ');
writer.write(valueDefault);
}
//writer.write(" ");
//writer.write(value);
writer.write('>');
writer.write(m_lineSep, 0, m_lineSepLen);
} catch (IOException e) {
throw new SAXException(e);
}
}
use of java.io.Writer in project j2objc by google.
the class ToUnknownStream method initStreamOutput.
/**
* Initialize the wrapped output stream (XML or HTML).
* If the stream handler should be HTML, then replace the XML handler with
* an HTML handler. After than send the starting method calls that were cached
* to the wrapped handler.
*
*/
private void initStreamOutput() throws SAXException {
// Try to rule out if this is an not to be an HTML document based on prefix
boolean firstElementIsHTML = isFirstElemHTML();
if (firstElementIsHTML) {
// create an HTML output handler, and initialize it
// keep a reference to the old handler, ... it will soon be gone
SerializationHandler oldHandler = m_handler;
/* We have to make sure we get an output properties with the proper
* defaults for the HTML method. The easiest way to do this is to
* have the OutputProperties class do it.
*/
Properties htmlProperties = OutputPropertiesFactory.getDefaultMethodProperties(Method.HTML);
Serializer serializer = SerializerFactory.getSerializer(htmlProperties);
// The factory should be returning a ToStream
// Don't know what to do if it doesn't
// i.e. the user has over-ridden the content-handler property
// for html
m_handler = (SerializationHandler) serializer;
//m_handler = new ToHTMLStream();
Writer writer = oldHandler.getWriter();
if (null != writer)
m_handler.setWriter(writer);
else {
OutputStream os = oldHandler.getOutputStream();
if (null != os)
m_handler.setOutputStream(os);
}
// need to copy things from the old handler to the new one here
// if (_setVersion_called)
// {
m_handler.setVersion(oldHandler.getVersion());
// }
// if (_setDoctypeSystem_called)
// {
m_handler.setDoctypeSystem(oldHandler.getDoctypeSystem());
// }
// if (_setDoctypePublic_called)
// {
m_handler.setDoctypePublic(oldHandler.getDoctypePublic());
// }
// if (_setMediaType_called)
// {
m_handler.setMediaType(oldHandler.getMediaType());
// }
m_handler.setTransformer(oldHandler.getTransformer());
}
// Call startDocument() if necessary
if (m_needToCallStartDocument) {
m_handler.startDocument();
m_needToCallStartDocument = false;
}
// the wrapped handler is now fully initialized
m_wrapped_handler_not_initialized = false;
}
use of java.io.Writer in project error-prone by google.
the class BugPatternFileGenerator method processLine.
@Override
public boolean processLine(String line) throws IOException {
BugPatternInstance pattern = new Gson().fromJson(line, BugPatternInstance.class);
result.add(pattern);
// replace spaces in filename with underscores
Path checkPath = Paths.get(pattern.name.replace(' ', '_') + ".md");
try (Writer writer = Files.newBufferedWriter(outputDir.resolve(checkPath), UTF_8)) {
// load side-car explanation file, if it exists
Path sidecarExplanation = explanationDir.resolve(checkPath);
if (Files.exists(sidecarExplanation)) {
if (!pattern.explanation.isEmpty()) {
throw new AssertionError(String.format("%s specifies an explanation via @BugPattern and side-car", pattern.name));
}
pattern.explanation = new String(Files.readAllBytes(sidecarExplanation), UTF_8).trim();
}
// Construct an appropriate page for this {@code BugPattern}. Include altNames if
// there are any, and explain the correct way to suppress.
ImmutableMap.Builder<String, Object> templateData = ImmutableMap.<String, Object>builder().put("category", pattern.category).put("severity", pattern.severity).put("name", pattern.name).put("summary", pattern.summary.trim()).put("altNames", Joiner.on(", ").join(pattern.altNames)).put("explanation", pattern.explanation.trim());
if (baseUrl != null) {
templateData.put("baseUrl", baseUrl);
}
if (generateFrontMatter) {
Map<String, String> frontmatterData = ImmutableMap.<String, String>builder().put("title", pattern.name).put("summary", pattern.summary).put("layout", "bugpattern").put("category", pattern.category.toString()).put("severity", pattern.severity.toString()).build();
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
Writer yamlWriter = new StringWriter();
yamlWriter.write("---\n");
yaml.dump(frontmatterData, yamlWriter);
yamlWriter.write("---\n");
templateData.put("frontmatter", yamlWriter.toString());
}
if (pattern.documentSuppression) {
String suppression;
switch(pattern.suppressibility) {
case SUPPRESS_WARNINGS:
suppression = String.format("Suppress false positives by adding an `@SuppressWarnings(\"%s\")` " + "annotation to the enclosing element.", pattern.name);
break;
case CUSTOM_ANNOTATION:
if (pattern.customSuppressionAnnotations.length == 1) {
suppression = String.format("Suppress false positives by adding the custom suppression annotation " + "`@%s` to the enclosing element.", pattern.customSuppressionAnnotations[0]);
} else {
suppression = String.format("Suppress false positives by adding one of these custom suppression " + "annotations to the enclosing element: %s", COMMA_JOINER.join(Lists.transform(Arrays.asList(pattern.customSuppressionAnnotations), ANNOTATE_AND_CODIFY)));
}
break;
case UNSUPPRESSIBLE:
suppression = "This check may not be suppressed.";
break;
default:
throw new AssertionError(pattern.suppressibility);
}
templateData.put("suppression", suppression);
}
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile("com/google/errorprone/resources/bugpattern.mustache");
mustache.execute(writer, templateData.build());
if (pattern.generateExamplesFromTestCases) {
// Example filename must match example pattern.
List<Path> examplePaths = new ArrayList<>();
Filter<Path> filter = new ExampleFilter(pattern.className.substring(pattern.className.lastIndexOf('.') + 1));
findExamples(examplePaths, exampleDirBase, filter);
List<ExampleInfo> exampleInfos = FluentIterable.from(examplePaths).transform(new PathToExampleInfo(pattern.className)).toSortedList(new Comparator<ExampleInfo>() {
@Override
public int compare(ExampleInfo first, ExampleInfo second) {
return first.name().compareTo(second.name());
}
});
Collection<ExampleInfo> positiveExamples = Collections2.filter(exampleInfos, IS_POSITIVE);
Collection<ExampleInfo> negativeExamples = Collections2.filter(exampleInfos, not(IS_POSITIVE));
if (!exampleInfos.isEmpty()) {
writer.write("\n----------\n\n");
if (!positiveExamples.isEmpty()) {
writer.write("### Positive examples\n");
for (ExampleInfo positiveExample : positiveExamples) {
writeExample(positiveExample, writer);
}
}
if (!negativeExamples.isEmpty()) {
writer.write("### Negative examples\n");
for (ExampleInfo negativeExample : negativeExamples) {
writeExample(negativeExample, writer);
}
}
}
}
}
return true;
}
use of java.io.Writer in project error-prone by google.
the class DocGenTool method main.
public static void main(String[] args) throws IOException {
Options options = new Options();
new JCommander(options, args);
Path bugPatterns = Paths.get(options.bugPatterns);
if (!Files.exists(bugPatterns)) {
usage("Cannot find bugPatterns file: " + options.bugPatterns);
}
Path explanationDir = Paths.get(options.explanations);
if (!Files.exists(explanationDir)) {
usage("Cannot find explanations dir: " + options.explanations);
}
Path wikiDir = Paths.get(options.docsRepository);
Files.createDirectories(wikiDir);
Path exampleDirBase = Paths.get(options.examplesDir);
if (!Files.exists(exampleDirBase)) {
usage("Cannot find example directory: " + options.examplesDir);
}
Path bugpatternDir = wikiDir.resolve("bugpattern");
if (!Files.exists(bugpatternDir)) {
Files.createDirectories(bugpatternDir);
}
Files.createDirectories(wikiDir.resolve("_data"));
BugPatternFileGenerator generator = new BugPatternFileGenerator(bugpatternDir, exampleDirBase, explanationDir, options.target == Target.EXTERNAL, options.usePygments, options.baseUrl);
try (Writer w = Files.newBufferedWriter(wikiDir.resolve("bugpatterns.md"), StandardCharsets.UTF_8)) {
List<BugPatternInstance> patterns = readLines(bugPatterns.toFile(), UTF_8, generator);
new BugPatternIndexWriter().dump(patterns, w, options.target, enabledChecks());
}
}
Aggregations