Example #1
0
 @Override
 public void parse(final String id) throws SAXException {
   try {
     contentHandler.startDocument();
     serialize(item);
     contentHandler.endDocument();
   } catch (final Exception ex) {
     throw new SAXException(ex);
   }
 }
Example #2
0
 /**
  * This method is always the second method of all callbacks in all handlers to be invoked
  * (setDocumentLocator is always first).
  */
 private void startDocument() throws JDOMException {
   try {
     contentHandler.startDocument();
   } catch (SAXException se) {
     throw new JDOMException("Exception in startDocument", se);
   }
 }
Example #3
0
 /**
  * This will invoke the <code>endPrefixMapping</code> callback in the <code>ContentHandler</code>
  * when a namespace is goes out of scope in the <code>Document</code>.
  *
  * @param namespace Namespace leaving scope.
  */
 private void endPrefixMapping(Namespace namespace) throws JDOMException {
   try {
     contentHandler.endPrefixMapping(namespace.getPrefix());
   } catch (SAXException se) {
     throw new JDOMException("Exception in endPrefixMapping", se);
   }
 }
Example #4
0
  /**
   * 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);
    }
  }
Example #5
0
 @Override
 protected void pi(final byte[] name, final byte[] value) throws IOException {
   try {
     contentHandler.processingInstruction(string(name), string(value));
   } catch (final SAXException ex) {
     throw new IOException(ex);
   }
 }
Example #6
0
 /**
  * This will be called for each chunk of character data encountered.
  *
  * @param elementText all text in an element, including whitespace.
  */
 private void characters(String elementText) throws JDOMException {
   char[] c = elementText.toCharArray();
   try {
     contentHandler.characters(c, 0, c.length);
   } catch (SAXException se) {
     throw new JDOMException("Exception in characters", se);
   }
 }
Example #7
0
  /** Always the last method of all callbacks in all handlers to be invoked. */
  private void endDocument() throws JDOMException {
    try {
      contentHandler.endDocument();

      // reset locator
      locator = null;
    } catch (SAXException se) {
      throw new JDOMException("Exception in endDocument", se);
    }
  }
Example #8
0
 @Override
 protected void text(final byte[] value, final FTPos ftp) throws IOException {
   try {
     final String s = string(value);
     final char[] c = s.toCharArray();
     contentHandler.characters(c, 0, c.length);
   } catch (final SAXException ex) {
     throw new IOException(ex);
   }
 }
Example #9
0
 /**
  * This will invoke the <code>ContentHandler.skippedEntity</code> callback when an entity
  * reference is encountered.
  *
  * @param entity <code>EntityRef</code>.
  */
 private void entityRef(EntityRef entity) throws JDOMException {
   if (entity != null) {
     try {
       // No need to worry about appending a '%' character as
       // we do not support parameter entities
       contentHandler.skippedEntity(entity.getName());
     } catch (SAXException se) {
       throw new JDOMException("Exception in entityRef", se);
     }
   }
 }
Example #10
0
  /**
   * 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);
    }
  }
Example #11
0
 /**
  * This will invoke the <code>ContentHandler.processingInstruction</code> callback when a
  * processing instruction is encountered.
  *
  * @param pi <code>ProcessingInstruction</code> containing target and data.
  */
 private void processingInstruction(ProcessingInstruction pi) throws JDOMException {
   if (pi != null) {
     String target = pi.getTarget();
     String data = pi.getData();
     try {
       contentHandler.processingInstruction(target, data);
     } catch (SAXException se) {
       throw new JDOMException("Exception in processingInstruction", se);
     }
   }
 }
Example #12
0
 /**
  * This will invoke the <code>endPrefixMapping</code> callback in the <code>ContentHandler</code>
  * when a namespace is goes out of scope in the <code>Document</code>.
  *
  * @param namespaces <code>List</code> stack of Namespaces in scope.
  * @param previouslyDeclaredNamespaces number of previously declared namespaces
  */
 private void endPrefixMapping(NamespaceStack namespaces, int previouslyDeclaredNamespaces)
     throws JDOMException {
   while (namespaces.size() > previouslyDeclaredNamespaces) {
     String prefix = namespaces.pop();
     try {
       contentHandler.endPrefixMapping(prefix);
     } catch (SAXException se) {
       throw new JDOMException("Exception in endPrefixMapping", se);
     }
   }
 }
Example #13
0
 @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);
   }
 }
Example #14
0
  /**
   * This will invoke the <code>ContentHandler.startPrefixMapping</code> callback when a new
   * namespace is encountered in the <code>Document</code>.
   *
   * @param element <code>Element</code> used in callbacks.
   * @param namespaces <code>List</code> stack of Namespaces in scope.
   * @return <code>Attributes</code> declaring the namespaces local to <code>element</code> or
   *     <code>null</code>.
   */
  private Attributes startPrefixMapping(Element element, NamespaceStack namespaces)
      throws JDOMException {
    AttributesImpl nsAtts = null; // The namespaces as xmlns attributes

    Namespace ns = element.getNamespace();
    if (ns != Namespace.XML_NAMESPACE) {
      String prefix = ns.getPrefix();
      String uri = namespaces.getURI(prefix);
      if (!ns.getURI().equals(uri)) {
        namespaces.push(ns);
        nsAtts = this.addNsAttribute(nsAtts, ns);
        try {
          contentHandler.startPrefixMapping(prefix, ns.getURI());
        } catch (SAXException se) {
          throw new JDOMException("Exception in startPrefixMapping", se);
        }
      }
    }

    // Fire additional namespace declarations
    List additionalNamespaces = element.getAdditionalNamespaces();
    if (additionalNamespaces != null) {
      Iterator itr = additionalNamespaces.iterator();
      while (itr.hasNext()) {
        ns = (Namespace) itr.next();
        String prefix = ns.getPrefix();
        String uri = namespaces.getURI(prefix);
        if (!ns.getURI().equals(uri)) {
          namespaces.push(ns);
          nsAtts = this.addNsAttribute(nsAtts, ns);
          try {
            contentHandler.startPrefixMapping(prefix, ns.getURI());
          } catch (SAXException se) {
            throw new JDOMException("Exception in startPrefixMapping", se);
          }
        }
      }
    }
    return nsAtts;
  }
Example #15
0
  /**
   * This method tells you the line of the XML file being parsed. For an in-memory document, it's
   * meaningless. The location is only valid for the current parsing lifecycle, but the document has
   * already been parsed. Therefore, it returns -1 for both line and column numbers.
   *
   * @param document JDOM <code>Document</code>.
   */
  private void documentLocator(Document document) {
    locator = new JDOMLocator();
    String publicID = null;
    String systemID = null;

    if (document != null) {
      DocType docType = document.getDocType();
      if (docType != null) {
        publicID = docType.getPublicID();
        systemID = docType.getSystemID();
      }
    }
    locator.setPublicId(publicID);
    locator.setSystemId(systemID);
    locator.setLineNumber(-1);
    locator.setColumnNumber(-1);

    contentHandler.setDocumentLocator(locator);
  }
Example #16
0
  @Override
  protected void finishOpen() throws IOException {
    try {
      final AttributesImpl attrs = new AttributesImpl();
      final int as = attributes.size();
      for (int a = 0; a < as; a++) {
        final byte[] name = attributes.name(a);
        final String uri = string(namespaces.get(prefix(name)));
        final String lname = string(local(name));
        final String rname = string(name);
        final String value = string(attributes.value(a));
        attrs.addAttribute(uri, lname, rname, null, value);
      }

      final String uri = string(namespaces.get(elem.prefix()));
      final String lname = string(elem.local());
      final String rname = string(elem.string());
      contentHandler.startElement(uri, lname, rname, attrs);

    } catch (final SAXException ex) {
      throw new IOException(ex);
    }
  }
 public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
   if (ch != null) {
     ch.endElement(namespaceURI, localName, qName);
   }
 }
 public void endPrefixMapping(String prefix) throws SAXException {
   if (ch != null) {
     ch.endPrefixMapping(prefix);
   }
 }
 public void processingInstruction(String target, String data) throws SAXException {
   if (ch != null) {
     ch.processingInstruction(target, data);
   }
 }
 public void startPrefixMapping(String prefix, String uri) throws SAXException {
   if (ch != null) {
     ch.startPrefixMapping(prefix, uri);
   }
 }
 public void skippedEntity(String name) throws SAXException {
   if (ch != null) {
     ch.skippedEntity(name);
   }
 }
 public void startDocument() throws SAXException {
   if (ch != null) {
     ch.startDocument();
   }
 }
Example #23
0
  /**
   * 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 endDocument() throws SAXException {
   if (ch != null) {
     ch.endDocument();
   }
 }
 public void setDocumentLocator(Locator locator) {
   if (ch != null) {
     ch.setDocumentLocator(locator);
   }
 }
Example #26
0
  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();
  }
 public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
     throws SAXException {
   if (ch != null) {
     ch.startElement(namespaceURI, localName, qName, atts);
   }
 }