org.datanucleus.api.jdo.metadata.JDOXmlMetaDataHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-api-jdo Show documentation
Show all versions of datanucleus-api-jdo Show documentation
Plugin providing DataNucleus implementation of the JDO API.
/**********************************************************************
Copyright (c) 2016 Andy Jefferson and others. All rights reserved.
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.
Contributors:
...
**********************************************************************/
package org.datanucleus.api.jdo.metadata;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.ArrayMetaData;
import org.datanucleus.metadata.ClassMetaData;
import org.datanucleus.metadata.CollectionMetaData;
import org.datanucleus.metadata.ColumnMetaData;
import org.datanucleus.metadata.ContainerMetaData;
import org.datanucleus.metadata.DiscriminatorMetaData;
import org.datanucleus.metadata.ElementMetaData;
import org.datanucleus.metadata.EmbeddedMetaData;
import org.datanucleus.metadata.FetchGroupMemberMetaData;
import org.datanucleus.metadata.FetchGroupMetaData;
import org.datanucleus.metadata.FetchPlanMetaData;
import org.datanucleus.metadata.FieldMetaData;
import org.datanucleus.metadata.FileMetaData;
import org.datanucleus.metadata.ForeignKeyMetaData;
import org.datanucleus.metadata.DatastoreIdentityMetaData;
import org.datanucleus.metadata.IndexMetaData;
import org.datanucleus.metadata.InheritanceMetaData;
import org.datanucleus.metadata.InterfaceMetaData;
import org.datanucleus.metadata.JoinMetaData;
import org.datanucleus.metadata.KeyMetaData;
import org.datanucleus.metadata.MapMetaData;
import org.datanucleus.metadata.MetaData;
import org.datanucleus.metadata.OrderMetaData;
import org.datanucleus.metadata.PackageMetaData;
import org.datanucleus.metadata.PrimaryKeyMetaData;
import org.datanucleus.metadata.PropertyMetaData;
import org.datanucleus.metadata.QueryMetaData;
import org.datanucleus.metadata.SequenceMetaData;
import org.datanucleus.metadata.UniqueMetaData;
import org.datanucleus.metadata.ValueMetaData;
import org.datanucleus.metadata.VersionMetaData;
import org.datanucleus.util.StringUtils;
/**
* Helper class that can convert internal metadata into (JDO) XML metadata.
*/
public class JDOXmlMetaDataHelper
{
public JDOXmlMetaDataHelper()
{
}
/**
* Method to convert an internal class/interface metadata into the associated JDO XML metadata.
* @param cmd Metadata for the class/interface
* @param prefix Prefix for the XML (e.g " ")
* @param indent Indent for each block of XML (e.g " ")
* @return The XML
*/
public String getXMLForMetaData(AbstractClassMetaData cmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
boolean intf = (cmd instanceof InterfaceMetaData);
if (intf)
{
str.append(prefix).append("\n");
// Identity
if (cmd.getDatastoreIdentityMetaData() != null)
{
str.append(getXMLForMetaData(cmd.getDatastoreIdentityMetaData(), prefix + indent,indent));
}
// PrimaryKey
if (cmd.getPrimaryKeyMetaData() != null)
{
str.append(getXMLForMetaData(cmd.getPrimaryKeyMetaData(), prefix + indent,indent));
}
// Inheritance
if (cmd.getInheritanceMetaData() != null)
{
str.append(getXMLForMetaData(cmd.getInheritanceMetaData(), prefix + indent,indent));
}
// Add Version
if (cmd.getVersionMetaData() != null)
{
str.append(getXMLForMetaData(cmd.getVersionMetaData(), prefix + indent,indent));
}
// Add joins
List joins = cmd.getJoinMetaData();
if (joins != null)
{
for (JoinMetaData joinmd : joins)
{
str.append(getXMLForMetaData(joinmd, prefix + indent, indent));
}
}
// Add foreign-keys
List foreignKeys = cmd.getForeignKeyMetaData();
if (foreignKeys != null)
{
for (ForeignKeyMetaData fkmd : foreignKeys)
{
str.append(getXMLForMetaData(fkmd, prefix + indent,indent));
}
}
// Add indexes
List indexes = cmd.getIndexMetaData();
if (indexes != null)
{
for (IndexMetaData idxmd : indexes)
{
str.append(getXMLForMetaData(idxmd, prefix + indent,indent));
}
}
// Add unique constraints
List uniqueConstraints = cmd.getUniqueMetaData();
if (uniqueConstraints != null)
{
for (UniqueMetaData unimd : uniqueConstraints)
{
str.append(getXMLForMetaData(unimd, prefix + indent,indent));
}
}
// Add members
int numMembers = cmd.getNoOfMembers();
for (int i=0;i unmappedColumns = cmd.getUnmappedColumns();
if (unmappedColumns != null)
{
for (int i=0;i fetchGroups = cmd.getFetchGroupMetaData();
if (fetchGroups != null)
{
for (FetchGroupMetaData fgmd : fetchGroups)
{
str.append(getXMLForMetaData(fgmd, prefix + indent,indent));
}
}
// Add extensions
processExtensions(cmd.getExtensions(), str, prefix, indent);
if (intf)
{
str.append(prefix + " \n");
}
else
{
str.append(prefix + "\n");
}
return str.toString();
}
public String getXMLForMetaData(DatastoreIdentityMetaData idmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
if (idmd.getValueStrategy() != null)
{
str.append(prefix).append(" 0)
{
str.append(">\n");
// Column MetaData
ColumnMetaData columnMetaData = idmd.getColumnMetaData();
if (columnMetaData != null)
{
str.append(getXMLForMetaData(columnMetaData, prefix + indent,indent));
}
// Add extensions
processExtensions(idmd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
}
else
{
str.append("/>\n");
}
return str.toString();
}
public String getXMLForMetaData(ColumnMetaData colmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append(" extensions = colmd.getExtensions();
if (extensions != null)
{
processExtensions(extensions, str, prefix, indent);
str.append(prefix).append(" \n");
}
else
{
str.append("/>\n");
}
return str.toString();
}
public String getXMLForMetaData(PrimaryKeyMetaData pkmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add columns
ColumnMetaData[] columns = pkmd.getColumnMetaData();
if (columns != null)
{
for (ColumnMetaData colmd : columns)
{
str.append(getXMLForMetaData(colmd, prefix + indent,indent));
}
}
// Add extensions
processExtensions(pkmd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(InheritanceMetaData inhmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add join
if (inhmd.getJoinMetaData() != null)
{
str.append(getXMLForMetaData(inhmd.getJoinMetaData(), prefix + indent,indent));
}
// Add discriminator
if (inhmd.getDiscriminatorMetaData() != null)
{
str.append(getXMLForMetaData(inhmd.getDiscriminatorMetaData(), prefix + indent,indent));
}
// Add extensions
processExtensions(inhmd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(DiscriminatorMetaData dismd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Column MetaData
if (dismd.getColumnMetaData() != null)
{
str.append(getXMLForMetaData(dismd.getColumnMetaData(), prefix + indent,indent));
}
// Add index
if (dismd.getIndexMetaData() != null)
{
str.append(getXMLForMetaData(dismd.getIndexMetaData(), prefix + indent,indent));
}
// Add extensions
processExtensions(dismd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(IndexMetaData idxmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n") : ">\n");
String[] memberNames = idxmd.getMemberNames();
if (memberNames != null)
{
for (String memberName : memberNames)
{
str.append(prefix).append(indent).append(" \n");
}
}
String[] columnNames = idxmd.getColumnNames();
if (columnNames != null)
{
for (String columnName : columnNames)
{
str.append(prefix).append(indent).append(" \n");
}
}
// Add extensions
processExtensions(idxmd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(UniqueMetaData unimd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n") : ">\n");
String[] memberNames = unimd.getMemberNames();
if (memberNames != null)
{
for (String memberName : memberNames)
{
str.append(prefix).append(indent).append(" \n");
}
}
String[] columnNames = unimd.getColumnNames();
if (columnNames != null)
{
for (String columnName : columnNames)
{
str.append(prefix).append(indent).append(" \n");
}
}
// Add extensions
processExtensions(unimd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(JoinMetaData joinmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Primary-key
if (joinmd.getPrimaryKeyMetaData() != null)
{
str.append(getXMLForMetaData(joinmd.getPrimaryKeyMetaData(), prefix + indent, indent));
}
// Add columns
ColumnMetaData[] columns = joinmd.getColumnMetaData();
if (columns != null)
{
for (ColumnMetaData colmd : columns)
{
str.append(getXMLForMetaData(colmd, prefix + indent,indent));
}
}
// Foreign-key
if (joinmd.getForeignKeyMetaData() != null)
{
str.append(getXMLForMetaData(joinmd.getForeignKeyMetaData(), prefix + indent, indent));
}
// Index
if (joinmd.getIndexMetaData() != null)
{
str.append(getXMLForMetaData(joinmd.getIndexMetaData(), prefix + indent, indent));
}
// Unique
if (joinmd.getUniqueMetaData() != null)
{
str.append(getXMLForMetaData(joinmd.getUniqueMetaData(), prefix + indent, indent));
}
// Add extensions
processExtensions(joinmd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(ForeignKeyMetaData fkmd, String prefix, String indent)
{
if (!StringUtils.isWhitespace(fkmd.getFkDefinition()))
{
return " ";
}
// Field needs outputting so generate metadata
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
String[] memberNames = fkmd.getMemberNames();
if (memberNames != null)
{
for (String memberName : memberNames)
{
str.append(prefix).append(indent).append(" \n");
}
}
ColumnMetaData[] columns = fkmd.getColumnMetaData();
if (columns != null)
{
for (ColumnMetaData colmd : columns)
{
str.append(getXMLForMetaData(colmd, prefix + indent,indent));
}
}
// Add extensions
processExtensions(fkmd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(VersionMetaData vermd, String prefix, String indent)
{
// Field needs outputting so generate metadata
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Column MetaData
if (vermd.getColumnMetaData() != null)
{
str.append(getXMLForMetaData(vermd.getColumnMetaData(), prefix + indent,indent));
}
// Add index
if (vermd.getIndexMetaData() != null)
{
str.append(getXMLForMetaData(vermd.getIndexMetaData(), prefix + indent,indent));
}
// Add extensions
processExtensions(vermd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(QueryMetaData qmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
str.append(prefix).append(qmd.getQuery()).append("\n");
// Add extensions
processExtensions(qmd.getExtensions(), str, prefix, indent);
str.append(prefix + " \n");
return str.toString();
}
public String getXMLForMetaData(FetchGroupMetaData fgmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add fetch-groups
Set fetchGroups = fgmd.getFetchGroups();
if (fetchGroups != null)
{
for (FetchGroupMetaData subfgmd : fetchGroups)
{
str.append(getXMLForMetaData(subfgmd, prefix + indent, indent));
}
}
// Add fields
Set members = fgmd.getMembers();
if (members != null)
{
for (FetchGroupMemberMetaData fgmmd : members)
{
str.append(getXMLForMetaData(fgmmd, prefix + indent, indent));
}
}
str.append(prefix + " \n");
return str.toString();
}
public String getXMLForMetaData(FetchGroupMemberMetaData fgmmd, String prefix, String indent)
{
StringBuilder sb = new StringBuilder();
if (fgmmd.isProperty())
{
sb.append(prefix).append(" \n");
}
else
{
sb.append("name=\"" + fgmmd.getName() + "\"/>\n");
}
return sb.toString();
}
public String getXMLForMetaData(AbstractMemberMetaData mmd, String prefix, String indent)
{
if (mmd.isStatic() || mmd.isFinal())
{
// If this field is static or final, don't bother with MetaData since we will ignore it anyway.
return "";
}
boolean field = (mmd instanceof FieldMetaData);
// Field needs outputting so generate metadata
StringBuilder str = new StringBuilder();
if (field)
{
str.append(prefix).append("\n");
// Add field containers
ContainerMetaData contmd = mmd.getContainer();
if (contmd != null)
{
if (contmd instanceof CollectionMetaData)
{
CollectionMetaData c = (CollectionMetaData)contmd;
str.append(getXMLForMetaData(c, prefix + indent,indent));
}
else if (contmd instanceof ArrayMetaData)
{
ArrayMetaData c = (ArrayMetaData)contmd;
str.append(getXMLForMetaData(c, prefix + indent,indent));
}
else if (contmd instanceof MapMetaData)
{
MapMetaData c = (MapMetaData)contmd;
str.append(getXMLForMetaData(c, prefix + indent,indent));
}
}
// Add columns
ColumnMetaData[] columnMetaData = mmd.getColumnMetaData();
if (columnMetaData != null)
{
for (int i=0; i\n");
}
else
{
str.append(prefix).append("\n");
}
return str.toString();
}
public String getXMLForMetaData(EmbeddedMetaData embmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
if (embmd.getDiscriminatorMetaData() != null)
{
str.append(getXMLForMetaData(embmd.getDiscriminatorMetaData(), prefix+indent, indent));
}
// Add members
List members = embmd.getMemberMetaData();
if (members != null)
{
for (AbstractMemberMetaData member : members)
{
str.append(getXMLForMetaData(member, prefix + indent,indent));
}
}
// Add extensions
processExtensions(embmd.getExtensions(), str, prefix, indent);
str.append(prefix + " \n");
return str.toString();
}
public String getXMLForMetaData(ElementMetaData elemmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add columns
ColumnMetaData[] columns = elemmd.getColumnMetaData();
if (columns != null)
{
for (ColumnMetaData colmd : columns)
{
str.append(getXMLForMetaData(colmd, prefix + indent,indent));
}
}
// Add index metadata
if (elemmd.getIndexMetaData() != null)
{
str.append(getXMLForMetaData(elemmd.getIndexMetaData(), prefix + indent,indent));
}
// Add unique metadata
if (elemmd.getUniqueMetaData() != null)
{
str.append(getXMLForMetaData(elemmd.getUniqueMetaData(), prefix + indent,indent));
}
// Add embedded metadata
if (elemmd.getEmbeddedMetaData() != null)
{
str.append(getXMLForMetaData(elemmd.getEmbeddedMetaData(), prefix + indent,indent));
}
// Add foreign-key metadata
if (elemmd.getForeignKeyMetaData() != null)
{
str.append(getXMLForMetaData(elemmd.getForeignKeyMetaData(), prefix + indent,indent));
}
// Add extensions
processExtensions(elemmd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(KeyMetaData keymd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add columns
ColumnMetaData[] columns = keymd.getColumnMetaData();
if (columns != null)
{
for (ColumnMetaData colmd : columns)
{
str.append(getXMLForMetaData(colmd, prefix + indent,indent));
}
}
// Add index metadata
if (keymd.getIndexMetaData() != null)
{
str.append(getXMLForMetaData(keymd.getIndexMetaData(), prefix + indent,indent));
}
// Add unique metadata
if (keymd.getUniqueMetaData() != null)
{
str.append(getXMLForMetaData(keymd.getUniqueMetaData(), prefix + indent,indent));
}
// Add embedded metadata
if (keymd.getEmbeddedMetaData() != null)
{
str.append(getXMLForMetaData(keymd.getEmbeddedMetaData(), prefix + indent,indent));
}
// Add foreign-key metadata
if (keymd.getForeignKeyMetaData() != null)
{
str.append(getXMLForMetaData(keymd.getForeignKeyMetaData(), prefix + indent,indent));
}
// Add extensions
processExtensions(keymd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(ValueMetaData valmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add columns
ColumnMetaData[] columns = valmd.getColumnMetaData();
if (columns != null)
{
for (ColumnMetaData colmd : columns)
{
str.append(getXMLForMetaData(colmd, prefix + indent,indent));
}
}
// Add index metadata
if (valmd.getIndexMetaData() != null)
{
str.append(getXMLForMetaData(valmd.getIndexMetaData(), prefix + indent,indent));
}
// Add unique metadata
if (valmd.getUniqueMetaData() != null)
{
str.append(getXMLForMetaData(valmd.getUniqueMetaData(), prefix + indent,indent));
}
// Add embedded metadata
if (valmd.getEmbeddedMetaData() != null)
{
str.append(getXMLForMetaData(valmd.getEmbeddedMetaData(), prefix + indent,indent));
}
// Add foreign-key metadata
if (valmd.getForeignKeyMetaData() != null)
{
str.append(getXMLForMetaData(valmd.getForeignKeyMetaData(), prefix + indent,indent));
}
// Add extensions
processExtensions(valmd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(OrderMetaData ordermd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add columns
ColumnMetaData[] columns = ordermd.getColumnMetaData();
if (columns != null)
{
for (int i=0; i\n");
return str.toString();
}
public String getXMLForMetaData(CollectionMetaData collmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add extensions
processExtensions(collmd.getExtensions(), str, prefix, indent);
str.append(prefix).append(" \n");
return str.toString();
}
public String getXMLForMetaData(MapMetaData mapmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
return str.toString();
}
public String getXMLForMetaData(ArrayMetaData arrmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append(" extensions = arrmd.getExtensions();
if (extensions != null)
{
str.append(">\n");
processExtensions(extensions, str, prefix, indent);
str.append(prefix).append(" \n");
}
else
{
str.append(prefix).append("/>\n");
}
return str.toString();
}
public String getXMLForMetaData(PackageMetaData pmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add interfaces
if (pmd.getNoOfInterfaces() > 0)
{
for (int i=0;i 0)
{
for (int i=0;i\n");
return str.toString();
}
public String getXMLForMetaData(SequenceMetaData seqmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("= 0)
{
str.append(" initial-value=\"" + seqmd.getInitialValue() + "\"");
}
if (seqmd.getAllocationSize() >= 0)
{
str.append(" allocation-size=\"" + seqmd.getAllocationSize() + "\"");
}
if (seqmd.getStrategy() != null)
{
str.append(" strategy=\"" + seqmd.getStrategy() + "\">");
}
str.append(">\n");
// Add extensions
processExtensions(seqmd.getExtensions(), str, prefix, indent);
str.append(prefix + " \n");
return str.toString();
}
public String getXMLForMetaData(FileMetaData filemd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add packages
if (filemd.getNoOfPackages() > 0)
{
for (int i=0;i");
return str.toString();
}
protected void processExtensions(Map extensions, StringBuilder str, String prefix, String indent)
{
if (extensions != null)
{
Iterator> entryIter = extensions.entrySet().iterator();
while (entryIter.hasNext())
{
Entry entry = entryIter.next();
str.append(getXMLForMetaData(entry.getKey(), entry.getValue(), prefix+indent, indent)).append("\n");
}
}
}
public String getXMLForMetaData(FetchPlanMetaData fpmd, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append("\n");
// Add fetch-groups
FetchGroupMetaData[] fetchGroups = fpmd.getFetchGroupMetaData();
if (fetchGroups != null)
{
for (FetchGroupMetaData fgmd : fetchGroups)
{
str.append(getXMLForMetaData(fgmd, prefix + indent, indent));
}
}
str.append(prefix + " \n");
return str.toString();
}
public String getXMLForMetaData(String key, String value, String prefix, String indent)
{
StringBuilder str = new StringBuilder();
str.append(prefix).append(" ");
return str.toString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy