
org.rhq.plugins.apache.parser.ApacheDirective Maven / Gradle / Ivy
package org.rhq.plugins.apache.parser;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ApacheDirective implements Cloneable {
private String name;
private List values;
private boolean isNested;
private boolean isRootNode;
private boolean isComment;
private static final String WS = "[ \t]*";
private static final String WORD = "\"(?:[^\"\n]|\\\")*\"|'(?:[^'\n]|\\\')*'|[^'\" \t\n]+";
private static final String DIRECTIVE_REGEX = WS + "(" + WORD + ")" + WS;
private static final String COMMENT_REGEX="^[\t ]*#.*+$";
private boolean updated=false;
private static final Pattern DIRECTIVE_PATTERN = Pattern.compile(DIRECTIVE_REGEX);
private static final Pattern COMMENT_PATTERN = Pattern.compile(COMMENT_REGEX);
private List childNodes;
private ApacheDirective parentNode;
private String file;
public ApacheDirective(){
values = new ArrayList();
childNodes = new ArrayList();
}
public ApacheDirective(String directive){
values = new ArrayList();
childNodes = new ArrayList();
Matcher matcher = COMMENT_PATTERN.matcher(directive);
if (matcher.matches()){
isComment = true;
values.add(directive);
name = "#";
}else{
int startIndex = 0;
boolean updated = true;
while (updated && startIndex < directive.length()){
updated = false;
Matcher m = DIRECTIVE_PATTERN.matcher(directive);
while (m.find(startIndex)) {
for (int i = 1; i <= m.groupCount(); i++) {
String val = m.group(i);
values.add(val);
}
updated = true;
startIndex = m.end();
}
}
if (values.isEmpty())
throw new RuntimeException("Directive "+directive+"is not in valid format.");
String lastVal = values.get(values.size()-1);
if (lastVal.endsWith(">")){
lastVal = lastVal.substring(0,lastVal.length()-1);
values.set(values.size()-1, lastVal);
}
name = values.get(0);
values.remove(0);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getValues() {
return values;
}
public void setValues(List values) {
this.values = values;
}
public boolean isNested() {
return isNested;
}
public void setNested(boolean isNested) {
this.isNested = isNested;
}
public String getValuesAsString(){
StringBuilder buf = new StringBuilder();
for (String val : values){
buf.append(val);
}
return buf.toString();
}
public void addChildDirective(ApacheDirective directive){
childNodes.add(directive);
}
public List getChildDirectives(){
return childNodes;
}
public List getChildByName(String name){
List kids = new ArrayList();
for (ApacheDirective dir : childNodes){
if (dir.getName().equals(name)){
kids.add(dir);
}
}
return kids;
}
public ApacheDirective getParentNode(){
return parentNode;
}
public void setParentNode(ApacheDirective parent){
this.parentNode = parent;
}
public boolean isRootNode() {
return isRootNode;
}
public void setRootNode(boolean isRootNode) {
this.isRootNode = isRootNode;
}
public boolean isUpdated() {
return updated;
}
public void setUpdated(boolean updated) {
this.updated = updated;
}
public String getFile() {
if (isRootNode)
return null;
if (file == null){
List dir = parentNode.getChildByName(name);
for (int i=0;i");
return builder.toString();
}
public int getSeq(){
List directives = parentNode.getChildByName(name);
for (int i=0;i newChildNodes = new ArrayList(childNodes.size());
for(ApacheDirective child : childNodes) {
ApacheDirective childCopy = child.clone();
childCopy.parentNode = copy;
newChildNodes.add(childCopy);
}
copy.childNodes = newChildNodes;
return copy;
} catch (CloneNotSupportedException e) {
throw new IllegalStateException("ApacheDirective not cloneable even though it is declared as such.", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy