/** * Pushes an element onto the tree under construction. Allows subclasses to put content under a * dummy root element which is useful for building content that would otherwise be a non-well * formed document. * * @param element root element under which content will be built */ protected void pushElement(Element element) { if (atRoot) { document.setRootElement(element); // XXX should we use a factory call? atRoot = false; } else { factory.addContent(currentElement, element); } currentElement = element; }
@Override public Representation fetch(String nodeName) { Element elt = this.getElement(nodeName); elt = (Element) ((Element) elt.clone()); // elt = (Element)elt.detach(); Document doc = new Document(); doc.setRootElement(elt); return new JdomRepresentation(doc); }
public void endElement(String namespaceURI, String localName, String qualifiedName) { // If we're immediately inside a skipped element // we need to reset current to null, not to the parent current = (ParentNode) parents.remove(parents.size() - 1); flushText(); parent = current.getParent(); if (parent.isDocument()) { // root element Document doc = (Document) parent; doc.setRootElement((Element) current); } }
/** * This reports the occurrence of an actual element. It will include the element's attributes, * with the exception of XML vocabulary specific attributes, such as <code> * xmlns:[namespace prefix]</code> and <code>xsi:schemaLocation</code>. * * @param namespaceURI <code>String</code> namespace URI this element is associated with, or an * empty <code>String</code> * @param localName <code>String</code> name of element (with no namespace prefix, if one is * present) * @param qName <code>String</code> XML 1.0 version of element name: [namespace * prefix]:[localName] * @param atts <code>Attributes</code> list for this element * @throws SAXException when things go wrong */ public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (suppress) return; Element element = null; if ((namespaceURI != null) && (!namespaceURI.equals(""))) { String prefix = ""; // Determine any prefix on the Element if (!qName.equals(localName)) { int split = qName.indexOf(":"); prefix = qName.substring(0, split); } Namespace elementNamespace = Namespace.getNamespace(prefix, namespaceURI); element = factory.element(localName, elementNamespace); } else { element = factory.element(localName); } // Take leftover declared namespaces and add them to this element's // map of namespaces if (declaredNamespaces.size() > 0) { transferNamespaces(element); } // Handle attributes for (int i = 0, len = atts.getLength(); i < len; i++) { Attribute attribute = null; String attLocalName = atts.getLocalName(i); String attQName = atts.getQName(i); int attType = getAttributeType(atts.getType(i)); // Bypass any xmlns attributes which might appear, as we got // them already in startPrefixMapping(). // This is sometimes necessary when SAXHandler is used with // another source than SAXBuilder, as with JDOMResult. if (attQName.startsWith("xmlns:") || attQName.equals("xmlns")) { continue; } // First clause per http://markmail.org/message/2p245ggcjst27xe6 // patch from Mattias Jiderhamn if ("".equals(attLocalName) && attQName.indexOf(":") == -1) { attribute = factory.attribute(attQName, atts.getValue(i), attType); } else if (!attQName.equals(attLocalName)) { String attPrefix = attQName.substring(0, attQName.indexOf(":")); Namespace attNs = Namespace.getNamespace(attPrefix, atts.getURI(i)); attribute = factory.attribute(attLocalName, atts.getValue(i), attType, attNs); } else { attribute = factory.attribute(attLocalName, atts.getValue(i), attType); } factory.setAttribute(element, attribute); } flushCharacters(); if (atRoot) { document.setRootElement(element); // XXX should we use a factory call? atRoot = false; } else { factory.addContent(getCurrentElement(), element); } currentElement = element; }
private void construeixModel(ParcAtraccions pParcAtraccions) throws ParcAtraccionsExcepcio { // Mètode on heu de construir el document XML Element raiz = new Element("parcAtraccions"); raiz.addAttribute(new Attribute("codi", pParcAtraccions.getCodi().toString())); raiz.addAttribute(new Attribute("nom", pParcAtraccions.getNom())); raiz.addAttribute(new Attribute("adreca", pParcAtraccions.getAdreca())); Element coordinadores = new Element("coordinadores"); raiz.appendChild(coordinadores); Element personasMantenimiento = new Element("personasMantenimiento"); raiz.appendChild(personasMantenimiento); Element atracciones = new Element("atracciones"); raiz.appendChild(atracciones); Element zonas = new Element("zonas"); raiz.appendChild(zonas); for (int i = 0; i < pParcAtraccions.getComptaElements(); i++) { Element elemento; if (pParcAtraccions.getElements()[i] instanceof Atraccio) { elemento = new Element("atraccion"); Element nombre = new Element("nombre"); nombre.appendChild(((Atraccio) pParcAtraccions.getElements()[i]).getNom()); elemento.appendChild(nombre); Element tipus = new Element("tipus"); tipus.appendChild(((Atraccio) pParcAtraccions.getElements()[i]).getTipus()); elemento.appendChild(tipus); Element restriccionEdad = new Element("restriccionEdad"); restriccionEdad.appendChild( String.valueOf(((Atraccio) pParcAtraccions.getElements()[i]).getRestriccioEdat())); elemento.appendChild(restriccionEdad); Element restriccionAltura = new Element("restriccionAltura"); restriccionAltura.appendChild( String.valueOf(((Atraccio) pParcAtraccions.getElements()[i]).getRestriccioAlcada())); elemento.appendChild(restriccionAltura); Element tieneProblema = new Element("tieneProblema"); tieneProblema.appendChild( String.valueOf(((Atraccio) pParcAtraccions.getElements()[i]).getTeProblema())); elemento.appendChild(tieneProblema); Element codigoProblema = new Element("codigoProblema"); codigoProblema.appendChild( String.valueOf(((Atraccio) pParcAtraccions.getElements()[i]).getCodiProblema())); elemento.appendChild(codigoProblema); Element estaSolucionado = new Element("estaSolucionado"); estaSolucionado.appendChild( String.valueOf(((Atraccio) pParcAtraccions.getElements()[i]).getEstaSolucionat())); elemento.appendChild(estaSolucionado); atracciones.appendChild(elemento); continue; } else if (pParcAtraccions.getElements()[i] instanceof Zona) { Element raiz2 = new Element("zona"); raiz2.addAttribute(new Attribute("nombre", pParcAtraccions.getCodi().toString())); Element coordinadores2 = new Element("coordinadores"); raiz2.appendChild(coordinadores2); Element personasMantenimiento2 = new Element("personasMantenimiento"); raiz2.appendChild(personasMantenimiento2); Element atracciones2 = new Element("atracciones"); raiz2.appendChild(atracciones2); for (int j = 0; j < ((Zona) pParcAtraccions.getElements()[i]).getComptaElements(); j++) { Element elemento2; if (pParcAtraccions.getElements()[j] instanceof Atraccio) { elemento2 = new Element("atraccion"); Element nombre = new Element("nombre"); nombre.appendChild(((Atraccio) pParcAtraccions.getElements()[j]).getNom()); elemento2.appendChild(nombre); Element tipus = new Element("tipus"); tipus.appendChild(((Atraccio) pParcAtraccions.getElements()[j]).getTipus()); elemento2.appendChild(tipus); Element restriccionEdad = new Element("restriccionEdad"); restriccionEdad.appendChild( String.valueOf(((Atraccio) pParcAtraccions.getElements()[j]).getRestriccioEdat())); elemento2.appendChild(restriccionEdad); Element restriccionAltura = new Element("restriccionAltura"); restriccionAltura.appendChild( String.valueOf( ((Atraccio) pParcAtraccions.getElements()[j]).getRestriccioAlcada())); elemento2.appendChild(restriccionAltura); Element tieneProblema = new Element("tieneProblema"); tieneProblema.appendChild( String.valueOf(((Atraccio) pParcAtraccions.getElements()[j]).getTeProblema())); elemento2.appendChild(tieneProblema); Element codigoProblema = new Element("codigoProblema"); codigoProblema.appendChild( String.valueOf(((Atraccio) pParcAtraccions.getElements()[j]).getCodiProblema())); elemento2.appendChild(codigoProblema); Element estaSolucionado = new Element("estaSolucionado"); estaSolucionado.appendChild( String.valueOf(((Atraccio) pParcAtraccions.getElements()[j]).getEstaSolucionat())); elemento2.appendChild(estaSolucionado); atracciones2.appendChild(elemento2); continue; } if (pParcAtraccions.getElements()[i] instanceof Coordinador) { elemento2 = new Element("coordinador"); coordinadores2.appendChild(elemento2); } else { elemento2 = new Element("personaManteniment"); personasMantenimiento2.appendChild(elemento2); } Element nif = new Element("nif"); nif.appendChild(((Persona) pParcAtraccions.getElements()[i]).getNif()); elemento2.appendChild(nif); Element nom = new Element("nom"); nom.appendChild(((Persona) pParcAtraccions.getElements()[i]).getNom()); elemento2.appendChild(nom); Element cognom = new Element("cognom"); cognom.appendChild(((Persona) pParcAtraccions.getElements()[i]).getCognom()); elemento2.appendChild(cognom); } zonas.appendChild(raiz2); } else if (pParcAtraccions.getElements()[i] instanceof Coordinador) { elemento = new Element("coordinador"); coordinadores.appendChild(elemento); } else if (pParcAtraccions.getElements()[i] instanceof PersonaManteniment) { elemento = new Element("personaManteniment"); personasMantenimiento.appendChild(elemento); } /* Element nif = new Element("nif"); nif.appendChild(((Persona)pParcAtraccions.getElements()[i]).getNif()); elemento.appendChild(nif); Element nom = new Element("nom"); nom.appendChild(((Persona)pParcAtraccions.getElements()[i]).getNom()); elemento.appendChild(nom); Element cognom = new Element("cognom"); cognom.appendChild(((Persona)pParcAtraccions.getElements()[i]).getCognom()); elemento.appendChild(cognom); */ } doc.setRootElement(raiz); // System.out.println(doc.toXML()); }
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()); } }