/** * Receive notification of the end of an element. * * @param namespaceURI the namespace URI, can be <code>null</code>. * @param localName the local name (without prefix); cannot be <code>null</code>. * @param qName the qualified name (with prefix), can be <code>null</code> since <code> * namespaceURI</code> and <code>localName</code> are only used. * @throws IllegalArgumentException if <code>localName == null</code>. */ public void endElement(String namespaceURI, String localName, String qName) throws IllegalArgumentException { // Temporarily enter ERROR state, on success this state is left State currentState = _state; _state = ERROR; // Check preconditions MandatoryArgumentChecker.check("localName", localName); if (currentState == ERROR) { String detail = "Unexpected state " + currentState + " (level=" + _level + ')'; throw Utils.logProgrammingError(detail); // Within data section } else { // Get the Element for which we process the end tag Element child = _dataElementStack.pop(); // Add the child to the parent if (_dataElementStack.size() > 0) { Element parent = _dataElementStack.peek(); parent.add(child); // Reset the state back from ERROR to PARSING _state = PARSING; } else { _element = child; _state = FINISHED; } } _level--; }
public CSS(StringBuffer text, Document doc) { super(STYLE); addAttribute(TYPE, TEXT_CSS); addText(COMMENT_START + text.toString() + COMMENT_END); Element head = doc.getHead(); head.add(this); }
public Element definitionList(Object term, Object... content) { Element dl = element("dl", element("dt", term)); if (content != null) { for (Object item : content) { dl.add(element("dd", item)); } } return dl; }
/** Creates a new instance of CSS */ public CSS(String link, Document doc) { super(LINK); addAttribute(REL, STYLESHEET); addAttribute(HREF, link); addAttribute(TYPE, TEXT_CSS); Element head = doc.getHead(); head.add(this); }
// ===================================================== // private: setHomeBilder() // // <home> // <bilder> ..path.. </bilder> // </home> // // ===================================================== private void setHomeBilder(String path) { Element root = document.getRootElement(); // pr�fen,ob home-tag vorhanden java.util.List homeListe = PM_XML_Utils.getElementListe(document, "//" + TAG_HOME); Element homeElement = null; if (homeListe.isEmpty()) { homeElement = new org.dom4j.tree.DefaultElement(TAG_HOME); addTag(homeElement, TAG_BILDER, path); root.add(homeElement); } else { homeElement = (Element) homeListe.get(0); addTag(homeElement, TAG_BILDER, path); } }
/** * Receive notification of character data. * * @param ch the <code>char</code> array that contains the characters from the XML document, * cannot be <code>null</code>. * @param start the start index within <code>ch</code>. * @param length the number of characters to take from <code>ch</code>. * @throws IndexOutOfBoundsException if characters outside the allowed range are specified. * @throws SAXException if the parsing failed. */ public void characters(char[] ch, int start, int length) throws IndexOutOfBoundsException, SAXException { // Temporarily enter ERROR state, on success this state is left State currentState = _state; _state = ERROR; // Get the Element within which we found a text snippet Element child = _dataElementStack.peek(); // Add the text snippet child.add(new String(ch, start, length)); // Reset _state _state = currentState; }
public void setLocale(Locale locale) { // locale == null --> l�schen Element root = document.getRootElement(); // pr�fen,ob locale-tag vorhanden java.util.List localeListe = PM_XML_Utils.getElementListe(document, "//" + TAG_LOCALE); Element localeElement = null; if (localeListe.isEmpty()) { if (locale == null) { return; // soll gel�scht sein } localeElement = new org.dom4j.tree.DefaultElement(TAG_LOCALE); updateAttribute(localeElement, ATTR_LOCALE_LANGUAGE, locale.getLanguage()); root.add(localeElement); } else { localeElement = (Element) localeListe.get(0); if (locale != null) { updateAttribute(localeElement, ATTR_LOCALE_LANGUAGE, locale.getLanguage()); } else { // l�schen updateAttribute(localeElement, ATTR_LOCALE_LANGUAGE, null); } } }
@Override // Implementation methods // ------------------------------------------------------------------------- protected Document parseDocument() throws DocumentException, IOException, XmlPullParserException { DocumentFactory df = getDocumentFactory(); Document document = df.createDocument(); Element parent = null; XmlPullParser pp = getXPPParser(); pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); while (true) { int type = pp.nextToken(); switch (type) { case XmlPullParser.PROCESSING_INSTRUCTION: { String text = pp.getText(); int loc = text.indexOf(' '); if (loc >= 0) { String target = text.substring(0, loc); String txt = text.substring(loc + 1); document.addProcessingInstruction(target, txt); } else { document.addProcessingInstruction(text, ""); } break; } case XmlPullParser.COMMENT: { if (parent != null) { parent.addComment(pp.getText()); } else { document.addComment(pp.getText()); } break; } case XmlPullParser.CDSECT: { if (parent != null) { parent.addCDATA(pp.getText()); } else { String msg = "Cannot have text content outside of the " + "root document"; throw new DocumentException(msg); } break; } case XmlPullParser.END_DOCUMENT: return document; case XmlPullParser.START_TAG: { QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace()) : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace()); Element newElement = df.createElement(qname); int nsStart = pp.getNamespaceCount(pp.getDepth() - 1); int nsEnd = pp.getNamespaceCount(pp.getDepth()); for (int i = nsStart; i < nsEnd; i++) { if (pp.getNamespacePrefix(i) != null) { newElement.addNamespace(pp.getNamespacePrefix(i), pp.getNamespaceUri(i)); } } for (int i = 0; i < pp.getAttributeCount(); i++) { QName qa = (pp.getAttributePrefix(i) == null) ? df.createQName(pp.getAttributeName(i)) : df.createQName( pp.getAttributeName(i), pp.getAttributePrefix(i), pp.getAttributeNamespace(i)); newElement.addAttribute(qa, pp.getAttributeValue(i)); } if (parent != null) { parent.add(newElement); } else { document.add(newElement); } parent = newElement; break; } case XmlPullParser.END_TAG: { if (parent != null) { parent = parent.getParent(); } break; } case XmlPullParser.ENTITY_REF: case XmlPullParser.TEXT: { String text = pp.getText(); if (parent != null) { parent.addText(text); } else { String msg = "Cannot have text content outside of the " + "root document"; throw new DocumentException(msg); } break; } default: break; } } }
/** * Parses a string and search for variables as definied in pattern as a regular expression * * @param src the source string to parse * @param pattern the regular expression for scanning the src * @param idPattern regexp to extract the ID for this variable * @param parmPattern regexp to extract optional parameter * @param quotePattern regexp to remove quotes from paramter values * @param flags regex compile flags * @return True if pattern is found, False otherwise */ public boolean parse( String src, String pattern, String idPattern, String parmPattern, String quotePattern, int flags) { boolean rc = false; Pattern r = Pattern.compile(pattern, flags); Matcher m = r.matcher(src); Pattern rid = Pattern.compile(idPattern); Matcher mid = null; Pattern rparm = Pattern.compile(parmPattern); Matcher mparm = null; Pattern rquot = (quotePattern != null ? Pattern.compile(quotePattern) : null); Matcher mquot = null; int gnumber; int slen, pos; elem = null; prev = null; gnumber = m.groupCount() > 0 ? 1 : 0; slen = src.length(); for (pos = 0; pos < slen; ) { if (m.find(pos)) { int start = m.start(); if (start > pos) addElement(new Element(src.substring(pos, start))); pos = m.end(); String cbData = m.group(gnumber); String cbID; if (mid == null) mid = rid.matcher(cbData); else mid.reset(cbData); if (mid.find()) { cbID = mid.group(mid.groupCount() > 0 ? 1 : 0); start = mid.end(); } else { cbID = cbData; start = 0; } String cbParm; if (start > 0) cbParm = cbData.substring(start); else cbParm = cbData; Element tmp = new Element(cbID, cbData); if (mparm == null) mparm = rparm.matcher(cbParm); else mparm.reset(cbParm); if (mparm.groupCount() >= 2) { int cbpos = 0; int cblen = cbParm.length(); while (cbpos < cblen) if (mparm.find(cbpos)) { String value = mparm.group(2); if (rquot != null) { if (mquot == null) mquot = rquot.matcher(value); else mquot.reset(value); if (mquot.find()) value = mquot.group(mquot.groupCount() > 0 ? 1 : 0); } tmp.add(mparm.group(1), value); cbpos = mparm.end(); } else break; } addElement(tmp); rc = true; } else { addElement(new Element(src.substring(pos))); pos = slen; } } return rc; }
private static List<Node> doInsert( Node insertionNode, List<Node> clonedNodes, XFormsInstance modifiedInstance, boolean doDispatch) { final List<Node> insertedNodes = new ArrayList<Node>(clonedNodes.size()); if (insertionNode instanceof Element) { // Insert inside an element final Element insertContextElement = (Element) insertionNode; int otherNodeIndex = 0; for (Node clonedNode : clonedNodes) { if (clonedNode != null) { // NOTE: we allow passing some null nodes so we check on null if (clonedNode instanceof Attribute) { // Add attribute to element // NOTE: In XML, attributes are unordered. dom4j handles them as a list so has order, // but the // XForms spec shouldn't rely on attribute order. We could try to keep the order, but it // is harder // as we have to deal with removing duplicate attributes and find a reasonable insertion // strategy. final Attribute clonedAttribute = (Attribute) clonedNode; final Attribute existingAttribute = insertContextElement.attribute(clonedAttribute.getQName()); if (existingAttribute != null) insertContextElement.remove(existingAttribute); insertContextElement.add(clonedAttribute); if (existingAttribute != null) { // Dispatch xxforms-replace event if required and possible // NOTE: For now, still dispatch xforms-insert for backward compatibility. if (doDispatch && modifiedInstance != null) { final DocumentWrapper documentWrapper = (DocumentWrapper) modifiedInstance.documentInfo(); Dispatch.dispatchEvent( new XXFormsReplaceEvent( modifiedInstance, documentWrapper.wrap(existingAttribute), documentWrapper.wrap(clonedAttribute))); } } insertedNodes.add(clonedAttribute); } else if (!(clonedNode instanceof Document)) { // Add other node to element insertContextElement.content().add(otherNodeIndex++, clonedNode); insertedNodes.add(clonedNode); } else { // "If a cloned node cannot be placed at the target location due to a node type // conflict, then the // insertion for that particular clone node is ignored." } } } return insertedNodes; } else if (insertionNode instanceof Document) { final Document insertContextDocument = (Document) insertionNode; // "If there is more than one cloned node to insert, only the first node that does not cause a // conflict is // considered." for (Node clonedNode : clonedNodes) { // Only an element can be inserted at the root of an instance if (clonedNode instanceof Element) { final Element formerRootElement = insertContextDocument.getRootElement(); insertContextDocument.setRootElement((Element) clonedNode); // Dispatch xxforms-replace event if required and possible // NOTE: For now, still dispatch xforms-insert for backward compatibility. if (doDispatch && modifiedInstance != null) { final DocumentWrapper documentWrapper = (DocumentWrapper) modifiedInstance.documentInfo(); Dispatch.dispatchEvent( new XXFormsReplaceEvent( modifiedInstance, documentWrapper.wrap(formerRootElement), documentWrapper.wrap(insertContextDocument.getRootElement()))); } insertedNodes.add(clonedNode); return insertedNodes; } } // NOTE: The spec does not allow inserting comments and PIs at the root of an instance // document at this // point. return insertedNodes; } else { throw new OXFException( "Unsupported insertion node type: " + insertionNode.getClass().getName()); } }