View Javadoc
1   /*
2    * Copyright (C) 2002-2003,2017-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.phplib.templateengine.intern;
5   
6   
7   import java.util.Objects;
8   import java.util.regex.Pattern;
9   
10  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11  
12  
13  /**
14   * Template block manager.
15   */
16  public final class BlockManager
17   {
18    /* *
19     * Logger.
20     */
21    // private static final Logger LOGGER = LogManager.getLogger(BlockManager.class);
22  
23    /**
24     * Variable manager reference.
25     */
26    private final VariableManager variableManager;
27  
28    /* *
29     * Temporary blocks map.
30     */
31    // private final Map<String, String> blocks = new ConcurrentHashMap<>();
32  
33  
34    /**
35     * Copy constructor.
36     *
37     * @param vManager Variable manager
38     * @param bManager Block manager to copy from
39     * @throws NullPointerException If vManager or bManager is null
40     */
41    @SuppressFBWarnings("EI_EXPOSE_REP2")
42    public BlockManager(final VariableManager vManager, final BlockManager bManager)
43     {
44      super();
45      Objects.requireNonNull(vManager, "vManager"); //$NON-NLS-1$
46      Objects.requireNonNull(bManager, "bManager"); //$NON-NLS-1$
47      this.variableManager = vManager;
48      /*
49      for (final Map.Entry<String, String> entry : bManager.blocks.entrySet())
50       {
51        this.blocks.put(entry.getKey(), entry.getValue());
52       }
53      */
54     }
55  
56  
57    /**
58     * Constructor.
59     *
60     * @param vManager Variable manager
61     */
62    @SuppressFBWarnings("EI_EXPOSE_REP2")
63    public BlockManager(final VariableManager vManager)
64     {
65      super();
66      this.variableManager = vManager;
67     }
68  
69  
70    /**
71     * Set template block (cut it from parent template and replace it with a variable).
72     *
73     * Used for repeatable blocks
74     *
75     * @param parent Name of parent template variable
76     * @param varname Name of template block
77     * @param name Name of variable in which the block will be placed - if empty will be the same as varname
78     * @return true on sucess otherwise false
79     * @throws IllegalStateException When no block with varname is found.
80     * @throws NullPointerException If parent or varname is null
81     * @throws IllegalArgumentException If parent or varname is empty
82     */
83    @SuppressWarnings({"PMD.LinguisticNaming", "PMD.AvoidLiteralsInIfCondition"})
84    public boolean setBlock(final String parent, final String varname, final String name)
85     {
86      // asserts
87      String internName = name;
88      if ((internName == null) || "".equals(internName)) //$NON-NLS-1$
89       {
90        internName = varname;
91       }
92      final var pattern = Pattern.compile("<!--\\s+BEGIN " + varname + "\\s+-->(.*)<!--\\s+END " + varname + "\\s+-->", Pattern.DOTALL | Pattern.MULTILINE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
93      final var matcher = pattern.matcher(this.variableManager.getVar(parent));
94      final String str = matcher.replaceFirst("{" + internName + "}"); //$NON-NLS-1$ //$NON-NLS-2$
95      this.variableManager.setVar(varname, matcher.group(1));
96      this.variableManager.setVar(parent, str);
97      return true;
98     }
99  
100 
101   /**
102    * Returns the string representation of this BlockManager.
103    *
104    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
105    *
106    * "BlockManager[vars=[name, ...]]"
107    *
108    * @return String representation of this BlockManager.
109    * @see java.lang.Object#toString()
110    */
111   @Override
112   public String toString()
113    {
114     return new StringBuilder().append("BlockManager[vars=").append(this.variableManager.getVars()).append(']').toString(); //$NON-NLS-1$
115    }
116 
117 
118   /**
119    * Calculate hash code.
120    *
121    * @return Hash
122    * @see java.lang.Object#hashCode()
123    */
124   @Override
125   public int hashCode()
126    {
127     return Objects.hash(this.variableManager);
128    }
129 
130 
131   /**
132    * Is equal with another object.
133    *
134    * @param obj Object
135    * @return true when equal, false otherwise
136    * @see java.lang.Object#equals(java.lang.Object)
137    */
138   @Override
139   public boolean equals(final Object obj)
140    {
141     if (this == obj)
142      {
143       return true;
144      }
145     if (!(obj instanceof BlockManager))
146      {
147       return false;
148      }
149     final BlockManager other = (BlockManager)obj;
150     return this.variableManager.equals(other.variableManager);
151    }
152 
153  }