📄 loader.java
字号:
public static Object loadValue(AttributeDefinition attr, XMLElement el) { assertTagName(el, attr.getID()); if(attr.getCardinality() < 0) { return loadSequence(attr, el, -attr.getCardinality(), ITEM); } if(attr.getCardinality() > 0) { Vector v = loadSequence(attr, el, -attr.getCardinality(), ITEM); Object[] array = (Object[])Array.newInstance(AD.getClass(attr.getType()), v.size()); v.copyInto(array); return array; } return loadContent(attr, el); } public static Vector loadSequence(AttributeDefinition attr, XMLElement el, int max, String tagName) { Vector v = new Vector(); for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); assertTagName(childEl, tagName); v.addElement(loadContent(attr, childEl)); } return v; } /** * Load the contents of a tag into a java object. * * @param el element which content should be converted to a java object. * @param attr definition defining type. */ public static Object loadContent(AttributeDefinition attr, XMLElement el) { String content = el.getContent(); if(content == null) { content = ""; } content = content.trim(); String msg = attr.validate(content); if(msg != null && !"".equals(msg)) { throw new XMLException("Validation error: '" + msg + "'", el); } return AD.parseSingle(content, attr.getType()); } /** * Parse a services or factories tag info an array of wrapper * objects. * *<p> * Children to this tag must all be "xsd:schema" *</p> */ static CMConfig[] parseServices(XMLElement el, boolean bFactory) { assertTagName(el, METATYPE_NS, bFactory ? FACTORIES : SERVICES); List list = new ArrayList(); for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); CMConfig[] conf = parseSchema(childEl); if(conf.length == 0) { throw new XMLException("No lements in schema", childEl); } conf[0].maxInstances = bFactory ? Integer.MAX_VALUE : 1; list.add(conf[0]); } CMConfig[] ads = new CMConfig[list.size()]; list.toArray(ads); return ads; } /** * Parse an XSD schema into a wrapper object for services and factories. * * <p> * Each schema element must contain exacly one child "xsd:complexType" * </p> */ private static CMConfig[] parseSchema(XMLElement el) { assertTagName(el, XSD_NS, "schema"); /* if(el.getChildrenCount() != 1) { throw new XMLException("service/factory schema must contain exacly one xsd.complexType", el); } */ List v = new ArrayList(); for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); AD[] ads = parseComplexType(childEl); Annotation an = loadAnnotationFromAny(childEl); String iconURL = childEl.getAttribute(ATTR_ICONURL); if("".equals(iconURL)) { iconURL = null; } int maxOccurs = getInteger(childEl, ATTR_MAXOCCURS, 1); String name = childEl.getAttribute(ATTR_NAME).toString(); // System.out.println("load " + name + ", maxOccurs=" + maxOccurs); v.add(new CMConfig(name, ads, an != null ? an.doc : "", iconURL, maxOccurs)); } CMConfig[] r = new CMConfig[v.size()]; v.toArray(r); return r; } static final String UNBOUNDED = "unbounded"; static int getInteger(XMLElement el, String attr, int def) { String s = el.getAttribute(attr, Integer.toString(def)); if(UNBOUNDED.equals(s)) { return Integer.MAX_VALUE; } return Integer.parseInt(s); } /** * Parse an XSD complexType info an array of <tt>AttributeDefinition</tt>, * definining the metadata for a PID. * * <p> * The name of the complexTyp specifies the metadata PID. * </p> * * <p> * The following child elements are supported: * <ul> * <li><b>xsd:element</b> - defines a singleton definition * <li><b>xsd:sequence</b> - defines a vector or array definition * </ul> * * The names of the elements definines the ID's of the definitions. Each * ID must only be present once. * </p> * * @throws XMLException if the <tt>el</tt> tag is not an "xsd:complexType" */ static AD[] parseComplexType(XMLElement el) { assertTagName(el, XSD_NS, TAG_COMPLEXTYPE); Set list = new HashSet(); Annotation annotation = null; for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); if(isName(childEl, XSD_NS, TAG_ANNOTATION)) { annotation = loadAnnotation(childEl); } else { try { AD ad = parseAttributeDefinition(childEl); if(list.contains(ad)) { throw new XMLException("Multiple definitions of id '" + ad.getID() + "'", childEl); } if(ad == null) { throw new XMLException("Null ad", childEl); } list.add(ad); } catch (XMLException ex) { System.out.println("Failed in " + el.getFullName() + ", name=" + el.getAttribute(ATTR_NAME) + ", line=" + el.getLineNr() + ", " + ex); throw ex; } } } AD[] ads = new AD[list.size()]; list.toArray(ads); return ads; } /** * Parse an XSD sequence into an <tt>AttributeDefinition</tt> of either * vector or array type. * * <p> * Only one child name "element" is allowed, and this child specifies * the element type of the vector/array. * </p> * * @throws XMLException of element is not an "xsd:sequence" */ static AD parseComplexTypeAttr(XMLElement el) { assertTagName(el, XSD_NS, TAG_COMPLEXTYPE); AD attr = null; for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); if(isName(childEl, XSD_NS, TAG_SEQUENCE)) { if(attr != null) { throw new XMLException("Only one sequence is allowed in complexType", childEl); } attr = parseSequence(childEl, el.getAttribute(ATTR_NAME)); } else if(isName(childEl, XSD_NS, TAG_RESTRICTION)) { System.out.println("skip restriction"); } else if(isName(childEl, XSD_NS, TAG_ANNOTATION)) { // parse later } } if(attr == null) { throw new XMLException("No sequence found in complexType", el); } return addAnnotation(attr, el); } static AD parseSimpleTypeAttr(XMLElement el) { assertTagName(el, XSD_NS, TAG_SIMPLETYPE); AD attr = null; String id = el.getAttribute(ATTR_NAME).toString(); for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); if(isName(childEl, XSD_NS, TAG_RESTRICTION)) { int type = getType(childEl); int card = 0; String name = id; String[] defValue = null; attr = new AD(id, type, card, name, defValue); addEnumeration(childEl, attr); } else if(isName(childEl, XSD_NS, TAG_ANNOTATION)) { // accept and parse later; } } return addAnnotation(attr, el); } static void addEnumeration(XMLElement el, AD ad) { assertTagName(el, XSD_NS, TAG_RESTRICTION); Vector v = new Vector(); for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); // System.out.println(" addEnum " + childEl.getName()); if(isName(childEl, XSD_NS, TAG_ENUMERATION)) { String val = childEl.getAttribute(ATTR_VALUE); if(val == null) { throw new XMLException("No value specified in enum", childEl); } String label = val; Annotation annotation = loadAnnotationFromAny(childEl); if(annotation != null && annotation.doc != null) { label = annotation.doc; } v.addElement(new String[] { val, label }); } } // System.out.println("optvalues=" + v); if(v.size() > 0) { String[] optValues = new String[v.size()]; String[] optLabels = new String[v.size()]; for(int i = 0; i < v.size(); i++) { String[] row = (String[])v.elementAt(i); optValues[i] = row[0]; optLabels[i] = row[1]; } ad.setOptions(optValues, optLabels); } } static AD addAnnotation(AD attr, XMLElement el) { Annotation a = loadAnnotationFromAny(el); if(a != null) { if(a.doc != null) { attr.setDescription(a.doc); } } int minOccurs = 1; try { minOccurs = Integer.parseInt(el.getAttribute(ATTR_MINOCCURS, "1")); } catch (Exception e) { throw new XMLException("minOccurs must be a valid integer: " + e, el); } if(minOccurs > 1) { throw new XMLException("minOccurs cannot be > 1, is " + minOccurs, el); } attr.bOptional = minOccurs == 0; return attr; } static Annotation loadAnnotationFromAny(XMLElement el) { for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); if(isName(childEl, XSD_NS, TAG_ANNOTATION)) { return loadAnnotation(childEl); } } return null; } static Annotation loadAnnotation(XMLElement el) { assertTagName(el, XSD_NS, TAG_ANNOTATION); Annotation a = null; for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); if(isName(childEl, XSD_NS, TAG_DOCUMENTATION)) { if(a == null) { a = new Annotation(); } a.doc = "" + childEl.getContent(); } else if(isName(childEl, XSD_NS, TAG_APPINFO)) { if(a == null) { a = new Annotation(); } a.appinfo = "" + childEl.getContent(); } } return a; } static AD parseSequence(XMLElement el, String name) { // System.out.println("parseSequence " + el.getAttribute(ATTR_NAME)); assertTagName(el, XSD_NS, TAG_SEQUENCE); boolean bArray = "true".equals(el.getAttribute(ATTR_ARRAY, "false").toLowerCase()); int maxOccurs = getInteger(el, ATTR_MAXOCCURS, Integer.MAX_VALUE); if(el.getChildrenCount() != 1) { throw new XMLException("sequence children count must be " + "exactly one", el); } else { String id = name; int type = -1; for(Enumeration e = el.enumerateChildren(); e.hasMoreElements(); ) { XMLElement childEl = (XMLElement)e.nextElement(); String childName = childEl.getAttribute(ATTR_NAME).toString(); int card = -1; if(!ITEM.equals(childName)) { throw new XMLException("Only '" + ITEM + "'" + " names are allowed in sequences, found " + childName, childEl); } if(bArray) { card = maxOccurs; } else { if(maxOccurs == Integer.MAX_VALUE) { card = Integer.MIN_VALUE; } else { card = -maxOccurs; } } AD ad = parseAttributeDefinition(childEl); type = ad.getType(); String[] defValue = null; return new AD(id, type, card, id, defValue); } throw new XMLException("parseSequence failed", el); } } /** * Parse an XSD element into an <tt>AttributeDefinition</tt> * * <p> * The type of the definition is derived using the <tt>getType</tt> * method. * </p> * <p> * If the XSD element is a sequence, parse using the <tt>parseSequence</tt> * method. * </p> */ static AD parseAttributeDefinition(XMLElement el) { // System.out.println("parseAttributeDefinition " + el.getFullName() + ", name=" + el.getAttribute(ATTR_NAME)); AD ad = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -