public boolean revisit(Gadget gadget, List<Node> nodes) throws RewritingException { Node head = DomUtil.getFirstNamedChildNode( nodes.get(0).getOwnerDocument().getDocumentElement(), "head"); if (head == null) { // Should never occur; do for paranoia's sake. return false; } // Detach nodes for (Node n : nodes) { n.getParentNode().removeChild(n); } // Add nodes back to DOM if (head.getFirstChild() == null) { // add each node to head for (Node n : nodes) { head.appendChild(n); } } else { // existing nodes in head, inject all nodes before the first one Node firstChild = head.getFirstChild(); for (Node n : nodes) { head.insertBefore(n, firstChild); } } return true; }
private void processTemplateDef(Builder<TagHandler> handlers, Element defElement) throws TemplateParserException { Attr tagAttribute = defElement.getAttributeNode(TAG_ATTRIBUTE); if (tagAttribute == null) { throw new TemplateParserException("Missing tag attribute on TemplateDef"); } ImmutableSet.Builder<TemplateResource> resources = ImmutableSet.builder(); Element scriptElement = (Element) DomUtil.getFirstNamedChildNode(defElement, JAVASCRIPT_TAG); if (scriptElement != null) { resources.add(TemplateResource.newJavascriptResource(scriptElement.getTextContent(), this)); } Element styleElement = (Element) DomUtil.getFirstNamedChildNode(defElement, STYLE_TAG); if (styleElement != null) { resources.add(TemplateResource.newStyleResource(styleElement.getTextContent(), this)); } Element templateElement = (Element) DomUtil.getFirstNamedChildNode(defElement, TEMPLATE_TAG); TagHandler handler = createHandler(tagAttribute.getNodeValue(), templateElement, resources.build()); if (handler != null) { handlers.add(handler); } }
private void processTemplateDef(Builder<TagHandler> handlers, Element defElement) throws TemplateParserException { Attr tagAttribute = defElement.getAttributeNode(TAG_ATTRIBUTE); if (tagAttribute == null) { throw new TemplateParserException("Missing tag attribute on TemplateDef"); } ImmutableSet.Builder<TemplateResource> resources = ImmutableSet.builder(); Element scriptElement = (Element) DomUtil.getFirstNamedChildNode(defElement, JAVASCRIPT_TAG); if (scriptElement != null) { resources.add(TemplateResource.newJavascriptResource(scriptElement.getTextContent(), this)); } Element styleElement = (Element) DomUtil.getFirstNamedChildNode(defElement, STYLE_TAG); if (styleElement != null) { resources.add(TemplateResource.newStyleResource(styleElement.getTextContent(), this)); } Element templateElement = (Element) DomUtil.getFirstNamedChildNode(defElement, TEMPLATE_TAG); TagHandler handler = createHandler(tagAttribute.getNodeValue(), templateElement, resources.build()); if (handler != null) { handlers.add(handler); } }
private void processTemplateDef(Builder<TagHandler> handlers, Element defElement) throws TemplateParserException { Attr tagAttribute = null; synchronized (defElement) { tagAttribute = defElement.getAttributeNode(TAG_ATTRIBUTE); if (tagAttribute == null) { throw new TemplateParserException("Missing tag attribute on TemplateDef"); } } ImmutableSet.Builder<TemplateResource> resources = ImmutableSet.builder(); Element scriptElement = (Element) DomUtil.getFirstNamedChildNode(defElement, JAVASCRIPT_TAG); if (scriptElement != null) { resources.add(TemplateResource.newJavascriptResource(scriptElement.getTextContent(), this)); } Element styleElement = (Element) DomUtil.getFirstNamedChildNode(defElement, STYLE_TAG); if (styleElement != null) { resources.add(TemplateResource.newStyleResource(styleElement.getTextContent(), this)); } Element templateElement = (Element) DomUtil.getFirstNamedChildNode(defElement, TEMPLATE_TAG); TagHandler handler = createHandler(tagAttribute.getNodeValue(), templateElement, resources.build()); if (handler != null) { handlers.add(handler); } }
@Test public void getFirstNamedChildNode() { assertEquals("zero", DomUtil.getFirstNamedChildNode(root, "element").getTextContent()); assertEquals("whatever", DomUtil.getFirstNamedChildNode(root, "other").getTextContent()); assertNull("Did not return null for missing element.", DomUtil.getFirstNamedChildNode(root, "fake")); }
@Test public void getFirstNamedChildNode() { assertEquals("zero", DomUtil.getFirstNamedChildNode(root, "element").getTextContent()); assertEquals("whatever", DomUtil.getFirstNamedChildNode(root, "other").getTextContent()); assertNull("Did not return null for missing element.", DomUtil.getFirstNamedChildNode(root, "fake")); }
@Test public void getFirstNamedChildNode() throws Exception { Element root = XmlUtil.parse(XML); assertEquals("zero", DomUtil.getFirstNamedChildNode(root, "element").getTextContent()); assertEquals("whatever", DomUtil.getFirstNamedChildNode(root, "other").getTextContent()); assertNull("Did not return null for missing element.", DomUtil.getFirstNamedChildNode(root, "fake")); }
@Test public void getFirstNamedChildNode() throws Exception { Element root = XmlUtil.parse(XML); assertEquals("zero", DomUtil.getFirstNamedChildNode(root, "element").getTextContent()); assertEquals("whatever", DomUtil.getFirstNamedChildNode(root, "other").getTextContent()); assertNull("Did not return null for missing element.", DomUtil.getFirstNamedChildNode(root, "fake")); }
@Test public void getFirstNamedChildNode() throws Exception { Element root = XmlUtil.parse(XML); assertEquals("zero", DomUtil.getFirstNamedChildNode(root, "element").getTextContent()); assertEquals("whatever", DomUtil.getFirstNamedChildNode(root, "other").getTextContent()); assertNull("Did not return null for missing element.", DomUtil.getFirstNamedChildNode(root, "fake")); }
@Override protected Document parseDomImpl(String source) throws GadgetException { DocumentHandler handler; HTMLConfiguration config = newConfiguration(); try { handler = parseHtmlImpl(source, config, new NormalizingTagBalancer()); } catch (IOException ioe) { return null; } Document document = handler.getDocument(); document.appendChild(DomUtil.getFirstNamedChildNode(handler.getFragment(), "html")); fixNekoWeirdness(document); return document; }
@Override protected Document parseDomImpl(String source) throws GadgetException { DocumentHandler handler; HTMLConfiguration config = newConfiguration(); try { handler = parseHtmlImpl(source, config, new NormalizingTagBalancer()); } catch (IOException ioe) { return null; } Document document = handler.getDocument(); document.appendChild(DomUtil.getFirstNamedChildNode(handler.getFragment(), "html")); fixNekoWeirdness(document); return document; }
@Override protected Document parseDomImpl(String source) throws GadgetException { DocumentHandler handler; HTMLConfiguration config = newConfiguration(); try { handler = parseHtmlImpl(source, config, new NormalizingTagBalancer()); } catch (IOException ioe) { return null; } Document document = handler.getDocument(); document.appendChild(DomUtil.getFirstNamedChildNode(handler.getFragment(), "html")); fixNekoWeirdness(document); return document; }
public void rewrite(Gadget gadget, MutableContent mutableContent) throws RewritingException { // Don't touch sanitized gadgets. if (gadget.sanitizeOutput()) { return; } // Quickly return if opensocial-i18n feature is not needed. if (!gadget.getAllFeatures().contains(I18N_FEATURE_NAME)) { return; } try { Document document = mutableContent.getDocument(); Element head = (Element)DomUtil.getFirstNamedChildNode(document.getDocumentElement(), "head"); injectI18NConstants(gadget, head); mutableContent.documentChanged(); } catch (GadgetException e) { throw new RewritingException(e, e.getHttpStatusCode()); } }
public void rewrite(Gadget gadget, MutableContent mutableContent) throws RewritingException { // Don't touch sanitized gadgets. if (gadget.sanitizeOutput()) { return; } // Quickly return if opensocial-i18n feature is not needed. if (!gadget.getAllFeatures().contains(I18N_FEATURE_NAME)) { return; } try { Document document = mutableContent.getDocument(); Element head = (Element)DomUtil.getFirstNamedChildNode(document.getDocumentElement(), "head"); injectI18NConstants(gadget, head); mutableContent.documentChanged(); } catch (GadgetException e) { throw new RewritingException(e, e.getHttpStatusCode()); } }
public void rewrite(Gadget gadget, MutableContent mutableContent) throws RewritingException { // Don't touch sanitized gadgets. if (gadget.sanitizeOutput()) { return; } // Quickly return if opensocial-i18n feature is not needed. if (!gadget.getAllFeatures().contains(I18N_FEATURE_NAME)) { return; } try { Document document = mutableContent.getDocument(); Element head = (Element)DomUtil.getFirstNamedChildNode(document.getDocumentElement(), "head"); injectI18NConstants(gadget, head); mutableContent.documentChanged(); } catch (GadgetException e) { throw new RewritingException(e, e.getHttpStatusCode()); } }
private void checkRevisitSingle(Element tpl, String id) throws Exception { Gadget gadget = mockGadget(OsTemplateXmlLoaderRewriter.OS_TEMPLATES_FEATURE_NAME, "another"); String xmlVal = "xml"; Converter converter = mockConverter(xmlVal, "{thejson}", 1); tpl.setTextContent(xmlVal); completeDocAsHtml(tpl); assertTrue(revisit(gadget, converter, tpl)); verify(gadget); verify(converter); Node head = DomUtil.getFirstNamedChildNode(doc.getDocumentElement(), "head"); assertNotNull(head); assertEquals(2, head.getChildNodes().getLength()); Node addedScript = head.getChildNodes().item(1); assertEquals(Node.ELEMENT_NODE, addedScript.getNodeType()); assertEquals("script", addedScript.getNodeName()); assertEquals("gadgets.jsondom.preload_('" + id + "',{thejson});", addedScript.getTextContent()); }
private void checkRevisitSingle(Element tpl, String id) throws Exception { Gadget gadget = mockGadget(OsTemplateXmlLoaderRewriter.OS_TEMPLATES_FEATURE_NAME, "another"); String xmlVal = "xml"; Converter converter = mockConverter(xmlVal, "{thejson}", 1); tpl.setTextContent(xmlVal); completeDocAsHtml(tpl); assertTrue(revisit(gadget, converter, tpl)); verify(gadget); verify(converter); Node head = DomUtil.getFirstNamedChildNode(doc.getDocumentElement(), "head"); assertNotNull(head); assertEquals(2, head.getChildNodes().getLength()); Node addedScript = head.getChildNodes().item(1); assertEquals(Node.ELEMENT_NODE, addedScript.getNodeType()); assertEquals("script", addedScript.getNodeName()); assertEquals("gadgets.jsondom.preload_('" + id + "',{thejson});", addedScript.getTextContent()); }
private void checkRevisitSingle(Element tpl, String id) throws Exception { Gadget gadget = mockGadget(OsTemplateXmlLoaderRewriter.OS_TEMPLATES_FEATURE_NAME, "another"); String xmlVal = "xml"; Converter converter = mockConverter(xmlVal, "{thejson}", 1); tpl.setTextContent(xmlVal); completeDocAsHtml(tpl); assertTrue(revisit(gadget, converter, tpl)); verify(gadget); verify(converter); Node head = DomUtil.getFirstNamedChildNode(doc.getDocumentElement(), "head"); assertNotNull(head); assertEquals(2, head.getChildNodes().getLength()); Node addedScript = head.getChildNodes().item(1); assertEquals(Node.ELEMENT_NODE, addedScript.getNodeType()); assertEquals("script", addedScript.getNodeName()); assertEquals("gadgets.jsondom.preload_('" + id + "',{thejson});", addedScript.getTextContent()); }
@Test public void revisitMultiples() throws Exception { Element tplId = createRewritableDiv("tpl_id"); Element tplName = createRewritableDiv(); tplName.removeAttribute("id"); tplName.setAttribute("name", "otherid"); Gadget gadget = mockGadget(OsTemplateXmlLoaderRewriter.OS_TEMPLATES_FEATURE_NAME, "another"); String xmlVal = "thexml"; Converter converter = mockConverter(xmlVal, "{thejson}", 2); tplId.setTextContent(xmlVal); tplName.setTextContent(xmlVal); completeDocAsHtml(tplId, tplName); assertTrue(revisit(gadget, converter, tplId, tplName)); verify(gadget); verify(converter); Node head = DomUtil.getFirstNamedChildNode(doc.getDocumentElement(), "head"); assertNotNull(head); assertEquals(2, head.getChildNodes().getLength()); Node addedScript = head.getChildNodes().item(1); assertEquals(Node.ELEMENT_NODE, addedScript.getNodeType()); assertEquals("script", addedScript.getNodeName()); assertEquals( "gadgets.jsondom.preload_('tpl_id',{thejson});gadgets.jsondom.preload_('otherid',{thejson});", addedScript.getTextContent()); }
@Test public void revisitMultiples() throws Exception { Element tplId = createRewritableDiv("tpl_id"); Element tplName = createRewritableDiv(); tplName.removeAttribute("id"); tplName.setAttribute("name", "otherid"); Gadget gadget = mockGadget(OsTemplateXmlLoaderRewriter.OS_TEMPLATES_FEATURE_NAME, "another"); String xmlVal = "thexml"; Converter converter = mockConverter(xmlVal, "{thejson}", 2); tplId.setTextContent(xmlVal); tplName.setTextContent(xmlVal); completeDocAsHtml(tplId, tplName); assertTrue(revisit(gadget, converter, tplId, tplName)); verify(gadget); verify(converter); Node head = DomUtil.getFirstNamedChildNode(doc.getDocumentElement(), "head"); assertNotNull(head); assertEquals(2, head.getChildNodes().getLength()); Node addedScript = head.getChildNodes().item(1); assertEquals(Node.ELEMENT_NODE, addedScript.getNodeType()); assertEquals("script", addedScript.getNodeName()); assertEquals( "gadgets.jsondom.preload_('tpl_id',{thejson});gadgets.jsondom.preload_('otherid',{thejson});", addedScript.getTextContent()); }