Template.java revision ca669ae54f86dbeea277280690584d9f591c7571
325N/A/*
325N/A * CDDL HEADER START
325N/A *
325N/A * The contents of this file are subject to the terms of the
325N/A * Common Development and Distribution License, Version 1.0 only
325N/A * (the "License"). You may not use this file except in compliance
325N/A * with the License.
325N/A *
325N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
325N/A * or http://forgerock.org/license/CDDLv1.0.html.
325N/A * See the License for the specific language governing permissions
325N/A * and limitations under the License.
325N/A *
325N/A * When distributing Covered Code, include this CDDL HEADER in each
325N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
325N/A * If applicable, add the following below this CDDL HEADER, with the
325N/A * fields enclosed by brackets "[]" replaced with your own identifying
325N/A * information:
325N/A * Portions Copyright [yyyy] [name of copyright owner]
325N/A *
325N/A * CDDL HEADER END
325N/A *
325N/A *
325N/A * Copyright 2006-2009 Sun Microsystems, Inc.
325N/A * Portions Copyright 2014-2015 ForgeRock AS
325N/A */
325N/Apackage org.opends.server.tools.makeldif;
325N/A
325N/Aimport org.forgerock.i18n.LocalizableMessage;
325N/A
325N/Aimport java.io.IOException;
325N/Aimport java.util.HashSet;
325N/Aimport java.util.Map;
325N/A
325N/Aimport org.opends.server.types.AttributeType;
325N/Aimport org.opends.server.types.DN;
325N/A
325N/Aimport static org.opends.messages.ToolMessages.*;
325N/Aimport static org.opends.server.util.StaticUtils.*;
325N/A
325N/A/**
325N/A * This class defines a template, which is a pattern that may be used to
325N/A * generate entries. A template may be used either below a branch or below
325N/A * another template.
325N/A */
325N/Apublic class Template
325N/A{
325N/A // The attribute types that are used in the RDN for entries generated using
325N/A // this template.
325N/A private AttributeType[] rdnAttributes;
325N/A
325N/A // The number of entries to create for each subordinate template.
325N/A private int[] numEntriesPerTemplate;
325N/A
325N/A // The name for this template.
325N/A private String name;
325N/A
325N/A // The names of the subordinate templates below this template.
325N/A private String[] subordinateTemplateNames;
325N/A
325N/A // The subordinate templates below this template.
325N/A private Template[] subordinateTemplates;
325N/A
325N/A // The template file that contains this template.
325N/A private TemplateFile templateFile;
325N/A
325N/A // The set of template lines for this template.
325N/A private TemplateLine[] templateLines;
325N/A
325N/A
325N/A
325N/A /**
325N/A * Creates a new template with the provided information.
325N/A *
325N/A * @param templateFile The template file that contains this
325N/A * template.
325N/A * @param name The name for this template.
325N/A * @param rdnAttributes The set of attribute types that are used
325N/A * in the RDN for entries generated using
325N/A * this template.
325N/A * @param subordinateTemplateNames The names of the subordinate templates
325N/A * below this template.
325N/A * @param numEntriesPerTemplate The number of entries to create below
* each subordinate template.
*/
public Template(TemplateFile templateFile, String name,
AttributeType[] rdnAttributes,
String[] subordinateTemplateNames,
int[] numEntriesPerTemplate)
{
this.templateFile = templateFile;
this.name = name;
this.rdnAttributes = rdnAttributes;
this.subordinateTemplateNames = subordinateTemplateNames;
this.numEntriesPerTemplate = numEntriesPerTemplate;
templateLines = new TemplateLine[0];
subordinateTemplates = null;
}
/**
* Creates a new template with the provided information.
*
* @param templateFile The template file that contains this
* template.
* @param name The name for this template.
* @param rdnAttributes The set of attribute types that are used
* in the RDN for entries generated using
* this template.
* @param subordinateTemplateNames The names of the subordinate templates
* below this template.
* @param numEntriesPerTemplate The number of entries to create below
* each subordinate template.
* @param templateLines The set of template lines for this
* template.
*/
public Template(TemplateFile templateFile, String name,
AttributeType[] rdnAttributes,
String[] subordinateTemplateNames,
int[] numEntriesPerTemplate, TemplateLine[] templateLines)
{
this.templateFile = templateFile;
this.name = name;
this.rdnAttributes = rdnAttributes;
this.subordinateTemplateNames = subordinateTemplateNames;
this.numEntriesPerTemplate = numEntriesPerTemplate;
this.templateLines = templateLines;
subordinateTemplates = null;
}
/**
* Performs any necessary processing to ensure that the template
* initialization is completed. In particular, it should make sure that all
* referenced subordinate templates actually exist in the template file, and
* that all of the RDN attributes are contained in the template lines.
*
* @param templates The set of templates defined in the template file.
*
* @throws MakeLDIFException If any of the subordinate templates are not
* defined in the template file.
*/
public void completeTemplateInitialization(Map<String,Template> templates)
throws MakeLDIFException
{
// Make sure that all of the specified subordinate templates exist.
if (subordinateTemplateNames == null)
{
subordinateTemplateNames = new String[0];
subordinateTemplates = new Template[0];
}
else
{
subordinateTemplates = new Template[subordinateTemplateNames.length];
for (int i=0; i < subordinateTemplates.length; i++)
{
subordinateTemplates[i] =
templates.get(toLowerCase(subordinateTemplateNames[i]));
if (subordinateTemplates[i] == null)
{
LocalizableMessage message = ERR_MAKELDIF_UNDEFINED_TEMPLATE_SUBORDINATE.get(
subordinateTemplateNames[i], name);
throw new MakeLDIFException(message);
}
}
}
// Make sure that all of the RDN attributes are defined.
HashSet<AttributeType> rdnAttrs =
new HashSet<AttributeType>(rdnAttributes.length);
for (AttributeType t : rdnAttributes)
{
rdnAttrs.add(t);
}
for (TemplateLine l : templateLines)
{
if (rdnAttrs.remove(l.getAttributeType())
&& rdnAttrs.isEmpty())
{
break;
}
}
if (! rdnAttrs.isEmpty())
{
AttributeType t = rdnAttrs.iterator().next();
LocalizableMessage message =
ERR_MAKELDIF_TEMPLATE_MISSING_RDN_ATTR.get(name, t.getNameOrOID());
throw new MakeLDIFException(message);
}
}
/**
* Retrieves the name for this template.
*
* @return The name for this template.
*/
public String getName()
{
return name;
}
/**
* Retrieves the set of attribute types that are used in the RDN for entries
* generated using this template.
*
* @return The set of attribute types that are used in the RDN for entries
* generated using this template.
*/
public AttributeType[] getRDNAttributes()
{
return rdnAttributes;
}
/**
* Retrieves the names of the subordinate templates used to generate entries
* below entries created by this template.
*
* @return The names of the subordinate templates used to generate entries
* below entries created by this template.
*/
public String[] getSubordinateTemplateNames()
{
return subordinateTemplateNames;
}
/**
* Retrieves the subordinate templates used to generate entries below entries
* created by this template.
*
* @return The subordinate templates used to generate entries below entries
* created by this template.
*/
public Template[] getSubordinateTemplates()
{
return subordinateTemplates;
}
/**
* Retrieves the number of entries that should be created for each subordinate
* template.
*
* @return The number of entries that should be created for each subordinate
* template.
*/
public int[] getNumEntriesPerTemplate()
{
return numEntriesPerTemplate;
}
/**
* Retrieves the set of template lines for this template.
*
* @return The set of template lines for this template.
*/
public TemplateLine[] getTemplateLines()
{
return templateLines;
}
/**
* Adds the provided template line to this template.
*
* @param line The template line to add to this template.
*/
public void addTemplateLine(TemplateLine line)
{
TemplateLine[] newTemplateLines = new TemplateLine[templateLines.length+1];
System.arraycopy(templateLines, 0, newTemplateLines, 0,
templateLines.length);
newTemplateLines[templateLines.length] = line;
templateLines = newTemplateLines;
}
/**
* Indicates whether this template contains any template lines that reference
* the provided attribute type.
*
* @param attributeType The attribute type for which to make the
* determination.
*
* @return <CODE>true</CODE> if this template contains one or more template
* lines that reference the provided attribute type, or
* <CODE>false</CODE> if not.
*/
public boolean hasAttribute(AttributeType attributeType)
{
for (TemplateLine l : templateLines)
{
if (l.getAttributeType().equals(attributeType))
{
return true;
}
}
return false;
}
/**
* Writes the entry for this template, as well as all appropriate subordinate
* entries.
*
* @param entryWriter The entry writer that will be used to write the
* entries.
* @param parentDN The DN of the entry below which the subordinate
* entries should be generated.
* @param count The number of entries to generate based on this
* template.
*
* @return The result that indicates whether processing should continue.
*
* @throws IOException If a problem occurs while attempting to write to the
* LDIF writer.
*
* @throws MakeLDIFException If some other problem occurs.
*/
public TagResult writeEntries(EntryWriter entryWriter, DN parentDN, int count)
throws IOException, MakeLDIFException
{
for (int i=0; i < count; i++)
{
templateFile.nextFirstAndLastNames();
TemplateEntry templateEntry = new TemplateEntry(this, parentDN);
for (TemplateLine l : templateLines)
{
TagResult r = l.generateLine(templateEntry);
if (!r.keepProcessingEntry()
|| !r.keepProcessingParent()
|| !r.keepProcessingTemplateFile())
{
return r;
}
}
if (! entryWriter.writeEntry(templateEntry))
{
return TagResult.STOP_PROCESSING;
}
for (int j=0; j < subordinateTemplates.length; j++)
{
TagResult r =
subordinateTemplates[j].writeEntries(entryWriter,
templateEntry.getDN(), numEntriesPerTemplate[j]);
if (!r.keepProcessingParent()
|| !r.keepProcessingTemplateFile())
{
if (r.keepProcessingTemplateFile())
{
// We don't want to propagate a "stop processing parent" all the
// way up the chain.
return TagResult.SUCCESS_RESULT;
}
return r;
}
}
}
return TagResult.SUCCESS_RESULT;
}
}