📄 editlib.java
字号:
//-------------------------------------------------------------------------- private String getParentNameFromChild(Element child) { String childName = child.getQualifiedName(); String parentName = "root"; Element parent = child.getParentElement(); if (parent != null) { parentName = parent.getQualifiedName(); } return parentName; } //-------------------------------------------------------------------------- /** Checks substitutions for element and returns substitute based on * namespace prefix of the root - NOTE: this code is to support * profiles of ISO19139 which should have a different root element * namespace - so no subs are done if this is vanilla iso19139 ie. gmd * root namespace. */ private String getProfileSubstituteName(MetadataSchema schema, String childQName, Element md) { ArrayList subsNames = schema.getElementSubs(childQName); if (subsNames == null) return childQName; // find root element, where namespaces *must* be declared Element root = md; while (root.getParent() != null && root.getParent() instanceof Element) root = (Element)root.getParent(); String prefix = root.getNamespacePrefix(); if (prefix.equals("gmd")) return childQName; for (int i = 0;i < subsNames.size();i++) { String sub = (String) subsNames.get(i); if (getPrefix(sub).equals(prefix)) return sub; } return childQName; } //-------------------------------------------------------------------------- /** Replace CHOICE_ELEMENTs with their children by enumerating the tree */ public void replaceChoiceElements(Element md) throws Exception { enumerateTree(md); removeEditingInfo(md); } //-------------------------------------------------------------------------- /** Adds editing information to a single element */ private void expandElement(String schemaName, MetadataSchema schema, Element md) throws Exception { // System.out.println("entering expandElement()"); // DEBUG String elemName = md.getQualifiedName(); String parentName = getParentNameFromChild(md); // System.out.println("elemName = " + elemName); // DEBUG // System.out.println("parentName = " + parentName); // DEBUG String elemType = schema.getElementType(elemName,parentName); // System.out.println("elemType = " + elemType); // DEBUG Element elem = md.getChild(Edit.RootChild.ELEMENT, Edit.NAMESPACE); addValues(schema, elem, elemName, parentName); if (schema.isSimpleElement(elemName,parentName)) { // System.out.println("is simple element"); // DEBUG return; } MetadataType type = schema.getTypeInfo(elemType); boolean postProcessChoiceElements = false; if (!type.isOrType()) { for(int i=0; i<type.getElementCount(); i++) { String childQName = type.getElementAt(i); // System.out.println("- childName = " + childQName); // DEBUG if (childQName == null) continue; // schema extensions cause null types; just skip // Now if the element has subs examine them and substitute the // correct element based on the namespace of the root element if (type.examineSubs(i)) { childQName = getProfileSubstituteName(schema,childQName,md); // System.out.println("- childName has substituted = " + childQName); } String childName = getUnqualifiedName(childQName); String childPrefix = getPrefix(childQName); String childNS = getNamespace(childQName, md); // System.out.println("- name = " + childName); // DEBUG // System.out.println("- prefix = " + childPrefix); // DEBUG // System.out.println("- namespace = " + childNS); // DEBUG List list = md.getChildren(childName, Namespace.getNamespace(childNS)); if (list.size() == 0) { // System.out.println("- no children of this type already present"); // DEBUG Element newElem = createElement(schemaName, schema, elemName, childQName, childPrefix, childNS); if (i == 0) insertFirst(md, newElem); else { String prevQName = type.getElementAt(i-1); String prevName = getUnqualifiedName(prevQName); String prevNS = getNamespace(prevQName, md); insertLast(md, prevName, prevNS, newElem); } } else { // System.out.println("- " + list.size() + " children of this type already present"); // DEBUG // System.out.println("- min cardinality = " + type.getMinCardinAt(i)); // DEBUG // System.out.println("- max cardinality = " + type.getMaxCardinAt(i)); // DEBUG for(int j=0; j<list.size(); j++) { Element listChild = (Element) list.get(j); // IF this is a CHOICE_ELEMENT then we will replace it afterwards // with its children postProcessChoiceElements = true; Element listElem = listChild.getChild(Edit.RootChild.ELEMENT, Edit.NAMESPACE); if (j>0) listElem.setAttribute(new Attribute(Edit.Element.Attr.UP, Edit.Value.TRUE)); if (j<list.size() -1) listElem.setAttribute(new Attribute(Edit.Element.Attr.DOWN, Edit.Value.TRUE)); if (list.size() > type.getMinCardinAt(i)) listElem.setAttribute(new Attribute(Edit.Element.Attr.DEL, Edit.Value.TRUE)); } if (list.size() < type.getMaxCardinAt(i)) insertLast(md, childName, childNS, createElement(schemaName, schema, elemName, childQName, childPrefix, childNS)); } }// Deals with CHOICE_ELEMENTs by replacing them with their children// FIXME: must also copy up the geonet:element child so that minOccurs/maxOccurs// are properly carried - this isn't a problem yet if (postProcessChoiceElements) { List list = md.getChildren(); for (int i = 0;i < list.size();i++) { Element listChild = (Element) list.get(i); if (listChild.getName().indexOf("CHOICE_ELEMENT") != -1) { // System.out.println("###### Replacing "+listChild.getName()); List children = listChild.getChildren(); Element child = (Element)children.get(0); list.set(i,child.detach()); } } } } addAttribs(type, md); } //-------------------------------------------------------------------------- public String getUnqualifiedName(String qname) { int pos = qname.indexOf(":"); if (pos < 0) return qname; else return qname.substring(pos + 1); } //-------------------------------------------------------------------------- public String getPrefix(String qname) { int pos = qname.indexOf(":"); if (pos < 0) return ""; else return qname.substring(0, pos); } //-------------------------------------------------------------------------- public String getNamespace(String qname, Element md) { // check the element first to see whether the namespace is // declared locally String result = checkNamespaces(qname,md); if (result.equals("UNKNOWN")) { // find root element, where namespaces *must* be declared Element root = md; while (root.getParent() != null && root.getParent() instanceof Element) root = (Element)root.getParent(); result = checkNamespaces(qname,root); } return result; } //-------------------------------------------------------------------------- public String checkNamespaces(String qname, Element md) { // get prefix String prefix = getPrefix(qname); // loop on namespaces to fine the one corresponding to prefix Namespace rns = md.getNamespace(); if (prefix.equals(rns.getPrefix())) return rns.getURI(); for (Iterator i = md.getAdditionalNamespaces().iterator(); i.hasNext(); ) { Namespace ns = (Namespace)i.next(); if (prefix.equals(ns.getPrefix())) return ns.getURI(); } return "UNKNOWN"; } //-------------------------------------------------------------------------- private void insertFirst(Element md, Element child) { Vector v = new Vector(); v.add(child); List list = md.getChildren(); for(int i=0; i<list.size(); i++) v.add((Element) list.get(i)); //--- md.removeContent(); for(int i=0; i<v.size(); i++) md.addContent((Element) v.get(i)); } //-------------------------------------------------------------------------- private void insertLast(Element md, String childName, String childNS, Element child) { boolean added = false; List list = md.getChildren(); Vector v = new Vector(); for(int i=0; i<list.size(); i++) { Element el = (Element) list.get(i); v.add(el); if (equal(childName, childNS, el) && !added) { if (i == list.size() -1) { v.add(child); added = true; } else { Element elNext = (Element) list.get(i+1); if (!equal(el, elNext)) { v.add(child); added = true; } } } } md.removeContent(); for(int i=0; i<v.size(); i++) md.addContent((Element) v.get(i)); } //-------------------------------------------------------------------------- private boolean equal(String childName, String childNS, Element el) { if (Edit.NS_URI.equals(el.getNamespaceURI())) { if (Edit.RootChild.CHILD.equals(el.getName())) return childName.equals(el.getAttributeValue(Edit.ChildElem.Attr.NAME)) && childNS.equals(el.getAttributeValue(Edit.ChildElem.Attr.NAMESPACE)); else return false; } else return childName.equals(el.getName()) && childNS.equals(el.getNamespaceURI()); } //-------------------------------------------------------------------------- private boolean equal(Element el1, Element el2) { String elemNS1 = el1.getNamespaceURI(); String elemNS2 = el2.getNamespaceURI(); if (Edit.NS_URI.equals(elemNS1)) { if (Edit.NS_URI.equals(elemNS2)) { //--- both are geonet:child elements if (!Edit.RootChild.CHILD.equals(el1.getName())) return false; if (!Edit.RootChild.CHILD.equals(el2.getName())) return false; String name1 = el1.getAttributeValue(Edit.ChildElem.Attr.NAME); String name2 = el2.getAttributeValue(Edit.ChildElem.Attr.NAME); String ns1 = el1.getAttributeValue(Edit.ChildElem.Attr.NAMESPACE); String ns2 = el2.getAttributeValue(Edit.ChildElem.Attr.NAMESPACE); return name1.equals(name2) && ns1.equals(ns2); } else { //--- el1 is a geonet:child, el2 is not if (!Edit.RootChild.CHILD.equals(el1.getName())) return false; String name1 = el1.getAttributeValue(Edit.ChildElem.Attr.NAME); String ns1 = el1.getAttributeValue(Edit.ChildElem.Attr.NAMESPACE); return el2.getName().equals(name1) && el2.getNamespaceURI().equals(ns1); } } else { if (Edit.NS_URI.equals(elemNS2)) { //--- el2 is a geonet:child, el1 is not if (!Edit.RootChild.CHILD.equals(el2.getName())) return false; String name2 = el2.getAttributeValue(Edit.ChildElem.Attr.NAME); String ns2 = el2.getAttributeValue(Edit.ChildElem.Attr.NAMESPACE); return el1.getName().equals(name2) && el1.getNamespaceURI().equals(ns2); } else { //--- both not geonet:child elements return el1.getName().equals(el2.getName()) && el1.getNamespaceURI().equals(el2.getNamespaceURI()); } } } //-------------------------------------------------------------------------- /** Create a new element for editing, adding all mandatory subtags */ private Element createElement(String schemaName, MetadataSchema schema, String parent, String qname, String childPrefix, String childNS) throws Exception { Element child = new Element(Edit.RootChild.CHILD, Edit.NAMESPACE); child.setAttribute(new Attribute(Edit.ChildElem.Attr.NAME, getUnqualifiedName(qname))); child.setAttribute(new Attribute(Edit.ChildElem.Attr.PREFIX, getPrefix(qname))); child.setAttribute(new Attribute(Edit.ChildElem.Attr.NAMESPACE, childNS)); if (!schema.isSimpleElement(qname,parent)) { String elemType = schema.getElementType(qname,parent); MetadataType type = schema.getTypeInfo(elemType); if (type.isOrType()) for(int l=0; l<type.getElementCount(); l++) { Element choose = new Element(Edit.ChildElem.Child.CHOOSE, Edit.NAMESPACE); String chType = type.getElementAt(l); //String prefix = getPrefix(chType); //choose.setAttribute(new Attribute(Edit.Choose.Attr.NAME, getUnqualifiedName(chType))); choose.setAttribute(new Attribute(Edit.Choose.Attr.NAME, chType)); //if (!prefix.equals("")) // choose.setAttribute(new Attribute(Edit.Choose.Attr.PREFIX,prefix)); child.addContent(choose); } } return child; } //-------------------------------------------------------------------------- private void addValues(MetadataSchema schema, Element elem, String name, String parent) throws Exception { List values = schema.getElementValues(name,parent); if (values != null) for(int i=0; i<values.size(); i++) { Element text = new Element(Edit.Element.Child.TEXT, Edit.NAMESPACE); text.setAttribute(Edit.Attribute.Attr.VALUE, (String) values.get(i)); elem.addContent(text); } } //-------------------------------------------------------------------------- private void addAttribs(MetadataType type, Element md) { for(int i=0; i<type.getAttributeCount(); i++) { MetadataAttribute attr = type.getAttributeAt(i); Element attribute = new Element(Edit.RootChild.ATTRIBUTE, Edit.NAMESPACE); //String name = getUnqualifiedName(attr.name); //String prefix = getPrefix(attr.name); attribute.setAttribute(new Attribute(Edit.Attribute.Attr.NAME, attr.name)); //if (prefix != null && !prefix.equals("")) // attribute.setAttribute(new Attribute(Edit.Attribute.Attr.PREFIX, prefix)); //--- add default value (if any) if (attr.defValue != null) { Element def = new Element(Edit.Attribute.Child.DEFAULT, Edit.NAMESPACE); def.setAttribute(Edit.Attribute.Attr.VALUE, attr.defValue); attribute.addContent(def); } for(int j=0; j<attr.values.size(); j++) { Element text = new Element(Edit.Attribute.Child.TEXT, Edit.NAMESPACE); text.setAttribute(Edit.Attribute.Attr.VALUE, (String) attr.values.get(j)); attribute.addContent(text); } //--- handle 'add' and 'del' attribs boolean present = (md.getAttributeValue(attr.name) != null); if (!present) attribute.setAttribute(new Attribute(Edit.Attribute.Attr.ADD, Edit.Value.TRUE)); else if (!attr.required) attribute.setAttribute(new Attribute(Edit.Attribute.Attr.DEL, Edit.Value.TRUE)); md.addContent(attribute); } } //-------------------------------------------------------------------------- private SchemaSuggestions getSchemaSuggestions(String name) { SchemaSuggestions sugg = (SchemaSuggestions) htSchemaSugg.get(name); if (sugg == null) throw new IllegalArgumentException("Schema suggestions not registered : " + name); return sugg; }}//=============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -