Gridarta Editor
FilterParser.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.filter;
21 
22 import java.util.Map.Entry;
25 import nu.xom.Element;
26 import nu.xom.Elements;
27 import nu.xom.ParentNode;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
36 public class FilterParser {
37 
41  @Nullable
42  private Element element;
43 
48  @NotNull
50 
51  @Override
52  public void visit(@NotNull final NamedFilterConfig filterConfig) {
53  final Element result = new Element("value");
54 
55  final Element enabled = new Element("enabled");
56  enabled.appendChild(Boolean.toString(filterConfig.isEnabled()));
57  result.appendChild(enabled);
58 
59  final Element inverted = new Element("inverted");
60  inverted.appendChild(Boolean.toString(filterConfig.isInverted()));
61  result.appendChild(inverted);
62 
63  for (final Entry<String, FilterConfig<?, ?>> entry : filterConfig.getEntries().entrySet()) {
64  final String key = entry.getKey();
65  final FilterConfig<?, ?> subFilterConfig = entry.getValue();
66  final Element filterValue = toXML(subFilterConfig);
67  final ParentNode subFilter = new Element("subfilter");
68  final Element fName = new Element("name");
69  fName.appendChild(key);
70  subFilter.appendChild(fName);
71  subFilter.appendChild(filterValue);
72  result.appendChild(subFilter);
73  }
74  element = result;
75  }
76 
77  @Override
78  public void visit(@NotNull final NamedGameObjectMatcherFilterConfig filterConfig) {
79  final Element value = new Element("value");
80 
81  final Element enabled = new Element("enabled");
82  enabled.appendChild(Boolean.toString(filterConfig.isEnabled()));
83  value.appendChild(enabled);
84 
85  for (final String key : filterConfig.getProperties()) {
86  final Element property = new Element("property");
87  property.appendChild(filterConfig.getProperty(key));
88  value.appendChild(property);
89  }
90  element = value;
91  }
92 
93  };
94 
99  @NotNull
101 
102  @Override
103  public void visit(@NotNull final NamedFilterConfig filterConfig) {
104  filterConfig.getFilter().resetConfig(filterConfig);
105  if (element == null) {
106  return;
107  }
108 
109  final Elements valueElements = element.getChildElements("value");
110  if (valueElements.size() == 0) {
111  return;
112  }
113  final Element value = valueElements.get(0);
114 
115  final boolean enabled = Boolean.valueOf(XmlUtils.getChild(value, "enabled").getValue().trim());
116  filterConfig.setEnabled(enabled);
117  filterConfig.setInverted(Boolean.valueOf(XmlUtils.getChild(value, "inverted").getValue().trim()));
118  for (final Element filterElement : new ElementsIterable(value.getChildElements("subfilter"))) {
119  final String name = XmlUtils.getChild(filterElement, "name").getValue().trim();
120  final FilterConfig<?, ?> subFilterConfig = filterConfig.getConfig(name);
121  fromXML(filterElement, subFilterConfig);
122  }
123  }
124 
125  @Override
126  public void visit(@NotNull final NamedGameObjectMatcherFilterConfig filterConfig) {
127  if (element == null) {
128  return;
129  }
130 
131  final Elements valueElements = element.getChildElements("value");
132  if (valueElements.size() == 0) {
133  return;
134  }
135  final Element value = valueElements.get(0);
136 
137  final boolean enabled = Boolean.valueOf(XmlUtils.getChild(value, "enabled").getValue().trim());
138  filterConfig.setEnabled(enabled);
139  for (final Element property : new ElementsIterable(value.getChildElements("property"))) {
140  final String pName = XmlUtils.getChild(property, "name").getValue().trim();
141  final String pValue = XmlUtils.getChild(property, "value").getValue().trim();
142  filterConfig.setProperty(pName, pValue);
143  }
144  }
145 
146  };
147 
153  @NotNull
154  public Element toXML(@NotNull final FilterConfig<?, ?> filterConfig) {
155  final Element prevElement = element;
156  try {
157  element = null;
158  filterConfig.accept(toXMLFilterConfigVisitor);
159  assert element != null;
160  return element;
161  } finally {
162  element = prevElement;
163  }
164  }
165 
171  public void fromXML(@NotNull final Element element, @NotNull final FilterConfig<?, ?> filterConfig) {
172  final Element prevElement = this.element;
173  try {
174  this.element = element;
175  filterConfig.accept(fromXMLFilterConfigVisitor);
176  } finally {
177  this.element = prevElement;
178  }
179  }
180 
181 }
XML related utility functions.
Definition: XmlUtils.java:30
void fromXML(@NotNull final Element element, @NotNull final FilterConfig<?, ?> filterConfig)
Import the filter configuration settings.
final FilterConfigVisitor fromXMLFilterConfigVisitor
The FilterConfigVisitor for converting FilterConfig instances from XML representation.
final FilterConfigVisitor toXMLFilterConfigVisitor
The FilterConfigVisitor for converting FilterConfig instances to XML representation.
Base package of all Gridarta classes.
Converts FilterConfig to and from XML representation.
Interface for visitors of filter configs.
static Element getChild(@NotNull final Element parentElement, @NotNull final String childName)
Returns a child Element of a parent element.
Definition: XmlUtils.java:46
Element element
The Element instance being converted.
Element toXML(@NotNull final FilterConfig<?, ?> filterConfig)
Export the filter configuration settings.
Filter configuration of NamedGameObjectMatcherFilter instances.