From 5d010f7aadd5ea313c447f8dbfe5c10c25f787ed Mon Sep 17 00:00:00 2001 From: lidong Date: Thu, 3 Nov 2016 09:41:40 +0800 Subject: [PATCH 1/6] 1.add writeTo(File) function in NgxConfig 2.add common-io dependency --- pom.xml | 6 +- .../common/formatter/EntryFormatter.java | 21 +++++ .../common/formatter/FormatterRepository.java | 92 +++++++++++++++++++ .../IndentSettableEntryFormatter.java | 55 +++++++++++ .../formatter/impl/DefaultEntryFormatter.java | 27 ++++++ .../formatter/impl/NgxBlockFormatter.java | 44 +++++++++ .../nginxparser/NgxAbstractEntry.java | 10 +- .../github/odiszapc/nginxparser/NgxBlock.java | 61 ++++++------ .../odiszapc/nginxparser/NgxConfig.java | 24 ++++- .../odiszapc/nginxparser/NgxEntryType.java | 3 +- 10 files changed, 305 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/github/odiszapc/common/formatter/EntryFormatter.java create mode 100644 src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java create mode 100644 src/main/java/com/github/odiszapc/common/formatter/IndentSettableEntryFormatter.java create mode 100644 src/main/java/com/github/odiszapc/common/formatter/impl/DefaultEntryFormatter.java create mode 100644 src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java diff --git a/pom.xml b/pom.xml index e02bdec..0630bff 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,11 @@ antlr4-runtime 4.5.3 - + + commons-io + commons-io + 2.5 + junit junit diff --git a/src/main/java/com/github/odiszapc/common/formatter/EntryFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/EntryFormatter.java new file mode 100644 index 0000000..53dc1a2 --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/EntryFormatter.java @@ -0,0 +1,21 @@ +package com.github.odiszapc.common.formatter; + +import com.github.odiszapc.nginxparser.NgxEntry; +import com.github.odiszapc.nginxparser.NgxEntryType; + +/** + * + *

+ * formatter for NgxEntry + *

+ * @author ${Author} + * @version ${Version} + * @since 8.1.0 + * + */ +public interface EntryFormatter{ + + StringBuffer formattedText(NgxEntry entry); + + NgxEntryType[] supportedEntryTypes(); +} diff --git a/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java b/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java new file mode 100644 index 0000000..59a131d --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java @@ -0,0 +1,92 @@ +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.nginxparser.NgxEntry; +import com.github.odiszapc.nginxparser.NgxEntryType; + +public class FormatterRepository { + + private static final EntryFormatter NULL_INDENT_SETTABLE_FORMATTER = new NullEntryFormatter(); + private Map innerFormatters; + private Map indentSettableFormatters; + + public FormatterRepository() { + initDefalutFormatters(); + } + + private void initDefalutFormatters() { + List entryFormatters = new ArrayList(); + entryFormatters.add(new DefaultEntryFormatter(this)); + entryFormatters.add(new NgxBlockFormatter(this)); + setFormatters(entryFormatters); + } + + public void setFormatters(Collection formatters) { + indentSettableFormatters = new HashMap(); + innerFormatters = new HashMap(); + 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 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 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[] {}; + } + + } + +} diff --git a/src/main/java/com/github/odiszapc/common/formatter/IndentSettableEntryFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/IndentSettableEntryFormatter.java new file mode 100644 index 0000000..8369002 --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/IndentSettableEntryFormatter.java @@ -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 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(); +} diff --git a/src/main/java/com/github/odiszapc/common/formatter/impl/DefaultEntryFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/impl/DefaultEntryFormatter.java new file mode 100644 index 0000000..7f56cc7 --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/impl/DefaultEntryFormatter.java @@ -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); + } + +} diff --git a/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java new file mode 100644 index 0000000..4bf6235 --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java @@ -0,0 +1,44 @@ +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); + 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()); + buf.append("}\n"); + return buf; + } + + @Override + public NgxEntryType[] supportedEntryTypes() { + return new NgxEntryType[] { NgxEntryType.BLOCK, NgxEntryType.CONFIG }; + } + + @Override + public IndentSettableEntryFormatter copy() { + return new NgxBlockFormatter(this); + } + +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java b/src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java index 968c1ae..a833fe0 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxAbstractEntry.java @@ -49,7 +49,11 @@ public String toString() { builder.append(value).append(" "); } String s = builder.toString(); - return s.substring(0, s.length()-1); + if (s.length() == 0) { + return s; + } else { + return s.substring(0, s.length() - 1); + } } public String getName() { @@ -66,7 +70,7 @@ public List getValues() { Iterator it = getTokens().iterator(); it.next(); - while(it.hasNext()) { + while (it.hasNext()) { values.add(it.next().toString()); } return values; @@ -78,7 +82,7 @@ public String getValue() { while (iterator.hasNext()) { builder.append(iterator.next()); if (iterator.hasNext()) { - builder.append(' '); + builder.append(' '); } } return builder.toString(); diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java b/src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java index 6a5fc07..8dfe478 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java @@ -40,7 +40,6 @@ public String toString() { return super.toString() + " {"; } - @Override public Iterator iterator() { return getEntries().iterator(); @@ -51,21 +50,21 @@ public void remove(NgxEntry itemToRemove) { throw new NullPointerException("Item can not be null"); Iterator it = entries.iterator(); - while(it.hasNext()) { + while (it.hasNext()) { NgxEntry entry = it.next(); switch (NgxEntryType.fromClass(entry.getClass())) { - case PARAM: - if (entry.equals(itemToRemove)) - it.remove(); - break; - case BLOCK: - if (entry.equals(itemToRemove)) - it.remove(); - else { - NgxBlock block = (NgxBlock) entry; - block.remove(itemToRemove); - } - break; + case PARAM: + if (entry.equals(itemToRemove)) + it.remove(); + break; + case BLOCK: + if (entry.equals(itemToRemove)) + it.remove(); + else { + NgxBlock block = (NgxBlock) entry; + block.remove(itemToRemove); + } + break; } } } @@ -116,25 +115,25 @@ public List findAll(Class clazz, List 0) { + if (block.getName().equals(head)) { + res.addAll(block.findAll(clazz, result, tail)); } - break; - - case BLOCK: - NgxBlock block = (NgxBlock) entry; - if (tail.length > 0) { - if (block.getName().equals(head)) { - res.addAll(block.findAll(clazz, result, tail)); - } - } else { - if (block.getName().equals(head) && (clazz.equals(NgxBlock.class))) { - res.add(block); - } + } else { + if (block.getName().equals(head) && (clazz.equals(NgxBlock.class))) { + res.add(block); } - break; + } + break; } } diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxConfig.java b/src/main/java/com/github/odiszapc/nginxparser/NgxConfig.java index d70e378..26498d1 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxConfig.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxConfig.java @@ -16,6 +16,9 @@ package com.github.odiszapc.nginxparser; +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.antlr.NginxLexer; import com.github.odiszapc.nginxparser.antlr.NginxListenerImpl; import com.github.odiszapc.nginxparser.antlr.NginxParser; @@ -25,10 +28,13 @@ import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTreeWalker; +import org.apache.commons.io.FileUtils; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Collection; /** @@ -41,6 +47,8 @@ public class NgxConfig extends NgxBlock { public static final Class BLOCK = NgxBlock.class; public static final Class IF = NgxIfBlock.class; + private FormatterRepository formatterRespository = new FormatterRepository(); + /** * Parse an existing config * @@ -69,7 +77,6 @@ public static NgxConfig readJavaCC(InputStream input) throws IOException, ParseE return parser.parse(); } - public static NgxConfig readAntlr(InputStream in) throws IOException { ANTLRInputStream input = new ANTLRInputStream(in); NginxLexer lexer = new NginxLexer(input); @@ -84,6 +91,10 @@ public static NgxConfig readAntlr(InputStream in) throws IOException { return listener.getResult(); } + public void setFormatterRepository(FormatterRepository formatterRespository) { + this.formatterRespository = formatterRespository; + } + @Override public Collection getTokens() { throw new IllegalStateException("Not implemented"); @@ -94,8 +105,17 @@ public void addValue(NgxToken token) { throw new IllegalStateException("Not implemented"); } + @Override public String toString() { - return "Nginx Config (" + getEntries().size() + " entries)"; + return "#Nginx Config (" + getEntries().size() + " entries)"; } + public void writeTo(File file) throws IOException { + EntryFormatter formatter = formatterRespository.getFormatter(NgxConfig.class); + if (formatter instanceof IndentSettableEntryFormatter) { + ((IndentSettableEntryFormatter) formatter).setIndent(0); + } + StringBuffer buffer = formatter.formattedText(this); + FileUtils.write(file, buffer, StandardCharsets.UTF_8); + } } diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxEntryType.java b/src/main/java/com/github/odiszapc/nginxparser/NgxEntryType.java index e62f6fb..b3bff86 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxEntryType.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxEntryType.java @@ -23,7 +23,8 @@ public enum NgxEntryType { PARAM(NgxParam.class), COMMENT(NgxComment.class), IF(NgxIfBlock.class), - BLOCK(NgxBlock.class); + BLOCK(NgxBlock.class), + CONFIG(NgxConfig.class); private final Class clazz; From c0395e524d5626d0a718b695a8925cf2fb3989c0 Mon Sep 17 00:00:00 2001 From: lidong Date: Fri, 4 Nov 2016 11:04:43 +0800 Subject: [PATCH 2/6] add NgxEntrys.parse(Object) to implement a object serialized to NgxEntry --- .../DefaultNgxEntrySerializer.java | 71 +++++++++++++++++++ .../nginxparser/NgxEntrySerializer.java | 6 ++ .../odiszapc/nginxparser/NgxEntrys.java | 35 +++++++++ .../odiszapc/nginxparser/NgxTokenValue.java | 13 ++++ .../odiszapc/nginxparser/NgxEntrysTest.java | 34 +++++++++ 5 files changed, 159 insertions(+) create mode 100644 src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java create mode 100644 src/main/java/com/github/odiszapc/nginxparser/NgxEntrySerializer.java create mode 100644 src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java create mode 100644 src/main/java/com/github/odiszapc/nginxparser/NgxTokenValue.java create mode 100644 src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java diff --git a/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java b/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java new file mode 100644 index 0000000..0053a90 --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java @@ -0,0 +1,71 @@ +package com.github.odiszapc.nginxparser; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; + +public class DefaultNgxEntrySerializer implements NgxEntrySerializer { + + private static final String UPPER_CHAR_REGEX = "(?=\\p{Upper})"; + + @Override + public NgxEntry serialize(String fieldName, Object obj) { + if (obj == null) { + throw new IllegalArgumentException("obj should not be null"); + } + Class clazz = obj.getClass(); + if (isPrimaryClass(clazz)) { + NgxParam param = new NgxParam(); + param.addValue(generateToken(fieldName)); + param.addValue(generateToken(obj.toString())); + return param; + } else { + NgxBlock block = new NgxBlock(); + block.addValue(generateToken(fieldName)); + for (Field field : clazz.getFields()) { + Annotation tokenAnnotation = field.getAnnotation(NgxTokenValue.class); + NgxEntry entry; + try { + if (tokenAnnotation != null) { + block.addValue(field.get(obj).toString()); + continue; + } + entry = serialize(field.getName(), field.get(obj)); + block.addEntry(entry); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + return block; + } + } + + private static boolean isPrimaryClass(Class clazz) { + if (CharSequence.class.isAssignableFrom(clazz)) { + return true; + } + if (clazz.isPrimitive()) { + return true; + } + return false; + } + + public static String generateToken(String token) { + // if (token.matches("\\W"))) { + String[] parts = token.split(UPPER_CHAR_REGEX); + StringBuffer buf = new StringBuffer(); + for (String part : parts) { + buf.append(part.toLowerCase()); + buf.append('_'); + } + if (buf.length() > 0) { + buf.deleteCharAt(buf.length() - 1); + } + return buf.toString(); + // } else { + // return token; + // } + } + +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxEntrySerializer.java b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrySerializer.java new file mode 100644 index 0000000..a8722fb --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrySerializer.java @@ -0,0 +1,6 @@ +package com.github.odiszapc.nginxparser; + +public interface NgxEntrySerializer { + + NgxEntry serialize(String fieldName, Object obj); +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java new file mode 100644 index 0000000..70048a2 --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java @@ -0,0 +1,35 @@ +package com.github.odiszapc.nginxparser; + +/** + * + *

+ * common util for NgxEntry + *

+ * @author ${Author} + * @version ${Version} + * + */ +public class NgxEntrys { + + private static final NgxEntrySerializer serializer = new DefaultNgxEntrySerializer(); + + /** + * + *

+ * parse a object to NgxEntry + *

+ * @param obj + * @return + */ + public static NgxEntry parse(Object obj) { + if (obj == null) { + throw new IllegalArgumentException("obj should not be null"); + } + if (obj instanceof NgxEntry) { + return (NgxEntry) obj; + } + Class clazz = obj.getClass(); + return serializer.serialize(clazz.getSimpleName(), obj); + } + +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxTokenValue.java b/src/main/java/com/github/odiszapc/nginxparser/NgxTokenValue.java new file mode 100644 index 0000000..c95a65f --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxTokenValue.java @@ -0,0 +1,13 @@ +package com.github.odiszapc.nginxparser; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface NgxTokenValue { + +} diff --git a/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java b/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java new file mode 100644 index 0000000..01c62c7 --- /dev/null +++ b/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java @@ -0,0 +1,34 @@ +package com.github.odiszapc.nginxparser; + +import java.util.Collection; + +import org.junit.Assert; +import org.junit.Test; + +public class NgxEntrysTest { + + @Test + public void testParse() { + Server testObj = new Server(); + testObj.realm = "*"; + testObj.listen = "80"; + testObj.serverName = "localhost"; + NgxEntry result = NgxEntrys.parse(testObj); + TestUtils.assertBlock(result, "server", testObj.realm); + NgxBlock resultBlock = (NgxBlock) result; + Collection resultEntrys = resultBlock.getEntries(); + Assert.assertEquals(resultEntrys.size(), 2); + for (NgxEntry entry : resultEntrys) { + Assert.assertTrue(entry instanceof NgxParam); + } + } + + static class Server { + + @NgxTokenValue + public String realm; + + public String listen; + public String serverName; + } +} From 242e300392de21eb5b2ff43e3b167d1a0e40d717 Mon Sep 17 00:00:00 2001 From: lidong Date: Fri, 4 Nov 2016 15:43:26 +0800 Subject: [PATCH 3/6] 1.add NgxConfigFormatter 2.add @NgxType to custom NgxEntry name while serialize --- .../common/formatter/FormatterRepository.java | 2 + .../formatter/impl/NgxBlockFormatter.java | 10 ++++- .../formatter/impl/NgxConfigFormatter.java | 40 +++++++++++++++++++ .../DefaultNgxEntrySerializer.java | 6 +-- .../odiszapc/nginxparser/NgxEntrys.java | 11 ++++- .../{ => annotation}/NgxTokenValue.java | 2 +- .../nginxparser/annotation/NgxType.java | 10 +++++ .../odiszapc/nginxparser/NgxEntrysTest.java | 2 + 8 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/github/odiszapc/common/formatter/impl/NgxConfigFormatter.java rename src/main/java/com/github/odiszapc/nginxparser/{ => annotation}/NgxTokenValue.java (83%) create mode 100644 src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java diff --git a/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java b/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java index 59a131d..c9bd265 100644 --- a/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java +++ b/src/main/java/com/github/odiszapc/common/formatter/FormatterRepository.java @@ -8,6 +8,7 @@ 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; @@ -25,6 +26,7 @@ private void initDefalutFormatters() { List entryFormatters = new ArrayList(); entryFormatters.add(new DefaultEntryFormatter(this)); entryFormatters.add(new NgxBlockFormatter(this)); + entryFormatters.add(new NgxConfigFormatter(this)); setFormatters(entryFormatters); } diff --git a/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java index 4bf6235..9b61b44 100644 --- a/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java +++ b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxBlockFormatter.java @@ -19,6 +19,13 @@ public NgxBlockFormatter(FormatterRepository 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; } @@ -27,13 +34,12 @@ public StringBuffer formattedText(NgxEntry entry) { buf.append(getFormatter(e.getClass()).formattedText(e)); } buf.append(indentText()); - buf.append("}\n"); return buf; } @Override public NgxEntryType[] supportedEntryTypes() { - return new NgxEntryType[] { NgxEntryType.BLOCK, NgxEntryType.CONFIG }; + return new NgxEntryType[] { NgxEntryType.BLOCK }; } @Override diff --git a/src/main/java/com/github/odiszapc/common/formatter/impl/NgxConfigFormatter.java b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxConfigFormatter.java new file mode 100644 index 0000000..bd0b54c --- /dev/null +++ b/src/main/java/com/github/odiszapc/common/formatter/impl/NgxConfigFormatter.java @@ -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 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 }; + } + +} diff --git a/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java b/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java index 0053a90..471068e 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java +++ b/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java @@ -3,6 +3,8 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Field; +import com.github.odiszapc.nginxparser.annotation.NgxTokenValue; + public class DefaultNgxEntrySerializer implements NgxEntrySerializer { private static final String UPPER_CHAR_REGEX = "(?=\\p{Upper})"; @@ -52,7 +54,6 @@ private static boolean isPrimaryClass(Class clazz) { } public static String generateToken(String token) { - // if (token.matches("\\W"))) { String[] parts = token.split(UPPER_CHAR_REGEX); StringBuffer buf = new StringBuffer(); for (String part : parts) { @@ -63,9 +64,6 @@ public static String generateToken(String token) { buf.deleteCharAt(buf.length() - 1); } return buf.toString(); - // } else { - // return token; - // } } } diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java index 70048a2..bef908f 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java @@ -1,5 +1,7 @@ package com.github.odiszapc.nginxparser; +import com.github.odiszapc.nginxparser.annotation.NgxType; + /** * *

@@ -29,7 +31,14 @@ public static NgxEntry parse(Object obj) { return (NgxEntry) obj; } Class clazz = obj.getClass(); - return serializer.serialize(clazz.getSimpleName(), obj); + String name; + NgxType ngxType = clazz.getAnnotation(NgxType.class); + if (ngxType != null) { + name = ngxType.name(); + } else { + name = clazz.getSimpleName(); + } + return serializer.serialize(name, obj); } } diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxTokenValue.java b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxTokenValue.java similarity index 83% rename from src/main/java/com/github/odiszapc/nginxparser/NgxTokenValue.java rename to src/main/java/com/github/odiszapc/nginxparser/annotation/NgxTokenValue.java index c95a65f..366b06f 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxTokenValue.java +++ b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxTokenValue.java @@ -1,4 +1,4 @@ -package com.github.odiszapc.nginxparser; +package com.github.odiszapc.nginxparser.annotation; import java.lang.annotation.ElementType; diff --git a/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java new file mode 100644 index 0000000..5b23602 --- /dev/null +++ b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java @@ -0,0 +1,10 @@ +package com.github.odiszapc.nginxparser.annotation; + +import java.lang.annotation.ElementType; + +import java.lang.annotation.Target; + +@Target({ ElementType.TYPE }) +public @interface NgxType { + String name() default "name"; +} diff --git a/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java b/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java index 01c62c7..65ce4ca 100644 --- a/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java +++ b/src/test/java/com/github/odiszapc/nginxparser/NgxEntrysTest.java @@ -5,6 +5,8 @@ import org.junit.Assert; import org.junit.Test; +import com.github.odiszapc.nginxparser.annotation.NgxTokenValue; + public class NgxEntrysTest { @Test From f19635b83d97e31284c042769f60f5f9f75e9e2f Mon Sep 17 00:00:00 2001 From: lidong Date: Tue, 8 Nov 2016 10:54:47 +0800 Subject: [PATCH 4/6] 1.move some serialize code from NgxEntrys to DefaultNgxEntrySerializer 2.fix NPE --- .../DefaultNgxEntrySerializer.java | 57 ++++++++++++++++--- .../odiszapc/nginxparser/NgxEntrys.java | 14 +---- .../nginxparser/annotation/NgxType.java | 4 +- 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java b/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java index 471068e..e996fe2 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java +++ b/src/main/java/com/github/odiszapc/nginxparser/DefaultNgxEntrySerializer.java @@ -3,7 +3,12 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Field; +import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.StringUtils; + import com.github.odiszapc.nginxparser.annotation.NgxTokenValue; +import com.github.odiszapc.nginxparser.annotation.NgxType; +import com.google.common.base.Defaults; public class DefaultNgxEntrySerializer implements NgxEntrySerializer { @@ -14,25 +19,38 @@ public NgxEntry serialize(String fieldName, Object obj) { if (obj == null) { throw new IllegalArgumentException("obj should not be null"); } + if (obj instanceof NgxEntry) { + return (NgxEntry) obj; + } Class clazz = obj.getClass(); + String realFiledName = getRealFieldName(fieldName,obj); + + // too complicated if (isPrimaryClass(clazz)) { NgxParam param = new NgxParam(); - param.addValue(generateToken(fieldName)); + param.addValue(generateToken(realFiledName)); param.addValue(generateToken(obj.toString())); return param; } else { NgxBlock block = new NgxBlock(); - block.addValue(generateToken(fieldName)); + block.addValue(generateToken(realFiledName)); for (Field field : clazz.getFields()) { - Annotation tokenAnnotation = field.getAnnotation(NgxTokenValue.class); NgxEntry entry; try { + Class fieldClazz = field.getType(); + Object fieldValue= field.get(obj); + if(isDefaultValue(fieldValue,fieldClazz)) { + continue; + } + Annotation tokenAnnotation = field.getAnnotation(NgxTokenValue.class); if (tokenAnnotation != null) { - block.addValue(field.get(obj).toString()); + block.addValue(fieldValue.toString()); continue; } - entry = serialize(field.getName(), field.get(obj)); - block.addEntry(entry); + entry = serialize(field.getName(), fieldValue); + if (entry != null) { + block.addEntry(entry); + } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { @@ -43,16 +61,41 @@ public NgxEntry serialize(String fieldName, Object obj) { } } + private static String getRealFieldName(String fieldName,Object obj){ + Class clazz = obj.getClass(); + NgxType ngxType = clazz.getAnnotation(NgxType.class); + if (ngxType != null) { + return ngxType.name(); + } else if(StringUtils.isNotEmpty(fieldName)){ + return fieldName; + }else{ + return getDefaultFieldName(obj); + } + } + + private static String getDefaultFieldName(Object obj){ + Class clazz = obj.getClass(); + return clazz.getSimpleName(); + } + private static boolean isPrimaryClass(Class clazz) { if (CharSequence.class.isAssignableFrom(clazz)) { return true; } - if (clazz.isPrimitive()) { + if (ClassUtils.isPrimitiveWrapper(clazz)) { return true; } return false; } + private static boolean isDefaultValue(Object value, Class clazz) { + if (value == null) { + return true; + } + Object defalutValue = Defaults.defaultValue(clazz); + return value.equals(defalutValue); + } + public static String generateToken(String token) { String[] parts = token.split(UPPER_CHAR_REGEX); StringBuffer buf = new StringBuffer(); diff --git a/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java index bef908f..2959718 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java +++ b/src/main/java/com/github/odiszapc/nginxparser/NgxEntrys.java @@ -1,6 +1,5 @@ package com.github.odiszapc.nginxparser; -import com.github.odiszapc.nginxparser.annotation.NgxType; /** * @@ -27,18 +26,7 @@ public static NgxEntry parse(Object obj) { if (obj == null) { throw new IllegalArgumentException("obj should not be null"); } - if (obj instanceof NgxEntry) { - return (NgxEntry) obj; - } - Class clazz = obj.getClass(); - String name; - NgxType ngxType = clazz.getAnnotation(NgxType.class); - if (ngxType != null) { - name = ngxType.name(); - } else { - name = clazz.getSimpleName(); - } - return serializer.serialize(name, obj); + return serializer.serialize(null, obj); } } diff --git a/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java index 5b23602..2907726 100644 --- a/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java +++ b/src/main/java/com/github/odiszapc/nginxparser/annotation/NgxType.java @@ -1,10 +1,12 @@ package com.github.odiszapc.nginxparser.annotation; import java.lang.annotation.ElementType; - +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) public @interface NgxType { String name() default "name"; } From 2e2f70478711a432506bf474b58aa8e30d286b60 Mon Sep 17 00:00:00 2001 From: lidong Date: Tue, 8 Nov 2016 10:55:15 +0800 Subject: [PATCH 5/6] add commons-lang3, guava lib --- pom.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pom.xml b/pom.xml index 0630bff..685260d 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,17 @@ commons-io 2.5 + + org.apache.commons + commons-lang3 + 3.5 + + + com.google.guava + guava + 20.0 + + junit junit From 927ebb1250ffab37573b13ededa1f062ffca2a68 Mon Sep 17 00:00:00 2001 From: lidong Date: Tue, 15 Nov 2016 16:23:12 +0800 Subject: [PATCH 6/6] update version to 0.9.6-1 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 685260d..f12e716 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ com.github.odiszapc nginxparser - 0.9.6 + 0.9.6-1 jar ${project.groupId}:${project.artifactId}