blob: 5616a29572d32a81c4e45b7d0ee249b83ddad74f [file] [log] [blame]
/*
* Copyright (c) 2002 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
* Jens Lukowski/Innoopract - initial renaming/restructuring
*
*/
package org.eclipse.wst.xml.core.internal.contentmodel.internal.test;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.eclipse.wst.xml.core.internal.contentmodel.CMAnyElement;
import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMContent;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDataType;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMEntityDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMGroup;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList;
import org.eclipse.wst.xml.core.internal.contentmodel.util.CMVisitor;
public class CMPrinter extends CMVisitor
{
protected StringBuffer sb = new StringBuffer();
protected Vector visitedElements = new Vector();
/****** formatting ******/
protected String indent = ""; //$NON-NLS-1$
protected void incrementIndent()
{
indent = indent + " "; //$NON-NLS-1$
}
protected void decrementIndent()
{
indent = indent.substring(0, indent.length()-2);
}
public void printCMNode(String filename, CMNode cmnode)
{
visitCMNode(cmnode);
try
{
FileOutputStream fileStream = new FileOutputStream(filename);
OutputStreamWriter writer = new OutputStreamWriter(fileStream);
writer.write(sb.toString());
writer.flush();
fileStream.close();
}
catch (Exception e) {}
}
public void visitCMAnyElement(CMAnyElement anyElement)
{
sb.append(indent + "<CMAnyElement"); //$NON-NLS-1$
printAttributes(sb, anyElement);
sb.append(">/n"); //$NON-NLS-1$
incrementIndent();
printProperties(sb, anyElement);
decrementIndent();
sb.append(indent + "</CMAnyElement>/n"); //$NON-NLS-1$
}
public void visitCMAttributeDeclaration(CMAttributeDeclaration ad)
{
sb.append(indent + "<CMAttributeDeclaration"); //$NON-NLS-1$
printAttributes(sb, ad);
sb.append(">\n"); //$NON-NLS-1$
incrementIndent();
printProperties(sb, ad);
decrementIndent();
visitCMNode(ad.getAttrType());
sb.append(indent + "</CMAttributeDeclaration>\n"); //$NON-NLS-1$
}
public void visitCMDataType(CMDataType dataType)
{
sb.append(indent + "<CMDataType"); //$NON-NLS-1$
printAttributes(sb, dataType);
sb.append(">\n"); //$NON-NLS-1$
incrementIndent();
sb.append(indent + "<ImpliedValue kind=\"" + dataType.getImpliedValueKind() + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
sb.append(dataType.getImpliedValue() + "</ImpliedValue>\n"); //$NON-NLS-1$
printEnumeration(sb, dataType.getEnumeratedValues());
decrementIndent();
sb.append(indent + "</CMDataType>\n"); //$NON-NLS-1$
}
public void visitCMDocument(CMDocument document)
{
sb.append(indent + "<CMDocument"); //$NON-NLS-1$
printAttributes(sb, document);
sb.append(">\n"); //$NON-NLS-1$
incrementIndent();
// printEntities(sb, document.getEntities());
printProperties(sb, document);
CMNamedNodeMap map = document.getElements();
int size = map.getLength();
for (int i = 0; i < size; i++)
{
visitCMNode(map.item(i));
}
decrementIndent();
sb.append(indent + "</CMDocument>\n"); //$NON-NLS-1$
}
public void visitCMGroup(CMGroup group)
{
sb.append(indent + "<CMGroup"); //$NON-NLS-1$
printAttributes(sb, group);
sb.append(">\n"); //$NON-NLS-1$
incrementIndent();
CMNodeList nodeList = group.getChildNodes();
int size = nodeList.getLength();
for (int i = 0; i < size; i++)
{
visitCMNode(nodeList.item(i));
}
decrementIndent();
sb.append(indent + "</CMGroup>\n"); //$NON-NLS-1$
}
public void visitCMElementDeclaration(CMElementDeclaration ed)
{
if (!visitedElements.contains(ed))
{
visitedElements.add(ed);
sb.append(indent + "<CMElementDeclaration"); //$NON-NLS-1$
printAttributes(sb, ed);
sb.append(">\n"); //$NON-NLS-1$
incrementIndent();
printProperties(sb, ed);
CMNamedNodeMap nodeMap = ed.getAttributes();
int size = nodeMap.getLength();
for (int i = 0; i < size; i++)
{
visitCMNode(nodeMap.item(i));
}
visitCMNode(ed.getContent());
CMDataType dataType = ed.getDataType();
if (dataType != null)
visitCMNode(dataType);
decrementIndent();
sb.append(indent + "</CMElementDeclaration>\n"); //$NON-NLS-1$
}
}
public void printEnumeration(StringBuffer sb, String[] values)
{
if ((values != null) && (values.length > 0))
{
sb.append(indent + "<Enumeration>\n"); //$NON-NLS-1$
incrementIndent();
for (int i=0; i<values.length; i++)
{
sb.append(indent + "<Value>" + values[i] + "</Values>\n"); //$NON-NLS-1$ //$NON-NLS-2$
}
decrementIndent();
sb.append(indent + "</Enumeration>\n"); //$NON-NLS-1$
}
}
public void printAttributes(StringBuffer sb, CMNode cmnode)
{
if (cmnode != null)
{
sb.append(" nodeName=\"" + cmnode.getNodeName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
sb.append(" nodeType=\"" + cmnode.getNodeType() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
if (cmnode instanceof CMContent)
{
sb.append(" minOccur=\"" + ((CMContent)cmnode).getMinOccur() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
sb.append(" maxOccur=\"" + ((CMContent)cmnode).getMaxOccur() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
if (cmnode instanceof CMAnyElement)
{
sb.append(" namespaceURI=\"" + ((CMAnyElement)cmnode).getNamespaceURI() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
if (cmnode instanceof CMAttributeDeclaration)
{
sb.append(" uasage=\"" + ((CMAttributeDeclaration)cmnode).getUsage() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
sb.append(" defaultValue=\"" + ((CMAttributeDeclaration)cmnode).getDefaultValue() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
if (cmnode instanceof CMDataType)
{
sb.append(" instanceValue=\"" + ((CMDataType)cmnode).generateInstanceValue() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
if (cmnode instanceof CMEntityDeclaration)
{
String value = ((CMEntityDeclaration)cmnode).getValue();
if (value.indexOf("\"") == -1) sb.append(" value=\"" + value + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
else sb.append(" value=\"" + value + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
if (cmnode instanceof CMGroup)
{
sb.append(" operator=\"" + ((CMGroup)cmnode).getOperator() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
if (cmnode instanceof CMElementDeclaration)
{
sb.append(" contentType=\"" + ((CMElementDeclaration)cmnode).getContentType() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}
public void printEntities(StringBuffer sb, CMNamedNodeMap entities)
{
if ((entities != null) && (entities.getLength()>0))
{
sb.append(indent + "<Entities>\n"); //$NON-NLS-1$
incrementIndent();
for (Iterator i = entities.iterator(); i.hasNext();)
{
CMEntityDeclaration entity = (CMEntityDeclaration)i.next();
sb.append(indent + "<Entity"); //$NON-NLS-1$
printAttributes(sb, entity);
sb.append("/>\n"); //$NON-NLS-1$
}
decrementIndent();
sb.append(indent + "</Entities>\n"); //$NON-NLS-1$
}
}
public void printProperties(StringBuffer sb, CMNode cmnode)
{
List properties = getProperties(cmnode);
sb.append(indent + "<Properties>\n"); //$NON-NLS-1$
incrementIndent();
for (int i=0; i<properties.size(); i++)
{
String property = (String)properties.get(i);
sb.append(indent + "<Property name=\"" + property + "\""); //$NON-NLS-1$ //$NON-NLS-2$
if (cmnode.supports(property))
{
sb.append(" supports=\"true\""); //$NON-NLS-1$
}
else
{
sb.append(" supports=\"false\""); //$NON-NLS-1$
}
Object value = cmnode.getProperty(property);
if (value instanceof String) sb.append(" value=\"" + (String)value + "\""); //$NON-NLS-1$ //$NON-NLS-2$
else if (value instanceof Boolean) sb.append(" value=\"" + value + "\""); //$NON-NLS-1$ //$NON-NLS-2$
else if (value != null) sb.append(" value=\"" + value.getClass() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
else sb.append(" value=\"null\""); //$NON-NLS-1$
sb.append("/>\n"); //$NON-NLS-1$
}
decrementIndent();
sb.append(indent + "</Properties>\n"); //$NON-NLS-1$
}
public List getProperties(CMNode cmnode)
{
List properties = new Vector();
if (cmnode != null)
{
properties.add("CMDocument"); //$NON-NLS-1$
properties.add("documentation"); //$NON-NLS-1$
properties.add("http://org.eclipse.wst/cm/properties/usesLocalElementDeclarations"); //$NON-NLS-1$
properties.add("http://org.eclipse.wst/cm/properties/isNameSpaceAware"); //$NON-NLS-1$
properties.add("http://org.eclipse.wst/cm/properties/nsPrefixQualification"); //$NON-NLS-1$
properties.add("http://org.eclipse.wst/cm/properties/nillable"); //$NON-NLS-1$
properties.add("http://org.eclipse.wst/cm/properties/mofNotifier"); //$NON-NLS-1$
properties.add("spec"); //$NON-NLS-1$
if (cmnode instanceof CMElementDeclaration)
{
properties.add("http://org.eclipse.wst/cm/properties/definitionInfo"); //$NON-NLS-1$
properties.add("http://org.eclipse.wst/cm/properties/definition"); //$NON-NLS-1$
properties.add("XSITypes"); //$NON-NLS-1$
properties.add("DerivedElementDeclaration"); //$NON-NLS-1$
// properties.add("SubstitutionGroup"); Currrently broken for nexted xsd files
properties.add("Abstract"); //$NON-NLS-1$
}
if (cmnode instanceof CMDocument)
{
properties.add("http://org.eclipse.wst/cm/properties/targetNamespaceURI"); //$NON-NLS-1$
properties.add("http://org.eclipse.wst/cm/properties/importedNamespaceInfo"); //$NON-NLS-1$
properties.add("http://org.eclipse.wst/cm/properties/namespaceInfo"); //$NON-NLS-1$
properties.add("http://org.eclipse.wst/cm/properties/elementFormDefault"); //$NON-NLS-1$
properties.add("annotationMap"); //$NON-NLS-1$
properties.add("encodingInfo"); //$NON-NLS-1$
}
// properties defines but not used
// properties.add("documentationSource");
// properties.add("documentationLanguage");
// properties.add("isValidEmptyValue");
// properties.add("http://org.eclipse.wst/cm/properties/defaultRootName");
}
return properties;
}
}