/java/src/com/google/gdata/data/extensions/OriginalEvent.java

http://gdata-java-client.googlecode.com/ · Java · 149 lines · 98 code · 27 blank · 24 comment · 16 complexity · 23ab44833530469ae88796bc6244c019 MD5 · raw file

  1. /* Copyright (c) 2008 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package com.google.gdata.data.extensions;
  16. import com.google.gdata.util.common.xml.XmlWriter;
  17. import com.google.gdata.client.CoreErrorDomain;
  18. import com.google.gdata.data.Extension;
  19. import com.google.gdata.data.ExtensionPoint;
  20. import com.google.gdata.data.ExtensionDescription;
  21. import com.google.gdata.data.ExtensionProfile;
  22. import com.google.gdata.util.Namespaces;
  23. import com.google.gdata.util.ParseException;
  24. import com.google.gdata.util.XmlParser;
  25. import org.xml.sax.Attributes;
  26. import java.io.IOException;
  27. import java.util.ArrayList;
  28. /**
  29. * GData schema extension describing a link to a recurring event.
  30. *
  31. *
  32. */
  33. public class OriginalEvent extends ExtensionPoint implements Extension {
  34. /** URL of the original recurring event entry. */
  35. protected String href;
  36. public String getHref() { return href; }
  37. public void setHref(String v) { href = v; }
  38. /** Event ID of the original recurring event entry. */
  39. protected String originalId;
  40. public String getOriginalId() { return originalId; }
  41. public void setOriginalId(String v) { originalId = v; }
  42. protected When originalStartTime;
  43. public When getOriginalStartTime() { return originalStartTime; }
  44. public void setOriginalStartTime(When v) { originalStartTime = v; }
  45. /** Returns the suggested extension description. */
  46. public static ExtensionDescription getDefaultDescription() {
  47. ExtensionDescription desc = new ExtensionDescription();
  48. desc.setExtensionClass(OriginalEvent.class);
  49. desc.setNamespace(Namespaces.gNs);
  50. desc.setLocalName("originalEvent");
  51. desc.setRepeatable(false);
  52. return desc;
  53. }
  54. @Override
  55. public void generate(XmlWriter w, ExtensionProfile extProfile)
  56. throws IOException {
  57. ArrayList<XmlWriter.Attribute> attrs = new ArrayList<XmlWriter.Attribute>();
  58. if (originalId != null) {
  59. attrs.add(new XmlWriter.Attribute("id", originalId));
  60. }
  61. if (href != null) {
  62. attrs.add(new XmlWriter.Attribute("href", href));
  63. }
  64. generateStartElement(w, Namespaces.gNs, "originalEvent", attrs, null);
  65. if (originalStartTime != null) {
  66. originalStartTime.generate(w, extProfile);
  67. }
  68. // Invoke ExtensionPoint.
  69. generateExtensions(w, extProfile);
  70. w.endElement(Namespaces.gNs, "originalEvent");
  71. }
  72. @Override
  73. public XmlParser.ElementHandler getHandler(ExtensionProfile extProfile,
  74. String namespace,
  75. String localName,
  76. Attributes attrs) {
  77. return new Handler(extProfile);
  78. }
  79. /** <g:originalEvent> parser. */
  80. private class Handler extends ExtensionPoint.ExtensionHandler {
  81. public Handler(ExtensionProfile extProfile) {
  82. super(extProfile, RecurrenceException.class);
  83. }
  84. @Override
  85. public void processAttribute(String namespace,
  86. String localName,
  87. String value) {
  88. if (namespace.equals("")) {
  89. if (localName.equals("id")) {
  90. originalId = value;
  91. } else if (localName.equals("href")) {
  92. href = value;
  93. }
  94. }
  95. }
  96. @Override
  97. public XmlParser.ElementHandler getChildHandler(String namespace,
  98. String localName,
  99. Attributes attrs)
  100. throws ParseException, IOException {
  101. if (namespace.equals(Namespaces.g)) {
  102. if (localName.equals("when")) {
  103. originalStartTime = new When();
  104. return originalStartTime.getHandler(extProfile, namespace, localName, attrs);
  105. }
  106. }
  107. return super.getChildHandler(namespace, localName, attrs);
  108. }
  109. @Override
  110. public void processEndElement() throws ParseException {
  111. if (originalId == null) {
  112. throw new ParseException(
  113. CoreErrorDomain.ERR.idRequired);
  114. }
  115. if (originalStartTime == null) {
  116. throw new ParseException(
  117. CoreErrorDomain.ERR.whenRequired);
  118. }
  119. super.processEndElement();
  120. }
  121. }
  122. }