Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>com.github.odiszapc</groupId>
<artifactId>nginxparser</artifactId>
<version>0.9.6</version>
<version>0.9.6-1</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down Expand Up @@ -73,7 +73,22 @@
<artifactId>antlr4-runtime</artifactId>
<version>4.5.3</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.odiszapc.common.formatter;

import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxEntryType;

/**
*
* <p>
* formatter for NgxEntry
* </p>
* @author ${Author}
* @version ${Version}
* @since 8.1.0
*
*/
public interface EntryFormatter{

StringBuffer formattedText(NgxEntry entry);

NgxEntryType[] supportedEntryTypes();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.github.odiszapc.common.formatter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.odiszapc.common.formatter.impl.DefaultEntryFormatter;
import com.github.odiszapc.common.formatter.impl.NgxBlockFormatter;
import com.github.odiszapc.common.formatter.impl.NgxConfigFormatter;
import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxEntryType;

public class FormatterRepository {

private static final EntryFormatter NULL_INDENT_SETTABLE_FORMATTER = new NullEntryFormatter();
private Map<NgxEntryType, EntryFormatter> innerFormatters;
private Map<NgxEntryType, IndentSettableEntryFormatter> indentSettableFormatters;

public FormatterRepository() {
initDefalutFormatters();
}

private void initDefalutFormatters() {
List<EntryFormatter> entryFormatters = new ArrayList<EntryFormatter>();
entryFormatters.add(new DefaultEntryFormatter(this));
entryFormatters.add(new NgxBlockFormatter(this));
entryFormatters.add(new NgxConfigFormatter(this));
setFormatters(entryFormatters);
}

public void setFormatters(Collection<EntryFormatter> formatters) {
indentSettableFormatters = new HashMap<NgxEntryType, IndentSettableEntryFormatter>();
innerFormatters = new HashMap<NgxEntryType, EntryFormatter>();
for (EntryFormatter formatter : formatters) {
for (NgxEntryType entryType : formatter.supportedEntryTypes()) {
putFormatter(entryType, formatter);
}
}
}

public void putFormatter(NgxEntryType entryType, EntryFormatter formatter) {
innerFormatters.put(entryType, formatter);
if (formatter instanceof IndentSettableEntryFormatter) {
indentSettableFormatters.put(entryType, ((IndentSettableEntryFormatter) formatter).copy());
}
}

public EntryFormatter getFormatter(Class<? extends NgxEntry> formatterClazz) {
NgxEntryType entryType = NgxEntryType.fromClass(formatterClazz);
return getFormatter(entryType);
}

public EntryFormatter getFormatter(NgxEntryType entryType) {
if (entryType != null) {
EntryFormatter formatter = innerFormatters.get(entryType);
if (formatter != null) {
return formatter;
}
}
return NULL_INDENT_SETTABLE_FORMATTER;
}

public IndentSettableEntryFormatter getIndentSettableEntryFormatter(Class<? extends NgxEntry> formatterClazz) {
NgxEntryType entryType = NgxEntryType.fromClass(formatterClazz);
return getIndentSettableEntryFormatter(entryType);
}

public IndentSettableEntryFormatter getIndentSettableEntryFormatter(NgxEntryType entryType) {
if (entryType != null) {
IndentSettableEntryFormatter formatter = indentSettableFormatters.get(entryType);
if (formatter != null) {
return formatter.copy();
}
}
return null;
}

private static class NullEntryFormatter implements EntryFormatter {

@Override
public StringBuffer formattedText(NgxEntry entry) {
return new StringBuffer();
}

@Override
public NgxEntryType[] supportedEntryTypes() {
return new NgxEntryType[] {};
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.odiszapc.common.formatter;

import com.github.odiszapc.nginxparser.NgxEntry;

public abstract class IndentSettableEntryFormatter implements EntryFormatter {

public static final int DEFAULT_INDENT = 4;
private int indent = DEFAULT_INDENT;
private FormatterRepository repository;

public IndentSettableEntryFormatter(IndentSettableEntryFormatter formatter) {
this(formatter.repository);
this.indent = formatter.indent;
}

public IndentSettableEntryFormatter(FormatterRepository repository) {
this.repository = repository;
}

public void setIndent(int indent) {
this.indent = indent;
}

public int getIndent() {
return indent;
}

protected EntryFormatter getFormatter(Class<? extends NgxEntry> formatterClazz) {
IndentSettableEntryFormatter insFormatter = repository.getIndentSettableEntryFormatter(formatterClazz);
if (insFormatter != null) {
insFormatter.setIndent(indent + insFormatter.indent);
return insFormatter;
}
return repository.getFormatter(formatterClazz);
}

public StringBuffer indentText() {
StringBuffer indentBuf = new StringBuffer();
for (int i = 0; i < indent; i++) {
indentBuf.append(' ');
}
return indentBuf;
}

@Override
public StringBuffer formattedText(NgxEntry entry) {
StringBuffer buf = new StringBuffer();
buf.append(indentText());
buf.append(entry.toString());
buf.append('\n');
return buf;
}

public abstract IndentSettableEntryFormatter copy();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.odiszapc.common.formatter.impl;

import com.github.odiszapc.common.formatter.FormatterRepository;
import com.github.odiszapc.common.formatter.IndentSettableEntryFormatter;
import com.github.odiszapc.nginxparser.NgxEntryType;

public class DefaultEntryFormatter extends IndentSettableEntryFormatter {

public DefaultEntryFormatter(DefaultEntryFormatter defaultEntryFormatter) {
super(defaultEntryFormatter);
}

public DefaultEntryFormatter(FormatterRepository repository) {
super(repository);
}

@Override
public NgxEntryType[] supportedEntryTypes() {
return new NgxEntryType[] { NgxEntryType.PARAM, NgxEntryType.COMMENT };
}

@Override
public IndentSettableEntryFormatter copy() {
return new DefaultEntryFormatter(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.odiszapc.common.formatter.impl;

import com.github.odiszapc.common.formatter.FormatterRepository;
import com.github.odiszapc.common.formatter.IndentSettableEntryFormatter;
import com.github.odiszapc.nginxparser.NgxBlock;
import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxEntryType;

public class NgxBlockFormatter extends IndentSettableEntryFormatter {

public NgxBlockFormatter(NgxBlockFormatter ngxBlockFormatter) {
super(ngxBlockFormatter);
}

public NgxBlockFormatter(FormatterRepository repository) {
super(repository);
}

@Override
public StringBuffer formattedText(NgxEntry entry) {
StringBuffer buf = super.formattedText(entry);
buf.append(formattedBlockBody(entry));
buf.append("}\n");
return buf;
}

protected StringBuffer formattedBlockBody(NgxEntry entry) {
StringBuffer buf = new StringBuffer();
if (!(entry instanceof NgxBlock)) {
return buf;
}
NgxBlock block = (NgxBlock) entry;
for (NgxEntry e : block.getEntries()) {
buf.append(getFormatter(e.getClass()).formattedText(e));
}
buf.append(indentText());
return buf;
}

@Override
public NgxEntryType[] supportedEntryTypes() {
return new NgxEntryType[] { NgxEntryType.BLOCK };
}

@Override
public IndentSettableEntryFormatter copy() {
return new NgxBlockFormatter(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.odiszapc.common.formatter.impl;

import com.github.odiszapc.common.formatter.EntryFormatter;
import com.github.odiszapc.common.formatter.FormatterRepository;
import com.github.odiszapc.common.formatter.IndentSettableEntryFormatter;
import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxEntryType;

//base config do not contains indent
public class NgxConfigFormatter extends NgxBlockFormatter {

public NgxConfigFormatter(FormatterRepository repository) {
super(repository);
}

@Override
public int getIndent() {
return 0;
}

@Override
public StringBuffer formattedText(NgxEntry entry) {
return formattedBlockBody(entry);
}

@Override
protected EntryFormatter getFormatter(Class<? extends NgxEntry> formatterClazz) {
EntryFormatter formatter = super.getFormatter(formatterClazz);
if (formatter instanceof IndentSettableEntryFormatter) {
((IndentSettableEntryFormatter) formatter).setIndent(0);
}
return formatter;
}

@Override
public NgxEntryType[] supportedEntryTypes() {
return new NgxEntryType[] { NgxEntryType.CONFIG };
}

}
Loading