All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.code.mojo.license.header.AdditionalHeaderDefinition Maven / Gradle / Ivy

/**
 * Copyright (C) 2008 http://code.google.com/p/maven-license-plugin/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.code.mojo.license.header;

import com.mycila.xmltool.CallBack;
import com.mycila.xmltool.XMLTag;

import java.util.HashMap;
import java.util.Map;

/**
 * The class AdditionalHeaderDefinition is used to collect header definitions declared in an "external" XML
 * document configuration.
 * 

* The XML document must respect the following XML schema: *

 <?xml version="1.0" encoding="UTF-8"?>
 * <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
 *          xmlns:xs="http://www.w3.org/2001/XMLSchema">
 *   <xs:element name="additionalHeaders" type="additionalHeadersType"/>
 *   <xs:complexType name="additionalHeadersType">
 *       <xs:sequence>
 *           <xs:annotation>
 *               <xs:documentation>
 *               Replace typeName tag name with the name of the type of header definition you are defining.
 *               </xs:documentation>
 *           </xs:annotation>
 *           <xs:element type="textType" name="typeName"/>
 *       </xs:sequence>
 *   </xs:complexType>
 *   <xs:complexType name="textType">
 *       <xs:sequence>
 *          <xs:annotation>
 *              <xs:documentation>
 *               Do not forget that you can use CDATA format as element value.
 *              </xs:documentation>
 *           </xs:annotation>
 *           <xs:element type="xs:string" name="firstLine" minOccurs="1" maxOccurs="1"/>
 *           <xs:element type="xs:string" name="beforeEachLine" minOccurs="1" maxOccurs="1"/>
 *           <xs:element type="xs:string" name="endLine" minOccurs="1" maxOccurs="1"/>
 *           <xs:element type="xs:string" name="skipLine" minOccurs="0" maxOccurs="1"/>
 *           <xs:element type="xs:string" name="firstLineDetectionPattern" minOccurs="1" maxOccurs="1"/>
 *           <xs:element type="xs:string" name="lastLineDetectionPattern" minOccurs="1" maxOccurs="1"/>
 *       </xs:sequence>
 *   </xs:complexType>
 *  </xs:schema>
 * 
* * * @author Cedric Pronzato */ public final class AdditionalHeaderDefinition { private final Map definitions = new HashMap(); /** * Construct an AdditionalHeaderDefinition object using the given XML document as header definitions * input. * * @param doc The XML definition to read. */ public AdditionalHeaderDefinition(XMLTag doc) { if (doc == null) { throw new IllegalArgumentException("The header definition XML document cannot be null"); } doc.gotoRoot().forEachChild(new CallBack() { public void execute(XMLTag doc) { final String type = doc.getCurrentTagName().toLowerCase(); HeaderDefinition definition = definitions.get(type); if (definition == null) { definition = new HeaderDefinition(type); definitions.put(type, definition); } doc.forEachChild(new FeedProperty(definition)); definition.validate(); } }); } /** * Returns the header definitions declared by the external header definition as a map using the header type name as * key. * * @return The header definitions declared. */ public Map getDefinitions() { return definitions; } private static final class FeedProperty implements CallBack { private final HeaderDefinition definition; private FeedProperty(HeaderDefinition definition) { this.definition = definition; } public void execute(XMLTag xmlDocument) { String value = xmlDocument.getText(); if ("".equals(value)) // value can't be null { value = xmlDocument.getCDATA(); } definition.setPropertyFromString(xmlDocument.getCurrentTagName(), value); } } }