/** * For Ajax Editing : swap element with sibling ([up] and [down] links). * * @param dbms * @param session * @param id * @param ref * @param down * @throws Exception */ public synchronized void swapElementEmbedded( Dbms dbms, UserSession session, String id, String ref, boolean down) throws Exception { dataManager.getMetadataSchema(dbms, id); // --- get metadata from session Element md = getMetadataFromSession(session, id); // --- get element to swap EditLib editLib = dataManager.getEditLib(); Element elSwap = editLib.findElement(md, ref); if (elSwap == null) throw new IllegalStateException(MSG_ELEMENT_NOT_FOUND_AT_REF + ref); // --- swap the elements int iSwapIndex = -1; List list = ((Element) elSwap.getParent()).getChildren(elSwap.getName(), elSwap.getNamespace()); for (int i = 0; i < list.size(); i++) if (list.get(i) == elSwap) { iSwapIndex = i; break; } if (iSwapIndex == -1) throw new IllegalStateException("Index not found for element --> " + elSwap); if (down) swapElements(elSwap, (Element) list.get(iSwapIndex + 1)); else swapElements(elSwap, (Element) list.get(iSwapIndex - 1)); // --- store the metadata in the session again setMetadataIntoSession(session, (Element) md.clone(), id); }
public boolean read(String strRoute, String strElement, int flag) { //SAXBuilder builder=new SAXBuilder(); strText = null; try { String[] route = new String[4]; String str = null; Document doc = builder.build(xmlFileName); Element root = doc.getRootElement(); Element element = root; //创建一个拆分字符串内容的对象,每次返回一项 StringTokenizer st = new StringTokenizer(strRoute, ":"); str = st.nextToken(); while (st.hasMoreTokens()) { str = st.nextToken(); element = element.getChild(str); } //mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,str); element = (Element) element.getParent(); /* * while(flag!=1) { if(element.removeChild(str)) * mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"deleted "+str); * else mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"not * deleted"); * * flag--; } */ // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,element.getName()); strText = element.getChild(str).getChild(strElement).getText(); } catch (JDOMException jdome) { mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, xmlFileName + " is not well-formed"); } catch (IOException ioe) { mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, ioe); } catch (NullPointerException nullpe) { mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "not founded" + "\n" + nullpe); } catch (Exception e) { mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "read no succeed" + "\n" + e); } if (strText == null) { mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "not founded"); return false; } else { // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"strText="+strText); return true; } }
/** {@inheritDoc } */ @Override public JdomRepresentation remove(String nodeName) throws XmlException { Element candidate = getElement(nodeName); assert candidate != null : "getElement should have throw an execption"; candidate.getParent().removeContent(candidate); return this; }
protected void generateForeignMarkup(Element e, List foreignMarkup) { if (foreignMarkup != null) { Iterator elems = (Iterator) foreignMarkup.iterator(); while (elems.hasNext()) { Element elem = (Element) elems.next(); Parent parent = elem.getParent(); if (parent != null) { parent.removeContent(elem); } e.addContent(elem); } } }
@Override public void generate(Module module, Element element) { if (!(module instanceof SSEModule)) { return; } SSEModule sseModule = (SSEModule) module; if (sseModule instanceof Sharing) { Sharing sharing = (Sharing) sseModule; // add sse namespace Element root = element; while ((root.getParent() != null) && root.getParent() instanceof Element) { root = (Element) root.getParent(); } root.addNamespaceDeclaration(SSEModule.SSE_NS); generateSharing(sharing, root); } else if (sseModule instanceof Sync) { generateSync((Sync) sseModule, element); } }
@Override protected void setUp() throws Exception { final Document document = new SAXBuilder().build(new File(getTestDataRoot(), "/RETest.xml")); final List<Element> list = XPath.selectNodes(document.getRootElement(), "//test"); new File(getTestDataPath()).mkdirs(); int i = 0; for (Element element : list) { final String name; final Element parent = (Element) element.getParent(); final String s = parent.getName(); final String t = parent.getAttribute("id") == null ? "" : parent.getAttribute("id").getValue() + "-"; if (!"tests".equals(s)) { name = s + "/test-" + t + ++i + ".regexp"; } else { name = "test-" + t + ++i + ".regexp"; } final Result result = Result.valueOf((String) XPath.selectSingleNode(element, "string(expected)")); final boolean warn = !"false".equals(element.getAttributeValue("warning")); final boolean info = "true".equals(element.getAttributeValue("info")); myMap.put(name, new Test(result, warn, info)); final File file = new File(getTestDataPath(), name); file.getParentFile().mkdirs(); final FileWriter stream = new FileWriter(file); final String pattern = (String) XPath.selectSingleNode(element, "string(pattern)"); if (!"false".equals(element.getAttributeValue("verify"))) try { Pattern.compile(pattern); if (result == Result.ERR) { System.out.println("Incorrect FAIL value for " + pattern); } } catch (PatternSyntaxException e) { if (result == Result.OK) { System.out.println("Incorrect OK value for " + pattern); } } stream.write(pattern); stream.close(); } super.setUp(); myOut = new ByteArrayOutputStream(); System.setErr(new PrintStream(myOut)); }
/** * Indicates the end of an element (<code></[element name]></code>) is reached. Note that * the parser does not distinguish between empty elements and non-empty elements, so this will * occur uniformly. * * @param namespaceURI <code>String</code> URI of namespace this element is associated with * @param localName <code>String</code> name of element without prefix * @param qName <code>String</code> name of element in XML 1.0 form * @throws SAXException when things go wrong */ public void endElement(String namespaceURI, String localName, String qName) throws SAXException { if (suppress) return; flushCharacters(); if (!atRoot) { Parent p = currentElement.getParent(); if (p instanceof Document) { atRoot = true; } else { currentElement = (Element) p; } } else { throw new SAXException("Ill-formed XML document (missing opening tag for " + localName + ")"); } }
/** * Detects whether the given element matches the given XPath string. * * @param element The element which will be checked * @param xPathString The XPath expression the element will be checked against * @return True if the given element matches the given XPath string * @throws AbstractXmlMergeException If an error occurred during the matching process */ private boolean matches(Element element, String xPathString) throws AbstractXmlMergeException { if (element == null) { return false; } try { JDOMXPath xPath = new JDOMXPath(xPathString); boolean result = xPath.selectNodes(element.getParent()).contains(element); return result; } catch (JaxenException e) { throw new MatchException(element, e); } }
public void addMessage(SAXParseException exception, String typeOfError) { if (errorCount == 0) xpaths = new Element("xsderrors", ns); errorCount++; Element elem = (Element) so.getLocator().getNode(); Element x = new Element("xpath", ns); try { String xpath = jeeves.utils.XPath.getXPath(elem); // -- remove the first element to ensure XPath fits XML passed with // -- root element if (xpath.startsWith("/")) { int ind = xpath.indexOf('/', 1); if (ind != -1) { xpath = xpath.substring(ind + 1); } else { xpath = "."; // error to be placed on the root element } } x.setText(xpath); } catch (JDOMException e) { e.printStackTrace(); x.setText("nopath"); } String message = exception.getMessage() + " (Element: " + elem.getQualifiedName(); String parentName; if (!elem.isRootElement()) { Element parent = (Element) elem.getParent(); if (parent != null) parentName = parent.getQualifiedName(); else parentName = "Unknown"; } else { parentName = "/"; } message += " with parent element: " + parentName + ")"; Element m = new Element("message", ns).setText(message); Element errorType = new Element("typeOfError", ns).setText(typeOfError); Element errorNumber = new Element("errorNumber", ns).setText(String.valueOf(errorCount)); Element e = new Element("error", ns); e.addContent(errorType); e.addContent(errorNumber); e.addContent(m); e.addContent(x); xpaths.addContent(e); }
private final void breakRepeated( final P parent, final List<C> children, org.jdom.Element element, final int indexOfElement, int firstIndex, int repeat, boolean before) { if (repeat > 0) { final org.jdom.Element newElem = (org.jdom.Element) element.clone(); assert element.getParent().getContent(indexOfElement) == element; element.getParentElement().addContent(indexOfElement + (before ? 0 : 1), newElem); setRepeated(newElem, this.attrName, repeat); final C preCell = this.create(newElem, parent, firstIndex, false); for (int i = 0; i < repeat; i++) { children.set(firstIndex + i, preCell); } } }
/** * 新增循环节点 * @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; }
public final void breakRepeated(final P parent, final List<C> children, final int col) { final C c = children.get(col); final org.jdom.Element element = c.getElement(); final String repeatedS = element.getAttributeValue(this.attrName, element.getNamespace()); if (repeatedS != null) { final int repeated = Integer.parseInt(repeatedS); // since for repeated elements, the same instance is at multiple index final int firstIndex = children.indexOf(c); final int lastIndex = firstIndex + repeated - 1; assert children.get(lastIndex) == c; // not the same as firstIndex as they're can be repeated elements before final int indexOfElement = element.getParent().indexOf(element); final int preRepeated = col - firstIndex; final int postRepeated = lastIndex - col; // start from the end to avoid changing indexOfElement breakRepeated(parent, children, element, indexOfElement, col + 1, postRepeated, false); element.removeAttribute(this.attrName, element.getNamespace()); breakRepeated(parent, children, element, indexOfElement, firstIndex, preRepeated, true); } children.set(col, this.create(element, parent, col, true)); }
//改写XML文件的某一元素的值 //strRoute为XML文件从根元素开始到该元素的父元素的路径 //strElement为需要读取的元素 //flag为当XML含有多个同名元素时,需要改写元素所处的序号 public void write(String strRoute, String strElement, String strSet, int flag) { //SAXBuilder builder=new SAXBuilder(); try { String str = null; Document doc = builder.build(xmlFileName); Element root = doc.getRootElement(); Element element = root; StringTokenizer st = new StringTokenizer(strRoute, ":"); str = st.nextToken(); while (st.hasMoreTokens()) { str = st.nextToken(); //mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,str); element = element.getChild(str); } // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"test :"+str); element = (Element) element.getParent(); //若需要改写的不是XML文件的第一个同名元素,则获取该元素 //方法为:将之前的元素依次移到后面,需要改写的元素前移至第一个 /* * if(flag>1) { int j=flag; while(j!=1) { * * Element tmp=element.getChild(str); * element.addContent(tmp.detach()); j--; } } */ element = element.getChild(str).getChild(strElement); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"gettxt // "+element.getText()); element.setText(strSet); //若需要改写的不是XML文件的第一个同名元素 //上面改变了次序,需要在成功改写后恢复原有次序 /* * if(flag!=1) { * * java.util.List children=element.getChildren(); Iterator * iterator=children.iterator(); int count=0; * while(iterator.hasNext()) { Element * child=(Element)iterator.next(); count++; } * * System.out.println("count"+count); * * k=(count+1-flag)%count; * * while(k!=0) { Element tmp=element.getChild(str); * element.addContent(tmp.detach()); k--; } } * */ XMLOutputter outputter = new XMLOutputter("", false, "GB2312"); PrintWriter out = new PrintWriter(new BufferedWriter( new FileWriter(xmlFileName))); Document myDocument = root.getDocument(); outputter.output(myDocument, out); out.close(); } catch (JDOMException jdome) { jdome.printStackTrace(); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, xmlFileName // + " is not well-formed\n"); } catch (IOException ioe) { ioe.printStackTrace(); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, ioe); } catch (Exception e) { e.printStackTrace(); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, // "write not succeed\n"); } }
public void endElement(String namespaceURI, String localName, String qName) { // end element reached.. just go up one parent curElem = (Element) curElem.getParent(); }