Java tutorial
/** * This class represents an XSD 'element' element. * * Copyright (C) 2007 Stephen Harding * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Please send inquiries to; steve@inverse2.com * * $Revision: 1.3 $ * * $Log: XSDElement.java,v $ * Revision 1.3 2008/07/01 17:26:46 stevewdh * Changed logging so that user can specify the type they want (Java, Log4J or HTML). * * Revision 1.2 2008/03/05 10:47:27 stevewdh * *** empty log message *** * * Revision 1.1 2007/10/04 11:06:42 stevewdh * *** empty log message *** * * Revision 1.2 2007/09/30 13:09:05 stephen harding * Added contact details to license header. * * Revision 1.1 2007/09/15 16:09:05 stephen harding * Added header. * * */ package com.init.octo.schema; import java.util.Iterator; import java.util.List; import org.jdom2.Element; import com.init.octo.util.Logger; import com.init.octo.util.Stack; public class XSDElement extends XSDElementType { private static Stack nameStack = new Stack(); // used by show to generate AJAX Ids // @todo implement fixed attribute... defines value for the element (if it is a simple type...) public XSDElement() { this(0); } public XSDElement(int indent) { this.log = Logger.getLogger(XSDElement.class.getName()); this.indent = indent; this.cachedType = false; groupType = "XSDElement"; } /** * This method builds the element definition * * @param root - the schema element that defines this element * @param cache - a list of pre-defined XML types */ public boolean build(Element root, XSDCache cache, String parentName) { log.debug("" + indent + ": " + "Build representation of an Element"); // root.getAttributeValue(XSDSchema.ID_ATT); //root.getAttributeValue(XSDSchema.BLOCK_ATT); //root.getAttributeValue(XSDSchema.FINAL_ATT); name = root.getAttributeValue(XSDSchema.NAME_ATT); ref = root.getAttributeValue(XSDSchema.REF_ATT); type = root.getAttributeValue(XSDSchema.TYPE_ATT); maxOccurs = root.getAttributeValue(XSDSchema.MAXOCCURS_ATT); minOccurs = root.getAttributeValue(XSDSchema.MINOCCURS_ATT); this.parentName = parentName; log.debug("" + indent + ": Element <" + name + ">"); /** Add the element definition to the element cache... **/ if (name != null && name.equals("") == false) { log.debug("Adding element [" + name + "] to the cache"); cache.putElement(name, this); } /** If this element is a reference to another full definition then get that definition... **/ if (ref != null && ref.equals("") == false) { name = ref; XSDElement refElement = (XSDElement) cache.getElement(ref); if (refElement == null) { log.debug("The reference element [" + ref + "] not in the cache..."); return (true); } log.debug("The reference element [" + ref + "] retrieved from the cache..."); /* Referenced element found so make this element look like it... */ elementType = refElement.getElementType(); if (maxOccurs == null) { maxOccurs = refElement.getMaxOccurs(); } if (minOccurs == null) { minOccurs = refElement.getMinOccurs(); } if (name == null) { name = refElement.getName(); } defaultOccurs(); return (true); } defaultOccurs(); /** if the type attribute has been specified then lookup its definition in the cache **/ if (type != null) { log.debug("" + indent + ": This element is of type <" + type + ">"); elementType = (XSDElementType) cache.getElementTypeComplex(type); if (elementType == null) { log.debug("No definition of type <" + type + "> found in type cache"); /** carry on, the type was probably an XSD standard type... **/ } else { cachedType = true; /** no sub definition allowed if the type has been specified **/ return (true); } } Element element; String elementName; for (Iterator<Element> i = (root.getChildren()).iterator(); i.hasNext();) { element = (Element) i.next(); elementName = element.getName(); log.debug("" + indent + ": " + "Child element <" + elementName + "> found"); /** process the child elements that define this element... **/ if (elementName.equals(XSDSchema.ANNOTATION)) { annotation = element.getTextTrim(); } else if (elementName.equals(XSDSchema.SIMPLETYPE)) { if (elementType != null) { log.error("Cannot define the type of an element more than once"); return (false); } elementType = new XSDElementTypeSimple(indent); if (elementType.build(element, cache, getFullName()) == false) { log.error("Error building a simpleType object"); return (false); } } else if (elementName.equals(XSDSchema.COMPLEXTYPE)) { if (elementType != null) { log.error("Cannot define the type of an element more than once"); return (false); } elementType = new XSDElementTypeComplex(indent); if (elementType.build(element, cache, getFullName()) == false) { log.error("Error building a complexType object"); return (false); } } else { log.warn("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored"); } } // end for all child elements of this <element> tag log.debug("" + indent + ": " + "Element built"); return (true); } // end build public XSDElementType getElementType() { return (elementType); } public void setElementType(XSDElementType type) { elementType = type; } public String getMaxOccurs() { return (maxOccurs); } public String getMinOccurs() { return (minOccurs); } private void defaultOccurs() { if (maxOccurs == null) { maxOccurs = "1"; } if (minOccurs == null) { minOccurs = "1"; } } public String getFullName() { return (((parentName.equals("")) ? "" : parentName + ".") + name); } /** * This method shows the internal representation of the schema */ public void show(int preIndent, boolean subCachedType, boolean htmlForm) { int currentIndent = this.indent + preIndent; String indentString = ""; for (int i = 0; i < currentIndent; i++) { indentString = indentString + " "; } if (htmlForm) { String fieldName; String container; String required; String namespaceLessType; String useSystemDefault; if (xToMany()) { fieldName = name + "[#]"; useSystemDefault = "false"; } else { fieldName = name; useSystemDefault = "true"; } if (hasChildren()) { container = "true"; } else { container = "false"; } if (isMandatory()) { required = "true"; } else { required = "false"; } namespaceLessType = (type == null) ? "" : type.replaceAll("^.*:", ""); nameStack.push(fieldName); /* Output name and type... */ System.out.println(indentString + "{"); System.out.println(indentString + " id=" + nameStack.join(".")); System.out.println(indentString + " type=" + namespaceLessType); System.out.println(indentString + " container=" + container); System.out.println(indentString + " tooltiptext=" + ((annotation == null) ? "" : annotation)); System.out.println(indentString + " name=" + name); System.out.println(indentString + " required=" + required); System.out.println(indentString + " minOccurs=" + minOccurs); System.out.println(indentString + " maxOccurs=" + maxOccurs); System.out.println(indentString + " useSystemDefault=" + useSystemDefault); System.err.println(indentString + "{"); System.err.println(indentString + " id=" + nameStack.join(".")); } else { System.err.print(indentString + "<" + name); if (elementType != null) { System.err.print(elementType.attributesToString()); } System.err.print(">"); if (minOccurs != null) { System.err.print("[minOccurs=\"" + minOccurs + "\"]"); } if (maxOccurs != null) { System.err.print("[maxOccurs=\"" + maxOccurs + "\"]"); } System.err.print("\n"); } if (elementType != null) { } if (htmlForm) { nameStack.pop(); System.out.println(indentString + "}"); System.err.println(indentString + "}"); } else { System.err.print(indentString + "</" + name + ">\n"); } return; } // end show() @Override public boolean getListOfSchemaItems(int preIndent, boolean subCachedType, List<SchemaItem> list) { boolean currentCachedType = subCachedType | cachedType; int currentIndent = this.indent + preIndent; SchemaItem item = new SchemaItem(); item.setItem(this); item.setIndent(currentIndent); list.add(item); if (elementType != null) { elementType.attributesToList((currentCachedType ? currentIndent : 0), currentCachedType, list); elementType.getListOfSchemaItems((currentCachedType ? currentIndent : 0), currentCachedType, list); } return (false); } public String getName() { return (name); } public void setName(String name) { this.name = name; } public XSDElement getChild(String childName) { if (elementType == null) { return (null); } return (elementType.getChild(childName)); } public List<XSDElement> getChildren() { return (elementType.getChildren()); } public String toString() { return ("XSDElement: " + ((type == null) ? " " : ("[" + type + "] ")) + name); } public boolean hasChildren() { boolean hasChildren = false; if (null != elementType && null != elementType.getChildren() && elementType.getChildren().size() > 0) { hasChildren = true; } return hasChildren; } public boolean xToMany() { if (maxOccurs == null || maxOccurs.equals("1")) { return (false); } if (maxOccurs.equals("0")) { return (false); } return (true); } public boolean isMandatory() { if (minOccurs == null || minOccurs.equals("0")) { return (false); } return (true); } public List<XSDAttributeType> getAttributeList() { if (elementType == null) { return (null); } return (elementType.getAttributeList()); } public String getDefaultAttValue(String attname) { List<?> attlist = getAttributeList(); if (attlist != null) { for (Iterator<?> it = attlist.iterator(); it.hasNext();) { XSDAttribute att = (XSDAttribute) it.next(); if (att.getName().equals(attname)) { return (att.getDefault()); } } return (""); } else { return (""); } } @Override public String attributesToString() { // TODO Auto-generated method stub return null; } @Override public List<XMLType> getGroup() { // TODO Auto-generated method stub return null; } @Override public List<SchemaItem> attributesToList(int preIndent, boolean subCachedType, List<SchemaItem> list) { // TODO Auto-generated method stub return null; } } // end class XSDElement