/** * Factory method, equivalent to a "fromXML" for step creation. Looks for a class with the same * name as the XML tag, with the first letter capitalized. For example, <call /> is * abbot.script.Call. */ public static Step createStep(Resolver resolver, Element el) throws InvalidScriptException { String tag = el.getName(); Map attributes = createAttributeMap(el); String name = tag.substring(0, 1).toUpperCase() + tag.substring(1); if (tag.equals(TAG_WAIT)) { attributes.put(TAG_WAIT, "true"); name = "Assert"; } try { name = "abbot.script." + name; Log.debug("Instantiating " + name); Class cls = Class.forName(name); try { // Steps with contents require access to the XML element Class[] argTypes = new Class[] {Resolver.class, Element.class, Map.class}; Constructor ctor = cls.getConstructor(argTypes); return (Step) ctor.newInstance(new Object[] {resolver, el, attributes}); } catch (NoSuchMethodException nsm) { // All steps must support this ctor Class[] argTypes = new Class[] {Resolver.class, Map.class}; Constructor ctor = cls.getConstructor(argTypes); return (Step) ctor.newInstance(new Object[] {resolver, attributes}); } } catch (ClassNotFoundException cnf) { String msg = Strings.get("step.unknown_tag", new Object[] {tag}); throw new InvalidScriptException(msg); } catch (InvocationTargetException ite) { Log.warn(ite); throw new InvalidScriptException(ite.getTargetException().getMessage()); } catch (Exception exc) { Log.warn(exc); throw new InvalidScriptException(exc.getMessage()); } }
/** * 普通版填写包的公共部分 * @param root 开始节点 * @param map 参数map * @param sCycName 循环节点名 * @return */ public Element writeptPublicNode(Element root,HashMap map,String sCycName) { Element node=null; try { String sName=""; String sValue=""; List list=root.getChildren(); for(int i=0;i<list.size();i++) { node=(Element)list.get(i); sName=node.getName(); sName=sName.trim(); sValue=(String)map.get(sName); //System.err.println("sName:"+sName+":"+sValue); if(sCycName.equals(sName)&&(root.getName()).trim().equals("ReqParamSet")) { //System.err.println("find cyc node:"+sName ); return node; } else if(sValue!=null && !"".equals(sValue) ) node.setText(sValue); node=writeptPublicNode(node,map,sCycName); } }catch(Exception ex) { ex.printStackTrace(); System.err.println("error:"+ex.getMessage()); } return node; }
/* * 递归完成查找 * para: Element root 开始查找节点 */ public Element findNode(Element root,String sNodeName) { Element node =null; try { String sName=""; List list=root.getChildren(); for(int i=0;i<list.size();i++) { node=(Element)list.get(i); sName=node.getName(); sName=sName.trim(); //System.err.println("node name:"+sName+"-"+sNodeName); if(sName.equals(sNodeName)) { System.err.println("find the node:"+sName); return node; } node=findNode(node,sNodeName); } }catch(Exception ex) { ex.printStackTrace(); System.err.println("error:"+ex.getMessage()); } return node; }
/** * This will invoke the <code>startElement</code> callback in the <code>ContentHandler</code>. * * @param element <code>Element</code> used in callbacks. * @param nsAtts <code>List</code> of namespaces to declare with the element or <code>null</code>. */ private void startElement(Element element, Attributes nsAtts) throws JDOMException { String namespaceURI = element.getNamespaceURI(); String localName = element.getName(); String rawName = element.getQualifiedName(); // Allocate attribute list. AttributesImpl atts = (nsAtts != null) ? new AttributesImpl(nsAtts) : new AttributesImpl(); List attributes = element.getAttributes(); Iterator i = attributes.iterator(); while (i.hasNext()) { Attribute a = (Attribute) i.next(); atts.addAttribute( a.getNamespaceURI(), a.getName(), a.getQualifiedName(), getAttributeTypeName(a.getAttributeType()), a.getValue()); } try { contentHandler.startElement(namespaceURI, localName, rawName, atts); } catch (SAXException se) { throw new JDOMException("Exception in startElement", se); } }
private void writeContent(final int end, final List childElements, final int depth) throws IOException { // sets index to end for (final Iterator i = childElements.iterator(); i.hasNext(); ) { final Element element = (Element) i.next(); final int elementBegin = element.begin; if (elementBegin >= end) break; if (indentAllElements) { writeText(elementBegin, depth); writeElement(element, depth, end, false, false); } else { if (inlinable(element)) continue; // skip over elements that can be inlined. writeText(elementBegin, depth); final String elementName = element.getName(); if (elementName == HTMLElementName.PRE || elementName == HTMLElementName.TEXTAREA) { writeElement(element, depth, end, true, true); } else if (elementName == HTMLElementName.SCRIPT) { writeElement(element, depth, end, true, false); } else { writeElement( element, depth, end, false, !removeLineBreaks && containsOnlyInlineLevelChildElements(element)); } } } writeText(end, depth); }
private static Element parseElement(XMLStreamReader xsr) throws XMLStreamException { // xsr points to a START_ELEMENT event. Create the element and read all its attributes // Then read all its children events Element element = new Element(xsr.getLocalName()); // text that will be added to the element. Text can come in different events, so we add it here // and add it to the element at the end StringBuilder elementText = new StringBuilder(); int attributeCount = xsr.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { element.putAttribute(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } while (xsr.hasNext()) { xsr.next(); if (xsr.getEventType() == XMLStreamConstants.END_ELEMENT) { // element is closed. Move the cursor and return it // check if there is some text to add before (empty text is not added, but added text is not // trimmed) // we set empty text also if the element has no children if (!elementText.toString().trim().isEmpty() || !element.hasChildren()) { element.setText(elementText.toString()); } // xsr.next(); return element; } else if (xsr.getEventType() == XMLStreamConstants.CHARACTERS) { // an attribute of the current element elementText.append(xsr.getText()); } else if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) { // new element begins -> read it recursively and add it to the current element element.addChild(parseElement(xsr)); } } // we reached the end of the document without the tag end -> error parsing throw new XMLStreamException( "End of the document unexpectedly reached. Element " + element.getName() + " not closed"); }
public void handleElement(Element e) { // create family if (e.getName().equals("family")) { try { lexicon.add(new Family(e)); } catch (RuntimeException exc) { System.err.println("Skipping family: " + e.getAttributeValue("name")); System.err.println(exc.toString()); } } // save distributive attributes else if (e.getName().equals("distributive-features")) distrElt = e; // save licensing features else if (e.getName().equals("licensing-features")) licensingElt = e; // save relation sort order else if (e.getName().equals("relation-sorting")) relationSortingElt = e; }
/** * Check to see if the object matches a predefined set of rules. * * @param obj The object to verify. * @return <code>true</code> if the objected matched a predfined set of rules. */ public boolean matches(Object obj) { if (obj instanceof Element) { Element el = (Element) obj; return (this.name == null || this.name.equals(el.getName())) && (this.namespace == null || this.namespace.equals(el.getNamespace())); } return false; }
private void load_snap_from_xml(Element snap, StructureCollection structs, LinkedList tempList) throws VisualizerLoadException { Iterator iterator = snap.getChildren().iterator(); if (!iterator.hasNext()) throw new VisualizerLoadException("Ran out of elements"); structs.loadTitle((Element) (iterator.next()), tempList, this); structs.calcDimsAndStartPts(tempList, this); if (!iterator.hasNext()) return; Element child = (Element) iterator.next(); if (child.getName().compareTo("doc_url") == 0) { // load the doc_url add_a_documentation_URL(child.getText().trim()); if (!iterator.hasNext()) return; child = (Element) iterator.next(); } if (child.getName().compareTo("pseudocode_url") == 0) { // load the psuedocode_url add_a_pseudocode_URL(child.getText().trim()); if (!iterator.hasNext()) return; child = (Element) iterator.next(); } if (child.getName().compareTo("audio_text") == 0) { // load the psuedocode_url add_audio_text(child.getText().trim()); if (!iterator.hasNext()) return; child = (Element) iterator.next(); } // create & load the individual structures, hooking them into the StructureCollection. // linespernode : calculate it at end of loadStructure call while (child.getName().compareTo("question_ref") != 0) { StructureType child_struct = assignStructureType(child); child_struct.loadStructure(child, tempList, this); structs.addChild(child_struct); if (!iterator.hasNext()) return; child = (Element) iterator.next(); } if (child.getName().compareTo("question_ref") == 0) { // set up question ref add_a_question_xml(child.getAttributeValue("ref")); } else // should never happen throw new VisualizerLoadException( "Expected question_ref element, but found " + child.getName()); if (iterator.hasNext()) { child = (Element) iterator.next(); throw new VisualizerLoadException( "Found " + child.getName() + " when expecting end of snap."); } } // load_snap_from_xml
public void addVariables(Element inRoot) throws org.jdom2.DataConversionException { Element vs = inRoot.getChild("decoder").getChild("variables"); Element segment = new Element("segment"); segment.setAttribute("space", "253"); root.addContent(segment); Iterator it = vs.getDescendants(); while (it.hasNext()) { Object o = it.next(); if (o instanceof Element) { Element e = (Element) o; if (e.getName().equals("variable")) { // get common attributes String comment = e.getAttributeValue("comment"); for (Object oc : e.getChildren("comment")) { Element ec = (Element) oc; if (ec.getAttributeValue("lang", "xml").equals("")) comment = ec.getText(); } String name = e.getAttributeValue("label"); if (name.equals("")) name = e.getAttributeValue("item"); for (Object on : e.getChildren("label")) { Element en = (Element) on; if (en.getAttributeValue("lang", "xml").equals("")) name = en.getText(); } long cv = e.getAttribute("CV").getIntValue(); // find subtype and process Element type; type = e.getChild("decVal"); if (type != null) { segment.addContent(handleDecVal(type, cv, name, comment, e.getAttributeValue("mask"))); continue; } type = e.getChild("enumVal"); if (type != null) { segment.addContent(handleEnumVal(type, cv, name, comment, e.getAttributeValue("mask"))); continue; } type = e.getChild("shortAddressVal"); if (type != null) { segment.addContent(handleShortAddressVal(type, cv, name, comment)); continue; } type = e.getChild("longAddressVal"); if (type != null) { segment.addContent(handleLongAddressVal(type, cv, name, comment)); continue; } } } } }
public void handleElement(Element e) { // create morph item if (e.getName().equals("entry")) { try { morphItems.add(new MorphItem(e)); } catch (RuntimeException exc) { System.err.println("Skipping morph item: " + e.getAttributeValue("word")); System.err.println(exc.toString()); } } // create macro item else if (e.getName().equals("macro")) { try { macroItems.add(new MacroItem(e)); } catch (RuntimeException exc) { System.err.println("Skipping macro item: " + e.getAttributeValue("name")); System.err.println(exc.toString()); } } }
/** * This will invoke the <code>endElement</code> callback in the <code>ContentHandler</code>. * * @param element <code>Element</code> used in callbacks. */ private void endElement(Element element) throws JDOMException { String namespaceURI = element.getNamespaceURI(); String localName = element.getName(); String rawName = element.getQualifiedName(); try { contentHandler.endElement(namespaceURI, localName, rawName); } catch (SAXException se) { throw new JDOMException("Exception in endElement", se); } }
/** * 新增循环节点 * @param cycNode 循环节点 * @return */ public Element addCycNode(Element cycNode) { Element node=cycNode; Element child=null; Element newNode=null; Element tmpNode=null; String sName=""; if(cycNode==null) return newNode; newNode=new Element(node.getName()); node.getParent().addContent(newNode); List cList=node.getChildren(); for(int i=0;i<cList.size();i++) { child=(Element)cList.get(i); sName=child.getName(); sName=sName.trim(); tmpNode=new Element(sName); newNode.addContent(tmpNode); } return newNode; }
private boolean containsOnlyInlineLevelChildElements(final Element element) { // returns true if the element contains only inline-level elements except for SCRIPT elements. final Collection childElements = element.getChildElements(); if (childElements.isEmpty()) return true; for (final Iterator i = childElements.iterator(); i.hasNext(); ) { final Element childElement = (Element) i.next(); final String elementName = childElement.getName(); if (elementName == HTMLElementName.SCRIPT || !HTMLElements.getInlineLevelElementNames().contains(elementName)) return false; if (!containsOnlyInlineLevelChildElements(childElement)) return false; } return true; }
private static void writeElement(XMLStreamWriter xtw, Element element) throws XMLStreamException { xtw.writeStartElement(element.getName()); for (String attributeName : element.getAttributeNames()) { xtw.writeAttribute(attributeName, element.getAttributeValue(attributeName)); } if (element.hasText()) { xtw.writeCharacters(element.getText()); } else { for (Element child : element.getChildren()) { writeElement(xtw, child); } } xtw.writeEndElement(); }
private boolean inlinable(final Element element) { // returns true if the specified element should be inlined final StartTagType startTagType = element.getStartTag().getStartTagType(); if (startTagType == StartTagType.DOCTYPE_DECLARATION) return false; if (startTagType != StartTagType.NORMAL) return true; // element is a normal type final String elementName = element.getName(); if (elementName == HTMLElementName.SCRIPT) return !indentScriptElements; if (removeLineBreaks && !HTMLElements.getElementNames().contains(elementName)) return true; // inline non-HTML elements if removing line breaks if (!HTMLElements.getInlineLevelElementNames().contains(elementName)) return false; // element is inline type if (removeLineBreaks) return true; return containsOnlyInlineLevelChildElements( element); // only inline if it doesn't illegally contain non-inline elements }
public void write(Writer out, Document doc, int pos, int len, Map<String, String> copiedImgs) throws IOException, BadLocationException { Debug.log(9, "SikuliEditorKit.write %d %d", pos, len); DefaultStyledDocument sdoc = (DefaultStyledDocument) doc; int i = pos; String absPath; while (i < pos + len) { Element e = sdoc.getCharacterElement(i); int start = e.getStartOffset(), end = e.getEndOffset(); if (e.getName().equals(StyleConstants.ComponentElementName)) { // A image argument to be filled AttributeSet attr = e.getAttributes(); Component com = StyleConstants.getComponent(attr); out.write(com.toString()); if (copiedImgs != null && (com instanceof EditorPatternButton || com instanceof EditorPatternLabel)) { if (com instanceof EditorPatternButton) { absPath = ((EditorPatternButton) com).getFilename(); } else { absPath = ((EditorPatternLabel) com).getFile(); } String fname = (new File(absPath)).getName(); copiedImgs.put(fname, absPath); Debug.log(3, "save image for copy&paste: " + fname + " -> " + absPath); } } else { if (start < pos) { start = pos; } if (end > pos + len) { end = pos + len; } out.write(doc.getText(start, end - start)); } i = end; } out.close(); }
/** * * @param map 需要写入的参数map * @param start 开始节点 * @param bNew 是否新增节点再写入参数 * @return */ public void writeParaMap(Element start,HashMap map) { try { String sName=""; String sValue=""; Element node=null; List list=start.getChildren(); for(int i=0;i<list.size();i++) { node=(Element)list.get(i); sName=node.getName(); sName=sName.trim(); sValue=(String)map.get(sName); if(sValue!=null && !"".equals(sValue) ); node.setText(sValue); } }catch(Exception ex) { ex.printStackTrace(); System.err.println("error:"+ex.getMessage()); } return ; }
private StructureType assignStructureType(Element structure) throws VisualizerLoadException { String name = structure.getName().toLowerCase(); if (name.compareTo("graph") == 0) { if (structure.getAttributeValue("weighted").compareTo("true") == 0) return new Graph_Network("NETWORK"); else return new Graph_Network("GRAPH"); } else if (name.equals("array")) return new MD_Array(); else if (name.equals("no3darray")) return new No3darray(); else if (name.equals("linkedlist")) return new VisLinkedList(); else if (name.equals("no3dlinkedlist")) return new No3dLinkedList(); else if (name.equals("linkedlistnonull")) return new VisLinkedListNoNull(); else if (name.equals("bargraph")) return new BarScat("BAR"); else if (name.equals("scattergraph")) return new BarScat( "SCAT"); // not implemented in xml dtd, and does not have a load-from-xml method defined else if (name.equals("stack")) return new Stack(); else if (name.equals("queue")) return new Queue(); else if (name.equals("tree")) { if (structure.getChild("binary_node") != null) return new BinaryTree(); else return new GeneralTree(); } else if (name.equals("text")) return new TextStructure(); // if the XML element name is different from your structure's name, you can do something like // this: // else if( name.equalsIgnoreCase("node")) // return new Node(); else if (name.equals("legend")) return new LegendofColors(); else { // try dynamic typing try { return assignStructureType(name); } catch (Exception e) { throw new VisualizerLoadException( "Unable to instantiate class \"" + name + "\":\n" + e.getMessage()); } } }