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 List<String> header = getHeader(); 45 for (String headerLine : header) { 46 XmlWriterUtil.writeComment(w, headerLine); 47 } 48 } 49 50 /** 51 * Resolve XML tag name for a class. Note: only root class needs such a resolution. 52 * 53 * @param modelClass the model class 54 * @return the XML tag name for the class 55 */ 56 protected String resolveTagName(ModelClass modelClass) { 57 return XmlModelHelpers.resolveTagName(modelClass); 58 } 59 60 /** 61 * Resolve XML tag name for a field. 62 * 63 * @param modelField the model field 64 * @param xmlFieldMetadata the XML metadata of the field 65 * @return the XML tag name for the field 66 */ 67 protected String resolveTagName(ModelField modelField, XmlFieldMetadata xmlFieldMetadata) { 68 return XmlModelHelpers.resolveTagName(modelField, xmlFieldMetadata); 69 } 70 71 /** 72 * Resolve XML tag name for an item in an association with many multiplicity. 73 * 74 * @param fieldTagName the XML tag name of the field containing the association 75 * @param xmlAssociationMetadata the XML metadata of the association 76 * @return the XML tag name for items 77 */ 78 protected String resolveTagName(String fieldTagName, XmlAssociationMetadata xmlAssociationMetadata) { 79 return XmlModelHelpers.resolveTagName(fieldTagName, xmlAssociationMetadata); 80 } 81 82 protected boolean hasContentField(List<ModelField> modelFields) { 83 return (getContentField(modelFields) != null); 84 } 85 86 /** 87 * Get the field which type is <code>Content</code> if any. 88 * 89 * @param modelFields the fields to check 90 * @return the field, or <code>null</code> if no field is <code>Content</code> 91 */ 92 protected ModelField getContentField(List<ModelField> modelFields) { 93 return XmlModelHelpers.getContentField(modelFields); 94 } 95 96 /** 97 * Gets all fields that are not marked as XML attribute. 98 * 99 * @param modelFields The collection of model fields from which to extract the XML attributes, must not be 100 * <code>null</code>. 101 * @return The list of XML attributes fields, can be empty but never <code>null</code>. 102 */ 103 protected List<ModelField> getXmlAttributeFields(List<ModelField> modelFields) { 104 return XmlModelHelpers.getXmlAttributeFields(modelFields); 105 } 106 107 /** 108 * Return the XML fields of this class, with proper XML order and no XML transient fields. 109 * 110 * @param modelClass current class 111 * @param version the version of the class to use 112 * @return the list of XML fields of this class 113 */ 114 protected List<ModelField> getFieldsForXml(ModelClass modelClass, Version version) { 115 return XmlModelHelpers.getFieldsForXml(modelClass, version); 116 } 117 }