1 package org.codehaus.modello.plugins.xml; 2 3 /* 4 * Copyright (c) 2004, Codehaus.org 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of 7 * this software and associated documentation files (the "Software"), to deal in 8 * the Software without restriction, including without limitation the rights to 9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 10 * of the Software, and to permit persons to whom the Software is furnished to do 11 * so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 25 import java.util.List; 26 27 import org.codehaus.modello.model.ModelClass; 28 import org.codehaus.modello.model.ModelField; 29 import org.codehaus.modello.model.Version; 30 import org.codehaus.modello.plugin.AbstractModelloGenerator; 31 import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata; 32 import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata; 33 import org.codehaus.plexus.util.xml.XMLWriter; 34 import org.codehaus.plexus.util.xml.XmlWriterUtil; 35 36 /** 37 * Abstract class for plugins working on XML representation of the model, without having any need to generate 38 * Java code. 39 * 40 * @author <a href="mailto:hboutemy@codehaus.org">Hervé Boutemy</a> 41 */ 42 public abstract class AbstractXmlGenerator extends AbstractModelloGenerator { 43 protected void initHeader(XMLWriter w) { 44 XmlWriterUtil.writeComment(w, getHeader()); 45 } 46 47 /** 48 * Resolve XML tag name for a class. Note: only root class needs such a resolution. 49 * 50 * @param modelClass the model class 51 * @return the XML tag name for the class 52 */ 53 protected String resolveTagName(ModelClass modelClass) { 54 return XmlModelHelpers.resolveTagName(modelClass); 55 } 56 57 /** 58 * Resolve XML tag name for a field. 59 * 60 * @param modelField the model field 61 * @param xmlFieldMetadata the XML metadata of the field 62 * @return the XML tag name for the field 63 */ 64 protected String resolveTagName(ModelField modelField, XmlFieldMetadata xmlFieldMetadata) { 65 return XmlModelHelpers.resolveTagName(modelField, xmlFieldMetadata); 66 } 67 68 /** 69 * Resolve XML tag name for an item in an association with many multiplicity. 70 * 71 * @param fieldTagName the XML tag name of the field containing the association 72 * @param xmlAssociationMetadata the XML metadata of the association 73 * @return the XML tag name for items 74 */ 75 protected String resolveTagName(String fieldTagName, XmlAssociationMetadata xmlAssociationMetadata) { 76 return XmlModelHelpers.resolveTagName(fieldTagName, xmlAssociationMetadata); 77 } 78 79 protected boolean hasContentField(List<ModelField> modelFields) { 80 return (getContentField(modelFields) != null); 81 } 82 83 /** 84 * Get the field which type is <code>Content</code> if any. 85 * 86 * @param modelFields the fields to check 87 * @return the field, or <code>null</code> if no field is <code>Content</code> 88 */ 89 protected ModelField getContentField(List<ModelField> modelFields) { 90 return XmlModelHelpers.getContentField(modelFields); 91 } 92 93 /** 94 * Gets all fields that are not marked as XML attribute. 95 * 96 * @param modelFields The collection of model fields from which to extract the XML attributes, must not be 97 * <code>null</code>. 98 * @return The list of XML attributes fields, can be empty but never <code>null</code>. 99 */ 100 protected List<ModelField> getXmlAttributeFields(List<ModelField> modelFields) { 101 return XmlModelHelpers.getXmlAttributeFields(modelFields); 102 } 103 104 /** 105 * Return the XML fields of this class, with proper XML order and no XML transient fields. 106 * 107 * @param modelClass current class 108 * @param version the version of the class to use 109 * @return the list of XML fields of this class 110 */ 111 protected List<ModelField> getFieldsForXml(ModelClass modelClass, Version version) { 112 return XmlModelHelpers.getFieldsForXml(modelClass, version); 113 } 114 }