📄 loader.java
字号:
if(isName(el, XSD_NS, TAG_SIMPLETYPE)) { ad = parseSimpleTypeAttr(el); } else if(isName(el, XSD_NS, TAG_COMPLEXTYPE)) { ad = parseComplexTypeAttr(el); } else if(isName(el, XSD_NS, TAG_ELEMENT)) { String id = el.getAttribute(ATTR_NAME); if(id == null) { throw new XMLException("No id specified in element", el); } String strType = el.getAttribute(ATTR_TYPE); if(strType == null) { throw new XMLException("No type specified in " + id, el); } int type = getType(el); int card = 0; String name = id; String[] defValue = null; ad = new AD(id, type, card, name, defValue); } else if(isName(el, XSD_NS, TAG_ANNOTATION)) { // } else { throw new XMLException("Unsupported tag " + el.getName() + ", ns=" + el.getNamespace() + ", name=" + el.getAttribute(ATTR_NAME), el); } return addAnnotation(ad, el); } /** * Get <tt>AttributeDefinition</tt> type from XSD element type. * * @return One of the <tt>AttributeDefinition.STRING...BOOLEAN</tt> types. * @throws XMLException if the XSD type is unsupported. */ static int getType(XMLElement el) { int type = -1; String strType = null; if(isName(el, XSD_NS, TAG_ELEMENT)) { strType = el.getAttribute(ATTR_TYPE); } else if(isName(el, XSD_NS, TAG_RESTRICTION)) { strType = el.getAttribute(ATTR_BASE); } else { throw new XMLException("Type is only supported in element and restriction tags", el); } if(strType == null) { throw new XMLException("No type in tag", el); } if("xsd:int".equals(strType)) { type = AttributeDefinition.INTEGER; } else if("xsd:string".equals(strType)) { type = AttributeDefinition.STRING; } else if("xsd:boolean".equals(strType)) { type = AttributeDefinition.BOOLEAN; } else if("xsd:float".equals(strType)) { type = AttributeDefinition.FLOAT; } else if("xsd:long".equals(strType)) { type = AttributeDefinition.LONG; } else if("xsd:short".equals(strType)) { type = AttributeDefinition.SHORT; } else if("xsd:char".equals(strType)) { type = AttributeDefinition.CHARACTER; } else if("xsd:double".equals(strType)) { type = AttributeDefinition.DOUBLE; } else { throw new XMLException("Unsupported type '" + strType + "'", el); } return type; } /** * Print sets of definitions to an XML file. */ public static void printMetatypeXML(MetaTypeProvider mtp, String[] servicePIDs, String[] factoryPIDs, boolean bXMLHeader, boolean bMetatypeTag, List propList, PrintWriter out) { if(bXMLHeader) { out.println("<?xml version=\"1.0\"?>"); } if(bMetatypeTag) { out.println("<metatype:metatype\n" + " xmlns:metatype=\"http://www.knopflerfish.org/XMLMetatype\"\n" + " xmlns:xsd = \"http://www.w3.org/2001/XMLSchema\">"); } out.println(""); out.println(" <xsd:schema>\n"); printOCDXML(mtp, servicePIDs, 1, out); out.println(""); printOCDXML(mtp, factoryPIDs, Integer.MAX_VALUE, out); out.println(""); out.println(" </xsd:schema>\n"); if(propList != null) { printValuesXML(propList, false, out); } if(bMetatypeTag) { out.println("</metatype:metatype>"); } } /** * Print a set of ObjectClassDefinitions as XML. * * @param mtp Metatype provider * @param pids Set of String (PIDs) * @param out writer to print to. */ public static void printOCDXML(MetaTypeProvider mtp, String[] pids, int maxOccurs, PrintWriter out) { for(int i = 0; i < pids.length; i++) { String pid = pids[i]; ObjectClassDefinition ocd = mtp.getObjectClassDefinition(pid, null); if(ocd instanceof OCD) { maxOccurs = ((OCD)ocd).maxInstances; } AttributeDefinition[] ads = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL); out.println(""); out.println(" <!-- " + (maxOccurs > 1 ? "Factory " : "Service ") + pid + " -->"); // out.println(" <xsd:schema>"); out.print (" <xsd:complexType " + ATTR_NAME + "=\"" + pid + "\""); out.print (" " + ATTR_MAXOCCURS + "=\"" + (maxOccurs == Integer.MAX_VALUE ? UNBOUNDED : Integer.toString(maxOccurs)) + "\""); if(ocd instanceof OCD) { OCD o2 = (OCD)ocd; String urlStr = o2.getIconURL(); if(urlStr != null) { if(urlStr.startsWith(BUNDLE_PROTO)) { int ix = urlStr.indexOf("/", BUNDLE_PROTO.length()); if(ix != -1) { urlStr = urlStr.substring(ix); } } out.print(" " + ATTR_ICONURL + "=\"" + urlStr + "\""); } } out.println(">"); printAnnotation(ocd.getDescription(), " ", out); for(int j = 0; j < ads.length; j++) { printXML(out, ads[j]); } out.println(" </xsd:complexType>"); // out.println(" </xsd:schema>\n"); } } static void printXMLSequence(PrintWriter out, AttributeDefinition ad, boolean bArray) { out.println(" <xsd:complexType " + ATTR_NAME + " = \"" + ad.getID() + "\">"); out.println(" <xsd:sequence " + ATTR_ARRAY + "=\"" + bArray + "\">"); out.println(" <xsd:element " + ATTR_NAME + " = \"" + ITEM + "\" " + ATTR_TYPE + "= \"" + getXSDType(ad.getType()) + "\"/>"); out.println(" </xsd:sequence>"); out.println(" </xsd:complexType>"); } /** * Print an attribute definition as XML. */ public static void printXML(PrintWriter out, AttributeDefinition ad) { if(ad.getCardinality() > 0) { printXMLSequence(out, ad, false); } else if(ad.getCardinality() < 0) { printXMLSequence(out, ad, true); } else { printXMLSingle(out, ad); } } static void printXMLSingle(PrintWriter out, AttributeDefinition ad) { String tag = getXSDType(ad.getType()); String[] optValues = ad.getOptionValues(); String[] optLabels = ad.getOptionLabels(); String desc = ad.getDescription(); if(optValues != null) { out.println(" <xsd:simpleType name = \"" + ad.getID() + "\">"); out.println(" <xsd:restriction base=\"" + tag + "\">"); for(int i = 0; i < optValues.length; i++) { out.println(" <xsd:enumeration value=\"" + optValues[i] + "\">"); if(optLabels != null) { printAnnotation(optLabels[i], " ", out); } out.println(" </xsd:enumeration>"); } out.println(" </xsd:restriction>"); out.println(" </xsd:simpleType>"); } else { if("".equals(desc)) { out.println(" <xsd:element name=\"" + ad.getID() + "\"" + " type=\"" + tag + "\"/>"); } else { out.println(" <xsd:element name=\"" + ad.getID() + "\"" + " type=\"" + tag + "\">"); printAnnotation(desc, " ", out); out.println(" </xsd:element>"); } } } public static void printValuesXML(List propList, boolean bXMLHeader, PrintWriter out) { if(bXMLHeader) { out.println("<?xml version=\"1.0\"?>"); } out.println(" <metatype:values\n" + " xmlns:metatype=\"http://www.knopflerfish.org/XMLMetatype\">" ); out.println(""); for(Iterator it = propList.iterator(); it.hasNext();) { Dictionary props = (Dictionary)it.next(); String pid = (String)props.get(SERVICE_PID); if(pid == null) { pid = (String)props.get("factory.pid"); } out.println(""); out.println(" <!-- pid " + pid + " -->"); out.println(" <" + pid + ">"); printPropertiesXML(props, out); out.println(" </" + pid + ">"); } out.println(" </metatype:values>"); } static void printPropertiesXML(Dictionary props, PrintWriter out) { for(Enumeration e = props.keys(); e.hasMoreElements(); ) { String key = (String)e.nextElement(); Object val = props.get(key); if(val instanceof Vector) { out.println(" <" + key + ">"); Vector v = (Vector)val; for(int i = 0; i < v.size(); i++) { out.println(" <item>" + v.elementAt(i) + "</item>"); } out.println(" </" + key + ">"); } else if(val.getClass().isArray()) { out.println(" <" + key + ">"); for(int i = 0; i < Array.getLength(val); i++) { out.println(" <item>" + Array.get(val, i) + "</item>"); } out.println(" </" + key + ">"); } else { out.println(" <" + key + ">" + val.toString() + "</" + key + ">"); } } } static String getXSDType(int type) { String tag = ""; switch(type) { case AttributeDefinition.STRING: return "xsd:string"; case AttributeDefinition.INTEGER: return "xsd:int"; case AttributeDefinition.LONG: return "xsd:long"; case AttributeDefinition.SHORT: return "xsd:short"; case AttributeDefinition.DOUBLE: return "xsd:double"; case AttributeDefinition.CHARACTER: return "xsd:char"; case AttributeDefinition.FLOAT: return "xsd:float"; case AttributeDefinition.BOOLEAN: return "xsd:boolean"; case AttributeDefinition.BIGINTEGER: return "xsd:integer"; case AttributeDefinition.BIGDECIMAL: return "xsd:decimal"; default: throw new IllegalArgumentException("Cannot print " + type); } } static void printAnnotation(String s, String prefix, PrintWriter out) { out.println(prefix + "<xsd:annotation>"); out.println(prefix + " <xsd:documentation>" + s + "</xsd:documentation>"); out.println(prefix + "</xsd:annotation>"); } static void assertTagName(XMLElement el, String name) { assertTagName(el, null, name); } static void assertTagName(XMLElement el, String namespace, String name) { if(!isName(el, namespace, name)) { throw new XMLException("Excepted tag '" + namespace + ":" + name + "', found '" + el.getFullName() + "'", el); } } static boolean isName(XMLElement el, String namespace, String name) { boolean b = el.getName().equals(name) && (namespace == null || el.getNamespace() == null || namespace.equals(el.getNamespace())); return b; } private static String[] toStringArray(Object[] val) { String[] r = new String[val.length]; for(int i = 0; i < val.length; i++) { r[i] = AD.escape(val[i].toString()); } return r; } private static String[] toStringArray(Vector val) { String[] r = new String[val.size()]; for(int i = 0; i < val.size(); i++) { r[i] = AD.escape(val.elementAt(i).toString()); } return r; }}class XMLException extends IllegalArgumentException { XMLElement el; private XMLException() { } public XMLException(XMLElement el) { this("", el); } public XMLException(String msg, XMLElement el) { super(msg + ", line=" + el.getLineNr()); this.el = el; } public XMLElement getXMLElement() { return el; }}class CMConfig { public String pid; public int maxInstances = 1; public AD[] ads; public String desc; public String iconURL; public CMConfig(String pid, AD[] ads, String desc, String iconURL, int maxInstances) { this.pid = pid; this.ads = ads; this.desc = desc != null ? desc : ""; this.iconURL = iconURL; this.maxInstances = maxInstances; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append("CMConfig["); sb.append("pid=" + pid); sb.append(", desc=" + desc); sb.append(", iconURL=" + iconURL); sb.append(", maxInstances=" + maxInstances); sb.append(", attribs="); for(int i = 0; i < ads.length; i++) { sb.append(ads[i]); if(i < ads.length - 1) { sb.append(", "); } } return sb.toString(); }}class Annotation { String appinfo; String doc; public Annotation() { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -