Revision: 3998
Author: [email protected]
Date: Tue Mar  2 13:55:40 2010
Log: Add a well known prefix for opensocial.
http://codereview.appspot.com/189044

This is pulled out of a larger CL, so apologies for the cruft.

There are 2 major changes:
(1) Changes to Namespaces to add well-known prefixes.
(2) Changes to Nodes.render so that adding new prefixes won't change
    rendered output that doesn't use those namespaces.  This makes
    test goldens more robust.

[email protected]

http://code.google.com/p/google-caja/source/detail?r=3998

Deleted:
 /trunk/src/com/google/caja/opensocial/OpenSocialMessageType.java
Modified:
/trunk/src/com/google/caja/demos/playground/server/GWTCajolingServiceImpl.java
 /trunk/src/com/google/caja/opensocial/DefaultGadgetRewriter.java
 /trunk/src/com/google/caja/parser/html/Namespaces.java
 /trunk/src/com/google/caja/parser/html/Nodes.java
 /trunk/src/com/google/caja/plugin/PluginMessageType.java
 /trunk/tests/com/google/caja/parser/html/NodesTest.java
/trunk/tests/com/google/caja/plugin/stages/LegacyNamespaceFixupStageTest.java

=======================================
--- /trunk/src/com/google/caja/opensocial/OpenSocialMessageType.java Tue Dec 18 12:48:49 2007
+++ /dev/null
@@ -1,56 +0,0 @@
-// Copyright (C) 2006 Google Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.google.caja.opensocial;
-
-import com.google.caja.reporting.MessageContext;
-import com.google.caja.reporting.MessageLevel;
-import com.google.caja.reporting.MessagePart;
-import com.google.caja.reporting.MessageType;
-import com.google.caja.reporting.MessageTypeInt;
-
-import java.io.IOException;
-
-/**
- * Messages for the PluginCompiler
- *
- * @author [email protected]
- */
-public enum OpenSocialMessageType implements MessageTypeInt {
-  FAILED_TO_RETRIEVE_EXTERNAL_URI(
-      "%s - Failed to resolve URI %s: %s", MessageLevel.ERROR),
-  NO_CONTENT("%s: no content", MessageLevel.FATAL_ERROR),
-  ;
-
-  private final String formatString;
-  private final MessageLevel level;
-  private final int paramCount;
-
-  OpenSocialMessageType(String formatString, MessageLevel level) {
-    this.formatString = formatString;
-    this.level = level;
-    this.paramCount = MessageType.formatStringArity(formatString);
-  }
-
-  public int getParamCount() {
-    return paramCount;
-  }
-
-  public void format(MessagePart[] parts, MessageContext context,
-                     Appendable out) throws IOException {
-    MessageType.formatMessage(formatString, parts, context, out);
-  }
-
-  public MessageLevel getLevel() { return level; }
-}
=======================================
--- /trunk/src/com/google/caja/demos/playground/server/GWTCajolingServiceImpl.java Tue Mar 2 12:21:46 2010 +++ /trunk/src/com/google/caja/demos/playground/server/GWTCajolingServiceImpl.java Tue Mar 2 13:55:40 2010
@@ -9,7 +9,6 @@
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;

@@ -89,7 +88,7 @@

     Map<InputSource, ? extends CharSequence> originalSources
         = Collections.singletonMap(new InputSource(guessURI(url)), input);
-
+
     try {
       DefaultGadgetRewriter rw = new DefaultGadgetRewriter(
           BuildInfo.getInstance(), mq);
=======================================
--- /trunk/src/com/google/caja/opensocial/DefaultGadgetRewriter.java Mon Feb 22 19:56:49 2010 +++ /trunk/src/com/google/caja/opensocial/DefaultGadgetRewriter.java Tue Mar 2 13:55:40 2010
@@ -32,6 +32,7 @@
 import com.google.caja.plugin.Planner;
 import com.google.caja.plugin.PluginCompiler;
 import com.google.caja.plugin.PluginEnvironment;
+import com.google.caja.plugin.PluginMessageType;
 import com.google.caja.plugin.PluginMeta;
 import com.google.caja.render.Concatenator;
 import com.google.caja.render.JsPrettyPrinter;
@@ -175,7 +176,7 @@
       throws GadgetRewriteException, ParseException {
     DomParser p = new DomParser(new HtmlLexer(htmlContent), src, mq);
     if (p.getTokenQueue().isEmpty()) {
-      mq.addMessage(OpenSocialMessageType.NO_CONTENT, src);
+      mq.addMessage(PluginMessageType.NO_CONTENT, src);
       throw new GadgetRewriteException("No content");
     }
     return p.parseFragment();
=======================================
--- /trunk/src/com/google/caja/parser/html/Namespaces.java Fri Nov 13 11:43:08 2009 +++ /trunk/src/com/google/caja/parser/html/Namespaces.java Tue Mar 2 13:55:40 2010
@@ -31,11 +31,15 @@
    */
   public final String uri;

-  public static String HTML_NAMESPACE_URI = "http://www.w3.org/1999/xhtml";;
-  public static String XML_NAMESPACE_URI
+  public static final String HTML_NAMESPACE_URI
+      = "http://www.w3.org/1999/xhtml";;
+  public static final String XML_NAMESPACE_URI
       = "http://www.w3.org/XML/1998/namespace";;
- public static String XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";;
-  public static String SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg";;
+  public static final String XMLNS_NAMESPACE_URI
+      = "http://www.w3.org/2000/xmlns/";;
+  public static final String XLINK_NAMESPACE_URI
+      = "http://www.w3.org/1999/xlink";;
+ public static final String SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg";;

   private static final Namespaces XMLNS = new Namespaces(
       "xmlns", XMLNS_NAMESPACE_URI);
@@ -45,14 +49,19 @@
    */
   public static final Namespaces XML_SPECIAL = new Namespaces(
       XMLNS, "xml", XML_NAMESPACE_URI);
+
+  public static final Namespaces XLINK = new Namespaces(
+      XML_SPECIAL, "xlink", XLINK_NAMESPACE_URI);
+
+  public static final Namespaces IHTML = new Namespaces(
+      XLINK, "ihtml", "http://code.google.com/p/google-caja/ihtml";);
+
   /** Some common namespaces good for parsing XML fragments. */
   public static final Namespaces COMMON = new Namespaces(
       new Namespaces(
           new Namespaces(
               new Namespaces(
-                  new Namespaces(
-                      XML_SPECIAL,
-                      "ihtml", "http://code.google.com/p/google-caja/ihtml";),
+                  IHTML,
                   "xsl", "http://www.w3.org/1999/XSL/Transform";),
               "os", "http://ns.opensocial.org/2008/markup";),
           "svg", SVG_NAMESPACE_URI),
=======================================
--- /trunk/src/com/google/caja/parser/html/Nodes.java Fri Dec 18 20:56:18 2009 +++ /trunk/src/com/google/caja/parser/html/Nodes.java Tue Mar 2 13:55:40 2010
@@ -392,10 +392,20 @@
     }
   }

+  private static final int COMMON_NS_DEPTH = depth(Namespaces.COMMON);
   private Namespaces addNamespace(Namespaces base, String uri) {
+ // We subtract COMMON_NS_DEPTH so that when we modify Namespaces.COMMON,
+    // we do not change the output for documents that do not depend on the
+    // added or removed namespaces.
+    // It is alright for depth to be negative since dashes can appear in
+    // namespace prefixes.
+ return new Namespaces(base, "_ns" + (depth(base) - COMMON_NS_DEPTH), uri);
+  }
+
+  private static int depth(Namespaces ns) {
     int depth = 0;
-    for (Namespaces p = base; p != null; p = p.parent) { ++depth; }
-    return new Namespaces(base, "_ns" + depth, uri);
+    for (Namespaces p = ns; p != null; p = p.parent) { ++depth; }
+    return depth;
   }

   private void renderNamespace(Namespaces ns) {
=======================================
--- /trunk/src/com/google/caja/plugin/PluginMessageType.java Thu Feb 4 19:04:30 2010 +++ /trunk/src/com/google/caja/plugin/PluginMessageType.java Tue Mar 2 13:55:40 2010
@@ -82,6 +82,7 @@
   FONT_FACE_NOT_ALLOWED("%s: @font-face not allowed", MessageLevel.ERROR),
   FAILED_TO_LOAD_EXTERNAL_URL(
       "%s: failed to load external url %s", MessageLevel.WARNING),
+  NO_CONTENT("%s: no content", MessageLevel.FATAL_ERROR),
   UNRECOGNIZED_CONTENT_TYPE(
       "%s: unrecognized content type %s for %s tag", MessageLevel.WARNING),
   UNRECOGNIZED_MEDIA_TYPE(
=======================================
--- /trunk/tests/com/google/caja/parser/html/NodesTest.java Fri Nov 13 11:43:08 2009 +++ /trunk/tests/com/google/caja/parser/html/NodesTest.java Tue Mar 2 13:55:40 2010
@@ -116,7 +116,7 @@
     assertEquals(
         ""
         + "<xml:foo>"
-        + "<_ns8:baz xmlns:_ns8=\"http://bobs.house.of/XML&amp;BBQ\"";
+        + "<_ns1:baz xmlns:_ns1=\"http://bobs.house.of/XML&amp;BBQ\"";
         + " boo=\"howdy\" xml:lang=\"es\" />"
         + "</xml:foo>",
         Nodes.render(xmlFragment(fromString(
@@ -139,7 +139,7 @@
     DocumentFragment fragment = xmlFragment(fromString(
         "<svg:foo><svg:bar xmlns:svg='http://foo/'/></svg:foo>"));
     assertEquals(
- "<svg:foo><_ns8:bar xmlns:_ns8=\"http://foo/\";></_ns8:bar></svg:foo>", + "<svg:foo><_ns1:bar xmlns:_ns1=\"http://foo/\";></_ns1:bar></svg:foo>",
         Nodes.render(fragment));
   }

@@ -154,8 +154,8 @@
     rc.getOut().noMoreTokens();
     assertEquals(
         ""
-        + "<_ns9:foo xmlns:_ns9=\"http://www.w3.org/2000/svg\";>"
-        + "<svg:bar /></_ns9:foo>",
+        + "<_ns2:foo xmlns:_ns2=\"http://www.w3.org/2000/svg\";>"
+        + "<svg:bar /></_ns2:foo>",
         sb.toString());
   }

=======================================
--- /trunk/tests/com/google/caja/plugin/stages/LegacyNamespaceFixupStageTest.java Mon Feb 22 08:26:46 2010 +++ /trunk/tests/com/google/caja/plugin/stages/LegacyNamespaceFixupStageTest.java Tue Mar 2 13:55:40 2010
@@ -71,8 +71,8 @@
   public final void testPrefixedAttribWithUnknownPrefix() {
     assertFixed(
         ""
-        + "<a xmlns:_ns8=\"http://example.net/unknown-xml-namespace/\"";
-        + " _ns8:lang=\"en\" href=\"bar.html\">Foo</a>",
+        + "<a xmlns:_ns1=\"http://example.net/unknown-xml-namespace/\"";
+        + " _ns1:lang=\"en\" href=\"bar.html\">Foo</a>",
         builder().open("a").attr("href", "bar.html")
             .attr("baz:lang", "en").text("Foo").close().job());
     assertMessage(
@@ -99,8 +99,8 @@
     // element, not necessarily HTML.
     assertFixed(
         ""
- + "<_ns8:a xmlns:_ns8=\"http://example.net/unknown-xml-namespace/\"";
-        + " href=\"foo.svg\">Foo</_ns8:a>",
+ + "<_ns1:a xmlns:_ns1=\"http://example.net/unknown-xml-namespace/\"";
+        + " href=\"foo.svg\">Foo</_ns1:a>",
         builder().open("foo:a").attr("href", "foo.svg").text("Foo").close()
             .job());
     assertMessage(

Reply via email to