📄 editlib.java.svn-base
字号:
if (schema.isSimpleElement(childName) || !elemType.isOrType()) { Element child = new Element(childName, md.getNamespace()); md.addContent(child); fillElement(schema, sugg, child); } } } } else { System.out.println("WARNING : requested expansion of an OR element : " +md.getName()); } } //-------------------------------------------------------------------------- //--- //--- Tree expansion methods //--- //-------------------------------------------------------------------------- /** Does a pre-order visit enumerating each node */ private int enumerateTree(Element md, int ref) { Element elem = new Element(Edit.RootChild.ELEMENT, Edit.NAMESPACE); elem.setAttribute(new Attribute(Edit.Element.Attr.REF, ref +"")); List list = md.getChildren(); for(int i=0; i<list.size(); i++) ref = enumerateTree((Element) list.get(i), ref +1); md.addContent(elem); return ref; } //-------------------------------------------------------------------------- /** Given a metadata, does a recursive scan adding information for editing */ private void expandTree(String schemaName, MetadataSchema schema, Element md) throws Exception { expandElement(schemaName, schema, md); List list = md.getChildren(); for(int i=0; i<list.size(); i++) { Element child = (Element) list.get(i); if (!Edit.NS_PREFIX.equals(child.getNamespacePrefix())) expandTree(schemaName, schema, child); } } //-------------------------------------------------------------------------- /** Adds editing information to a single element */ private void expandElement(String schemaName, MetadataSchema schema, Element md) throws Exception { String elemName = md.getQualifiedName(); // RGFIX: was: getname String elemType = schema.getElementType(elemName); System.out.println("elemName = " + elemName); // DEBUG Element elem = md.getChild(Edit.RootChild.ELEMENT, Edit.NAMESPACE);// addValues(schema, elem, elemName); // RGFIX if (schema.isSimpleElement(elemName)) return; MetadataType type = schema.getTypeInfo(elemType); if (!type.isOrType()) { for(int i=0; i<type.getElementCount(); i++) { String childName = type.getElementAt(i); System.out.println("- childName = " + childName); // DEBUG if (childName == null) continue; // schema extensions cause null types; just skip List list = md.getChildren(); if (list.size() == 0) { Element newElem = createElement(schemaName, schema, childName); if (i == 0) insertFirst(md, newElem); else insertLast(md, type.getElementAt(i-1), newElem); } else { for(int j=0; j<list.size(); j++) { Element listChild = (Element) list.get(j); 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, createElement(schemaName, schema, childName)); } } } addAttribs(type, md); } //-------------------------------------------------------------------------- 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, 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, 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, Element el) { if (Edit.NS_PREFIX.equals(el.getNamespacePrefix())) { if (Edit.RootChild.CHILD.equals(el.getName())) return childName.equals(el.getAttributeValue(Edit.ChildElem.Attr.NAME)); else return false; } else return childName.equals(el.getName()); } //-------------------------------------------------------------------------- private boolean equal(Element el1, Element el2) { String ns1 = el1.getNamespacePrefix(); String ns2 = el2.getNamespacePrefix(); if (Edit.NS_PREFIX.equals(ns1)) { if (Edit.NS_PREFIX.equals(ns2)) { //--- el1 has namespace, el2 has namespace 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); return name1.equals(name2); } else { //--- el1 has namespace, el2 not if (!Edit.RootChild.CHILD.equals(el1.getName())) return false; String name1 = el1.getAttributeValue(Edit.ChildElem.Attr.NAME); return el2.getName().equals(name1); } } else { if (Edit.NS_PREFIX.equals(ns2)) { //--- el1 has no namespace, el2 yes if (!Edit.RootChild.CHILD.equals(el2.getName())) return false; String name2 = el2.getAttributeValue(Edit.ChildElem.Attr.NAME); return el1.getName().equals(name2); } else { //--- el1 has no namespace, el2 neither return el1.getName().equals(el2.getName()); } } } //-------------------------------------------------------------------------- /** Create a new element for editing, adding all mandatory subtags */ private Element createElement(String schemaName, MetadataSchema schema, String name) throws Exception { Element child = new Element(Edit.RootChild.CHILD, Edit.NAMESPACE); child.setAttribute(new Attribute(Edit.ChildElem.Attr.NAME, name)); String prefix = ""; String namespace = ""; //FIXME: Cacciavitata if (schemaName.equals("dublin-core") && !name.equals("simpledc")) { prefix = "dc"; namespace = "http://purl.org/dc/elements/1.1/"; } child.setAttribute(new Attribute(Edit.ChildElem.Attr.PREFIX, prefix)); child.setAttribute(new Attribute(Edit.ChildElem.Attr.NAMESPACE, namespace)); if (!schema.isSimpleElement(name)) { String elemType = schema.getElementType(name); 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); choose.setAttribute(new Attribute(Edit.Choose.Attr.NAME, type.getElementAt(l))); child.addContent(choose); } } return child; } //-------------------------------------------------------------------------- private void addValues(MetadataSchema schema, Element elem, String name) { List values = schema.getElementValues(name); 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); attribute.setAttribute(new Attribute(Edit.Attribute.Attr.NAME, attr.name)); //--- 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 + -