Files
SCM-Manager/scm-annotation-processor/src/main/java/sonia/scm/annotation/ClassSetElement.java

170 lines
4.5 KiB
Java
Raw Normal View History

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.annotation;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Strings;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
2014-03-28 20:38:36 +01:00
//~--- JDK imports ------------------------------------------------------------
import java.util.Map;
import java.util.Map.Entry;
/**
*
* @author Sebastian Sdorra
*/
public class ClassSetElement implements DescriptorElement
{
/** Field description */
private static final String EL_CLASS = "class";
/** Field description */
private static final String EL_DESCRIPTION = "description";
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param elementName
* @param classes
*/
2014-03-28 20:38:36 +01:00
public ClassSetElement(String elementName,
Iterable<ClassWithAttributes> classes)
{
this.elementName = elementName;
this.classes = classes;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param doc
* @param root
*/
@Override
public void append(Document doc, Element root)
{
2014-03-28 20:38:36 +01:00
for (ClassWithAttributes c : classes)
{
2014-03-28 20:38:36 +01:00
Element element = doc.createElement(elementName);
Element classEl = doc.createElement(EL_CLASS);
2014-03-28 20:38:36 +01:00
classEl.setTextContent(c.className);
if (!Strings.isNullOrEmpty(c.description))
{
Element descriptionEl = doc.createElement(EL_DESCRIPTION);
descriptionEl.setTextContent(c.description);
element.appendChild(descriptionEl);
}
2014-03-28 20:38:36 +01:00
for (Entry<String, String> e : c.attributes.entrySet())
{
Element attr = doc.createElement(e.getKey());
2014-03-28 20:38:36 +01:00
attr.setTextContent(e.getValue());
element.appendChild(attr);
}
if (c.requires != null) {
for (String requiresEntry : c.requires) {
Element requiresElement = doc.createElement("requires");
requiresElement.setTextContent(requiresEntry);
element.appendChild(requiresElement);
}
}
element.appendChild(classEl);
2014-03-28 20:38:36 +01:00
root.appendChild(element);
}
}
2014-03-28 20:38:36 +01:00
//~--- inner classes --------------------------------------------------------
/**
* Class description
*
*
* @version Enter version here..., 14/03/18
* @author Enter your name here...
*/
public static class ClassWithAttributes
{
/**
* Constructs ...
*
* @param className
* @param description
* @param requires
2014-03-28 20:38:36 +01:00
* @param attributes
*/
public ClassWithAttributes(String className, String description,
String[] requires, Map<String, String> attributes)
2014-03-28 20:38:36 +01:00
{
this.className = className;
this.description = description;
this.requires = requires;
2014-03-28 20:38:36 +01:00
this.attributes = attributes;
}
//~--- fields -------------------------------------------------------------
private final String[] requires;
2014-03-28 20:38:36 +01:00
/** Field description */
private final Map<String, String> attributes;
/** Field description */
private final String className;
/** Field description */
private final String description;
2014-03-28 20:38:36 +01:00
}
//~--- fields ---------------------------------------------------------------
/** Field description */
2014-03-28 20:38:36 +01:00
private final Iterable<ClassWithAttributes> classes;
/** Field description */
private final String elementName;
}