a tiny c-template work-alike for java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

162 lines
4.5 KiB

  1. package com.binarythought.picotemplate;
  2. import java.util.Map;
  3. import java.util.Set;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.LinkedList;
  8. import java.util.Collections;
  9. /**
  10. * TemplateDictionary is used to assign variables and show sections
  11. * inside templates. It is functionally similar to a HashMap, although
  12. * not a decendent. <b>All methods that use identifying keys under this object
  13. * are case-insensitive.</b> This means that "MY_VAR" is the same as "my_var"
  14. * for both section names and variable names.
  15. * <p>At the most basic level, TemplateDictionary can be used with
  16. * Template to create simple fill-in-the-blank style templates.
  17. * <p>More advanced functionality is available, allowing developers
  18. * to toggle shown sections and loop over sections using child dictionaries.
  19. * @see Template
  20. */
  21. public class TemplateDictionary
  22. {
  23. private Map<String,String> dictionary;
  24. private Map<String,List<TemplateDictionary>> children;
  25. private Set<String> sections;
  26. private TemplateDictionary parent;
  27. /**
  28. * Instantiate an empty TemplateDictionary.
  29. */
  30. public TemplateDictionary(){ this(null); }
  31. private TemplateDictionary(TemplateDictionary parent)
  32. {
  33. this.parent = parent;
  34. dictionary = new HashMap<String,String>();
  35. children = new HashMap<String,List<TemplateDictionary>>();
  36. sections = new HashSet<String>();
  37. }
  38. /**
  39. * Get the parent dictionary to this one, or null if one does not exist.
  40. * @return This dictionary's parent dictionary.
  41. */
  42. public TemplateDictionary getParent(){ return parent; }
  43. /**
  44. * Show a specific section. This method is case-insensitive.
  45. * @param name The section to show.
  46. */
  47. public void show(String name){ sections.add(name.toUpperCase()); }
  48. /**
  49. * Hide a specific section. This method is case-insensitive.
  50. * @param name The section to hide.
  51. */
  52. public void hide(String name){ sections.remove(name.toUpperCase()); }
  53. /**
  54. * Checks if a section is hidden. This method is case-insensitive.
  55. * @param name The section name checking against.
  56. * @return True if a section is hidden, false if it is not.
  57. */
  58. public boolean isHidden(String name){ return !sections.contains(name.toUpperCase()); }
  59. /**
  60. * Checks if a section is shown. This method is case-insensitive.
  61. * @param name The section name checking against.
  62. * @return True if this section is shown, false if it is not.
  63. */
  64. public boolean isShown(String name){ return sections.contains(name.toUpperCase()); }
  65. /**
  66. * Provides a list of children identified by a case-insensitive section name.
  67. * @param name The section name checking against.
  68. * @return List of children dictionaries identified by specific section name or null if there are none.
  69. */
  70. public List<TemplateDictionary> getChild(String name){
  71. if(children.containsKey(name.toUpperCase())){
  72. return children.get(name.toUpperCase());
  73. } else { return null; }
  74. }
  75. /**
  76. * Remove all of the child dictionaries under this one identified by
  77. * a specific section name.
  78. * @param name Case-insensitive section name's children to remove.
  79. */
  80. public void removeChildren(String name){
  81. children.remove(name.toUpperCase());
  82. }
  83. /**
  84. * Create a child dictionary underneath this one for use with a specific
  85. * section.
  86. * @param name Case-insensitive section name for the new dictionary.
  87. * @return TemplateDictionary for specific section.
  88. */
  89. public TemplateDictionary createChild(String name){
  90. TemplateDictionary child = new TemplateDictionary(this);
  91. if(!children.containsKey(name.toUpperCase())){
  92. List<TemplateDictionary> list = new LinkedList<TemplateDictionary>();
  93. children.put(name.toUpperCase(), list);
  94. }
  95. children.get(name.toUpperCase()).add(child);
  96. return child;
  97. }
  98. /**
  99. * Gets the value of a variable by key. This method also ascends into parent
  100. * dictionaries looking for the key.
  101. * @param key Case-insensitive Key to look for.
  102. * @return Value of key as string, or an empty string if the key is not found.
  103. */
  104. public String get(String key)
  105. {
  106. if(dictionary.containsKey(key.toUpperCase())){
  107. return dictionary.get(key.toUpperCase());
  108. } else if(parent != null){
  109. return parent.get(key);
  110. } else {
  111. return "";
  112. }
  113. }
  114. /**
  115. * Set the value of a variable.
  116. * @param key Case-insensitive key that identifies this variable.
  117. * @param val Value of this variable.
  118. */
  119. public void put(String key, String val)
  120. {
  121. dictionary.put(key.toUpperCase(), val);
  122. }
  123. /**
  124. * Remove a variable.
  125. * @param key Case-insensitive key to remove.
  126. */
  127. public void remove(String key)
  128. {
  129. dictionary.remove(key.toUpperCase());
  130. }
  131. }