/** * Generate the xml top level element and start streaming the components. * * @param components * @param contentHandler * @throws SAXException */ protected static void generateXML( Components components, ContentHandler contentHandler, boolean isScrPrivateFile) throws SAXException { // detect namespace to use final String namespace; if (components.getSpecVersion() == Constants.VERSION_1_0) { namespace = NAMESPACE_URI_1_0; } else if (components.getSpecVersion() == Constants.VERSION_1_1) { namespace = NAMESPACE_URI_1_1; } else { namespace = NAMESPACE_URI_1_1_FELIX; } contentHandler.startDocument(); contentHandler.startPrefixMapping(PREFIX, namespace); // wrapper element to generate well formed xml contentHandler.startElement( "", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS, new AttributesImpl()); IOUtils.newline(contentHandler); for (final Component component : components.getComponents()) { if (component.isDs()) { generateXML(namespace, component, contentHandler, isScrPrivateFile); } } // end wrapper element contentHandler.endElement( "", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS); IOUtils.newline(contentHandler); contentHandler.endPrefixMapping(PREFIX); contentHandler.endDocument(); }
public static void streamNullDocument(ContentHandler contentHandler) throws SAXException { contentHandler.startDocument(); contentHandler.startPrefixMapping(XMLConstants.XSI_PREFIX, XMLConstants.XSI_URI); final AttributesImpl attributes = new AttributesImpl(); attributes.addAttribute(XMLConstants.XSI_URI, "nil", "xsi:nil", "CDATA", "true"); contentHandler.startElement("", "null", "null", attributes); contentHandler.endElement("", "null", "null"); contentHandler.endPrefixMapping(XMLConstants.XSI_PREFIX); contentHandler.endDocument(); }
/** * 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); } }
@Override protected void finishClose() throws IOException { try { final String uri = string(namespaces.get(elem.prefix())); final String lname = string(elem.local()); final String rname = string(elem.string()); contentHandler.endElement(uri, lname, rname); namespaces = namespaces.getParent(); } catch (final SAXException ex) { throw new IOException(ex); } }
public void outputDelimiter(ContentHandler contentHandler, String classes, String id) throws SAXException { reusableAttributes.clear(); if (id != null) reusableAttributes.addAttribute("", "id", "id", ContentHandlerHelper.CDATA, id); if (classes != null) reusableAttributes.addAttribute("", "class", "class", ContentHandlerHelper.CDATA, classes); final String delimiterQName = XMLUtils.buildQName(delimiterPrefix, delimiterLocalName); contentHandler.startElement( delimiterNamespaceURI, delimiterLocalName, delimiterQName, reusableAttributes); contentHandler.endElement(delimiterNamespaceURI, delimiterLocalName, delimiterQName); }
/** * Write the xml for a {@link Interface}. * * @param interf * @param contentHandler * @throws SAXException */ protected static void generateXML(Interface interf, ContentHandler contentHandler) throws SAXException { final AttributesImpl ai = new AttributesImpl(); IOUtils.addAttribute(ai, "interface", interf.getInterfacename()); IOUtils.indent(contentHandler, 3); contentHandler.startElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.INTERFACE, ComponentDescriptorIO.INTERFACE_QNAME, ai); contentHandler.endElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.INTERFACE, ComponentDescriptorIO.INTERFACE_QNAME); IOUtils.newline(contentHandler); }
/** * Write the xml for a {@link Implementation}. * * @param implementation * @param contentHandler * @throws SAXException */ protected static void generateXML(Implementation implementation, ContentHandler contentHandler) throws SAXException { final AttributesImpl ai = new AttributesImpl(); IOUtils.addAttribute(ai, "class", implementation.getClassame()); IOUtils.indent(contentHandler, 2); contentHandler.startElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.IMPLEMENTATION, ComponentDescriptorIO.IMPLEMENTATION_QNAME, ai); contentHandler.endElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.IMPLEMENTATION, ComponentDescriptorIO.IMPLEMENTATION_QNAME); IOUtils.newline(contentHandler); }
/** * Write the xml for a {@link Component}. * * @param component * @param contentHandler * @throws SAXException */ protected static void generateXML( final String namespace, final Component component, final ContentHandler contentHandler, final boolean isScrPrivateFile) throws SAXException { final AttributesImpl ai = new AttributesImpl(); IOUtils.addAttribute(ai, COMPONENT_ATTR_ENABLED, component.isEnabled()); IOUtils.addAttribute(ai, COMPONENT_ATTR_IMMEDIATE, component.isImmediate()); IOUtils.addAttribute(ai, COMPONENT_ATTR_NAME, component.getName()); IOUtils.addAttribute(ai, COMPONENT_ATTR_FACTORY, component.getFactory()); // attributes new in 1.1 if (NAMESPACE_URI_1_1.equals(namespace) || NAMESPACE_URI_1_1_FELIX.equals(namespace)) { IOUtils.addAttribute(ai, COMPONENT_ATTR_POLICY, component.getConfigurationPolicy()); IOUtils.addAttribute(ai, COMPONENT_ATTR_ACTIVATE, component.getActivate()); IOUtils.addAttribute(ai, COMPONENT_ATTR_DEACTIVATE, component.getDeactivate()); IOUtils.addAttribute(ai, COMPONENT_ATTR_MODIFIED, component.getModified()); } IOUtils.indent(contentHandler, 1); contentHandler.startElement( namespace, ComponentDescriptorIO.COMPONENT, ComponentDescriptorIO.COMPONENT_QNAME, ai); IOUtils.newline(contentHandler); generateXML(component.getImplementation(), contentHandler); if (component.getService() != null) { generateXML(component.getService(), contentHandler); } if (component.getProperties() != null) { for (final Property property : component.getProperties()) { generateXML(property, contentHandler, isScrPrivateFile); } } if (component.getReferences() != null) { for (final Reference reference : component.getReferences()) { generateXML(namespace, reference, contentHandler, isScrPrivateFile); } } IOUtils.indent(contentHandler, 1); contentHandler.endElement( namespace, ComponentDescriptorIO.COMPONENT, ComponentDescriptorIO.COMPONENT_QNAME); IOUtils.newline(contentHandler); }
/** * Write the xml for a {@link Service}. * * @param service * @param contentHandler * @throws SAXException */ protected static void generateXML(Service service, ContentHandler contentHandler) throws SAXException { final AttributesImpl ai = new AttributesImpl(); IOUtils.addAttribute(ai, "servicefactory", String.valueOf(service.isServicefactory())); IOUtils.indent(contentHandler, 2); contentHandler.startElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.SERVICE, ComponentDescriptorIO.SERVICE_QNAME, ai); if (service.getInterfaces() != null && service.getInterfaces().size() > 0) { IOUtils.newline(contentHandler); for (final Interface interf : service.getInterfaces()) { generateXML(interf, contentHandler); } IOUtils.indent(contentHandler, 2); } contentHandler.endElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.SERVICE, ComponentDescriptorIO.SERVICE_QNAME); IOUtils.newline(contentHandler); }
/** * Write the xml for a {@link Property}. * * @param property * @param contentHandler * @throws SAXException */ protected static void generateXML( Property property, ContentHandler contentHandler, boolean isScrPrivateFile) throws SAXException { final AttributesImpl ai = new AttributesImpl(); IOUtils.addAttribute(ai, "name", property.getName()); IOUtils.addAttribute(ai, "type", property.getType()); IOUtils.addAttribute(ai, "value", property.getValue()); // we have to write more information if this is our scr private file if (isScrPrivateFile) { IOUtils.addAttribute(ai, "private", String.valueOf(property.isPrivate())); if (property.getLabel() != null) { IOUtils.addAttribute(ai, "label", String.valueOf(property.getLabel())); } if (property.getDescription() != null) { IOUtils.addAttribute(ai, "description", String.valueOf(property.getDescription())); } if (property.getCardinality() != null) { IOUtils.addAttribute(ai, "cardinality", String.valueOf(property.getCardinality())); } } IOUtils.indent(contentHandler, 2); contentHandler.startElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.PROPERTY, ComponentDescriptorIO.PROPERTY_QNAME, ai); if (property.getMultiValue() != null && property.getMultiValue().length > 0) { // generate a new line first IOUtils.text(contentHandler, "\n"); for (int i = 0; i < property.getMultiValue().length; i++) { IOUtils.indent(contentHandler, 3); IOUtils.text(contentHandler, property.getMultiValue()[i]); IOUtils.newline(contentHandler); } IOUtils.indent(contentHandler, 2); } contentHandler.endElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.PROPERTY, ComponentDescriptorIO.PROPERTY_QNAME); IOUtils.newline(contentHandler); }
/** * Write the xml for a {@link Reference}. * * @param reference * @param contentHandler * @throws SAXException */ protected static void generateXML( final String namespace, Reference reference, ContentHandler contentHandler, boolean isScrPrivateFile) throws SAXException { final AttributesImpl ai = new AttributesImpl(); IOUtils.addAttribute(ai, "name", reference.getName()); IOUtils.addAttribute(ai, "interface", reference.getInterfacename()); IOUtils.addAttribute(ai, "cardinality", reference.getCardinality()); IOUtils.addAttribute(ai, "policy", reference.getPolicy()); IOUtils.addAttribute(ai, "target", reference.getTarget()); IOUtils.addAttribute(ai, "bind", reference.getBind()); IOUtils.addAttribute(ai, "unbind", reference.getUnbind()); // attributes new in 1.1-felix (FELIX-1893) if (NAMESPACE_URI_1_1_FELIX.equals(namespace)) { IOUtils.addAttribute(ai, "updated", reference.getUpdated()); } if (isScrPrivateFile) { IOUtils.addAttribute(ai, "checked", String.valueOf(reference.isChecked())); IOUtils.addAttribute(ai, "strategy", reference.getStrategy()); } IOUtils.indent(contentHandler, 2); contentHandler.startElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.REFERENCE, ComponentDescriptorIO.REFERENCE_QNAME, ai); contentHandler.endElement( INNER_NAMESPACE_URI, ComponentDescriptorIO.REFERENCE, ComponentDescriptorIO.REFERENCE_QNAME); IOUtils.newline(contentHandler); }
public void endElement(String namespaceURI, String localName, String qName) throws SAXException { if (ch != null) { ch.endElement(namespaceURI, localName, qName); } }
/** * Handles the creation of new XML files into a particular directory. Files are stored based on * the user's unique userid. Each file contains the user's name, userid and resource IDs. * * @param userid * @param locator * @param name * @param address * @throws IOException * @throws SAXException */ public void createFile(String userid, String locator, String name, String address) throws IOException, SAXException { // Where to create the XML file String filename = "/home/policygrid/apache-tomcat-6.0.18/webapps/ourspaces/users/" + userid + ".xml"; // Define a new filetype File file = new File(filename); FileOutputStream fos = new FileOutputStream(filename); // Set the output of the filetype OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true); of.setIndent(1); of.setIndenting(true); XMLSerializer serializer = new XMLSerializer(fos, of); ContentHandler hd = serializer.asContentHandler(); hd.startDocument(); AttributesImpl atts = new AttributesImpl(); // Generate XML tags hd.startElement("", "", "user", null); hd.startElement("", "", "userid", null); hd.characters(userid.toCharArray(), 0, userid.length()); hd.endElement("", "", "userid"); hd.startElement("", "", "locator", null); hd.characters(locator.toCharArray(), 0, locator.length()); hd.endElement("", "", "locator"); hd.startElement("", "", "name", null); hd.characters(name.toCharArray(), 0, name.length()); hd.endElement("", "", "name"); hd.startElement("", "", "address", null); hd.characters(address.toCharArray(), 0, address.length()); hd.endElement("", "", "address"); hd.startElement("", "", "home", null); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "myresources"); atts.addAttribute("", "", "column", "CDATA", "2"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "2"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "myprojects"); atts.addAttribute("", "", "column", "CDATA", "2"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "1"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "mytags"); atts.addAttribute("", "", "column", "CDATA", "1"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "2"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "mycontacts"); atts.addAttribute("", "", "column", "CDATA", "1"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "1"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "myblog"); atts.addAttribute("", "", "column", "CDATA", "3"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "1"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "ouractivities"); atts.addAttribute("", "", "column", "CDATA", "3"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "2"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "myactivities"); atts.addAttribute("", "", "column", "CDATA", "3"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "2"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); hd.endElement("", "", "home"); hd.endElement("", "", "user"); hd.endDocument(); // Close the file fos.close(); }
public void deleteBox(int id, String original, String remove) throws ParserConfigurationException, SAXException, IOException { // Open the correct file String filename = "/home/policygrid/apache-tomcat-6.0.18/webapps/ourspaces/users/" + id + ".xml"; /* * Handle the tokenizing of the string. Remove the '&' symbol, then split into columns. */ String pattern = "&"; String newString = original.substring(0, original.length() - 1); String[] fields = newString.split("&"); ArrayList lft = new ArrayList(); ArrayList mid = new ArrayList(); ArrayList rht = new ArrayList(); for (int i = 0; i < fields.length; i++) { if (fields[i].charAt(11) == '0') { lft.add(fields[i].split("widget_col_0=")); } if (fields[i].charAt(11) == '1') { mid.add(fields[i].split("widget_col_1=")); } if (fields[i].charAt(11) == '2') { rht.add(fields[i].split("widget_col_2=")); } } for (int i = 0; i < lft.size(); i++) { String[] tmp = (String[]) lft.get(i); String name2 = (String) tmp[1]; if (name2.equals(remove)) lft.remove(i); } for (int i = 0; i < mid.size(); i++) { String[] tmp = (String[]) mid.get(i); String name2 = (String) tmp[1]; if (name2.equals(remove)) mid.remove(i); } for (int i = 0; i < rht.size(); i++) { String[] tmp = (String[]) rht.get(i); String name2 = (String) tmp[1]; if (name2.equals(remove)) rht.remove(i); } // Initialise the DOM parser DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document dom; DocumentBuilder db = dbf.newDocumentBuilder(); // Parse the file dom = db.parse(filename); Element docEle = dom.getDocumentElement(); // Retrieve the previous tags from the parsed file so they can be re-added. String userid = getTextValue(docEle, "userid"); String locator = getTextValue(docEle, "locator"); String name = getTextValue(docEle, "name"); String address = getTextValue(docEle, "address"); File file = new File(filename); FileOutputStream fos = new FileOutputStream(filename); // Set the output of the filetype OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true); of.setIndent(1); of.setIndenting(true); XMLSerializer serializer = new XMLSerializer(fos, of); ContentHandler hd = serializer.asContentHandler(); hd.startDocument(); AttributesImpl atts = new AttributesImpl(); // Generate new XML tags hd.startElement("", "", "user", null); hd.startElement("", "", "userid", null); hd.characters(userid.toCharArray(), 0, userid.length()); hd.endElement("", "", "userid"); hd.startElement("", "", "locator", null); hd.characters(locator.toCharArray(), 0, locator.length()); hd.endElement("", "", "locator"); hd.startElement("", "", "name", null); hd.characters(name.toCharArray(), 0, name.length()); hd.endElement("", "", "name"); hd.startElement("", "", "address", null); hd.characters(address.toCharArray(), 0, address.length()); hd.endElement("", "", "address"); hd.startElement("", "", "home", null); for (int j = 0; j < lft.size(); j++) { String[] tmp = (String[]) lft.get(j); String name2 = (String) tmp[1]; Integer pos = j + 1; String position = pos.toString(); atts.clear(); atts.addAttribute("", "", "name", "CDATA", getTagName(name2)); atts.addAttribute("", "", "column", "CDATA", "1"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", position); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); } for (int j = 0; j < mid.size(); j++) { String[] tmp = (String[]) mid.get(j); String name2 = (String) tmp[1]; Integer pos = j + 1; String position = pos.toString(); atts.clear(); atts.addAttribute("", "", "name", "CDATA", getTagName(name2)); atts.addAttribute("", "", "column", "CDATA", "2"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", position); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); } for (int j = 0; j < rht.size(); j++) { String[] tmp = (String[]) rht.get(j); String name2 = (String) tmp[1]; Integer pos = j + 1; String position = pos.toString(); atts.clear(); atts.addAttribute("", "", "name", "CDATA", getTagName(name2)); atts.addAttribute("", "", "column", "CDATA", "3"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", position); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); } hd.endElement("", "", "home"); hd.endElement("", "", "user"); hd.endDocument(); // Close the file fos.close(); }