下面列出了org.w3c.dom.Element#setUserData ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// Mutation event fired by the adding of element end,
// and so lastAddedElement will be set.
super.endElement(uri, localName, qName);
if (locatorStack.size() > 0) {
Locator startLocator = locatorStack.pop();
XmlLocationData location = new XmlLocationData(
startLocator.getSystemId(),
startLocator.getLineNumber(),
startLocator.getColumnNumber(),
locator.getLineNumber(),
locator.getColumnNumber());
Element lastAddedElement = elementStack.pop();
lastAddedElement.setUserData(
XmlLocationData.LOCATION_DATA_KEY, location,
dataHandler);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// Mutation event fired by the adding of element end,
// and so lastAddedElement will be set.
super.endElement(uri, localName, qName);
if (locatorStack.size() > 0) {
Locator startLocator = locatorStack.pop();
XmlLocationData location = new XmlLocationData(
startLocator.getSystemId(),
startLocator.getLineNumber(),
startLocator.getColumnNumber(),
locator.getLineNumber(),
locator.getColumnNumber());
Element lastAddedElement = elementStack.pop();
lastAddedElement.setUserData(
XmlLocationData.LOCATION_DATA_KEY, location,
dataHandler);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// Mutation event fired by the adding of element end,
// and so lastAddedElement will be set.
super.endElement(uri, localName, qName);
if (locatorStack.size() > 0) {
Locator startLocator = locatorStack.pop();
XmlLocationData location = new XmlLocationData(
startLocator.getSystemId(),
startLocator.getLineNumber(),
startLocator.getColumnNumber(),
locator.getLineNumber(),
locator.getColumnNumber());
Element lastAddedElement = elementStack.pop();
lastAddedElement.setUserData(
XmlLocationData.LOCATION_DATA_KEY, location,
dataHandler);
}
}
@Test
public void testCheckForElements_servletClass() throws ParserConfigurationException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element root = document.createElement("web-app");
root.setUserData("version", "2.5", null);
root.setUserData("location", new DocumentLocation(1, 1), null);
Element element = document.createElement("servlet-class");
element.setTextContent("DoesNotExist");
element.setUserData("location", new DocumentLocation(2, 1), null);
root.appendChild(element);
document.appendChild(root);
WebXmlValidator validator = new WebXmlValidator();
ArrayList<ElementProblem> problems = validator.checkForProblems(resource, document);
assertEquals(1, problems.size());
String markerId = "com.google.cloud.tools.eclipse.appengine.validation.undefinedServletMarker";
assertEquals(markerId, problems.get(0).getMarkerId());
}
@Test
public void testCheckForElements_servletClassExists() throws ParserConfigurationException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element root = document.createElement("web-app");
root.setUserData("version", "2.5", null);
root.setUserData("location", new DocumentLocation(1, 1), null);
Element element = document.createElement("servlet-class");
element.setTextContent("ServletClass");
element.setUserData("location", new DocumentLocation(2, 1), null);
root.appendChild(element);
document.appendChild(root);
WebXmlValidator validator = new WebXmlValidator();
ArrayList<ElementProblem> problems = validator.checkForProblems(resource, document);
assertTrue(problems.isEmpty());
}
@Test
public void testValidateJavaServlet() throws ParserConfigurationException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element element = document.createElementNS("http://xmlns.jcp.org/xml/ns/javaee", "web-app");
element.setUserData("version", "3.1", null);
element.setUserData("location", new DocumentLocation(1, 1), null);
document.appendChild(element);
ArrayList<ElementProblem> problems = validator.checkForProblems(resource, document);
assertEquals(1, problems.size());
String markerId = "com.google.cloud.tools.eclipse.appengine.validation.servletMarker";
assertEquals(markerId, problems.get(0).getMarkerId());
}
@Test
public void testCheckForJava7() {
Element runtime =
document.createElementNS("http://appengine.google.com/ns/1.0", "runtime");
runtime.setUserData("location", new DocumentLocation(0, 25), null);
document.appendChild(runtime);
Node java7 = document.createTextNode("java7");
runtime.appendChild(java7);
List<ElementProblem> problems = validator.checkForProblems(null, document);
assertEquals(1, problems.size());
ElementProblem problem = problems.get(0);
assertEquals(RUNTIME_MARKER_ID, problem.getMarkerId());
assertEquals(0, problem.getStart().getLineNumber());
assertEquals(16, problem.getStart().getColumnNumber());
assertEquals(24, problem.getLength());
}
@Test
public void testCheckForElements_noElements() {
Element plugin = document.createElementNS("http://maven.apache.org/POM/4.0.0", "plugin");
plugin.setUserData("location", new DocumentLocation(1, 1), null);
Element groupId = document.createElementNS("http://maven.apache.org/POM/4.0.0", "groupId");
groupId.setUserData("location", new DocumentLocation(2, 1), null);
groupId.setTextContent("com.google.cloud.tools");
plugin.appendChild(groupId);
Element artifactId = document.createElementNS("http://maven.apache.org/POM/4.0.0", "artifactId");
artifactId.setUserData("location", new DocumentLocation(3, 1), null);
artifactId.setTextContent("appengine-maven-plugin");
plugin.appendChild(artifactId);
document.appendChild(plugin);
ArrayList<ElementProblem> problems = validator.checkForProblems(null, document);
assertEquals(0, problems.size());
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
addText();
Objects.requireNonNull(document);
Element element = document.createElementNS(uri, qName);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttribute(attributes.getQName(i), attributes.getValue(i));
}
element.setUserData(LOCATION_USER_DATA_KEY, getDocumentLocation(), null);
elementStack.push(element);
}
@Test
public void testCheckForElements_noElements() throws ParserConfigurationException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element element = document.createElementNS("http://java.sun.com/xml/ns/javaee", "web-app");
element.setUserData("version", "2.5", null);
element.setUserData("location", new DocumentLocation(1, 1), null);
document.appendChild(element);
ArrayList<ElementProblem> problems = validator.checkForProblems(resource, document);
assertEquals(0, problems.size());
}
@Test
public void testValidateServletMapping() throws ParserConfigurationException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element webApp = document.createElementNS("http://java.sun.com/xml/ns/javaee", "web-app");
webApp.setUserData("version", "2.5", null);
webApp.setUserData("location", new DocumentLocation(1, 1), null);
Element servlet = document.createElementNS("http://java.sun.com/xml/ns/javaee", "servlet");
servlet.setUserData("location", new DocumentLocation(2, 1), null);
Element servletName = document.createElementNS("http://java.sun.com/xml/ns/javaee", "servlet-name");
servletName.setTextContent("ServletName");
servletName.setUserData("location", new DocumentLocation(3, 1), null);
servlet.appendChild(servletName);
webApp.appendChild(servlet);
Element servletMapping = document.createElementNS("http://java.sun.com/xml/ns/javaee", "servlet-mapping");
servletMapping.setUserData("location", new DocumentLocation(4, 1), null);
Element servletMappingName = document.createElementNS("http://java.sun.com/xml/ns/javaee", "servlet-name");
servletMappingName.setTextContent("NotServletName");
servletMappingName.setUserData("location", new DocumentLocation(2, 1), null);
servletMapping.appendChild(servletMappingName);
webApp.appendChild(servletMapping);
document.appendChild(webApp);
ArrayList<ElementProblem> problems = validator.checkForProblems(resource, document);
assertEquals(1, problems.size());
}
@Override
@SuppressWarnings("checkstyle:parameterName")
public void startElement(final String uri, final String localName, final String qName,
final Attributes attributes) {
addTextIfNeeded();
final Element el = doc.createElementNS(uri, qName);
for (int i = 0, len = attributes.getLength(); i < len; i++) {
el.setAttributeNS(attributes.getURI(i), attributes.getQName(i), attributes.getValue(i));
}
final StatementSourceReference ref = DeclarationInTextSource.atPosition(file, documentLocator.getLineNumber(),
documentLocator.getColumnNumber());
el.setUserData(USER_DATA_KEY, ref, null);
stack.push(el);
}
protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
String bus = element.getAttribute("bus");
if (StringUtils.isEmpty(bus)) {
bus = element.getAttribute("name");
if (StringUtils.isEmpty(bus)) {
element.setAttribute("bus", bus);
}
}
element.removeAttribute("name");
if (StringUtils.isEmpty(bus)) {
bus = "cxf";
}
String id = element.getAttribute("id");
if (!StringUtils.isEmpty(id)) {
bean.addPropertyValue("id", id);
}
super.doParse(element, ctx, bean);
if (ctx.getRegistry().containsBeanDefinition(bus)) {
BeanDefinition def = ctx.getRegistry().getBeanDefinition(bus);
copyProps(bean, def);
bean.addConstructorArgValue(bus);
} else if (!"cxf".equals(bus)) {
bean.getRawBeanDefinition().setBeanClass(SpringBus.class);
bean.setDestroyMethodName("shutdown");
try {
element.setUserData("ID", bus, null);
bean.getRawBeanDefinition().getPropertyValues().removePropertyValue("bus");
} catch (Throwable t) {
//likely not DOM level 3, ignore
}
} else {
addBusWiringAttribute(bean, BusWiringType.PROPERTY, bus, ctx);
bean.getRawBeanDefinition().setAttribute(WIRE_BUS_CREATE,
resolveId(element, null, ctx));
bean.addConstructorArgValue(bus);
}
}
@Test
public void testCheckForJava6() {
Element element =
document.createElementNS("http://appengine.google.com/ns/1.0", "runtime");
element.setUserData("location", new DocumentLocation(3, 15), null);
document.appendChild(element);
Node java6 = document.createTextNode("java"); // sic; java, not java6
element.appendChild(java6);
List<ElementProblem> problems = validator.checkForProblems(null, document);
assertEquals(1, problems.size());
assertEquals(RUNTIME_MARKER_ID, problems.get(0).getMarkerId());
}
@Test
public void testCheckForElements() {
Element groupId = document.createElementNS("http://maven.apache.org/POM/4.0.0", "groupId");
groupId.setUserData("location", new DocumentLocation(2, 1), null);
groupId.setTextContent("com.google.appengine");
Element artifactId = document.createElementNS("http://maven.apache.org/POM/4.0.0", "artifactId");
artifactId.setUserData("location", new DocumentLocation(3, 1), null);
artifactId.setTextContent("appengine-maven-plugin");
Element plugin = document.createElementNS("http://maven.apache.org/POM/4.0.0", "plugin");
plugin.setUserData("location", new DocumentLocation(4, 1), null);
plugin.appendChild(groupId);
plugin.appendChild(artifactId);
Element plugins = document.createElementNS("http://maven.apache.org/POM/4.0.0", "plugins");
plugins.setUserData("location", new DocumentLocation(5, 1), null);
plugins.appendChild(plugin);
Element build = document.createElementNS("http://maven.apache.org/POM/4.0.0", "build");
build.setUserData("location", new DocumentLocation(6, 1), null);
build.appendChild(plugins);
document.appendChild(build);
ArrayList<ElementProblem> problems = validator.checkForProblems(null, document);
assertEquals(1, problems.size());
String markerId = "com.google.cloud.tools.eclipse.appengine.validation.mavenPluginMarker";
assertEquals(markerId, problems.get(0).getMarkerId());
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
addText();
Element element = document.createElementNS(uri, qName);
for (int i = 0; i < attributes.getLength(); i++) {
element.setUserData(attributes.getQName(i), attributes.getValue(i), null);
}
DocumentLocation location = new DocumentLocation(
locator.getLineNumber(), locator.getColumnNumber());
element.setUserData("location", location, null);
elementStack.push(element);
}
@Override
public void endElement(String uri, String localName, String qName){
addText();
Element closedElement = elementStack.pop();
if (elementStack.isEmpty()) { // If this is the root element
closedElement.setUserData("encoding", locator.getEncoding(), null);
document.appendChild(closedElement);
} else {
Element parentElement = elementStack.peek();
parentElement.appendChild(closedElement);
}
}
/**
* @see nu.validator.htmlparser.impl.TreeBuilder#createElement(String,
* java.lang.String, org.xml.sax.Attributes, java.lang.Object)
*/
@Override protected Element createElement(String ns, String name,
HtmlAttributes attributes, Element form) throws SAXException {
try {
Element rv = createElement(ns, name, attributes);
rv.setUserData("nu.validator.form-pointer", form, null);
return rv;
} catch (DOMException e) {
fatal(e);
return null;
}
}
@Test
public void testCheckForElements_multiplePluginTags() {
Element rootPlugin = document.createElementNS("http://maven.apache.org/POM/4.0.0", "plugins");
//plugin #1
Element markedPlugin = document.createElementNS("http://maven.apache.org/POM/4.0.0", "plugin");
markedPlugin.setUserData("location", new DocumentLocation(1, 1), null);
Element groupId1 = document.createElementNS("http://maven.apache.org/POM/4.0.0", "groupId");
groupId1.setUserData("location", new DocumentLocation(2, 1), null);
groupId1.setTextContent("com.google.appengine");
markedPlugin.appendChild(groupId1);
Element artifactId1 = document.createElementNS("http://maven.apache.org/POM/4.0.0", "artifactId");
artifactId1.setUserData("location", new DocumentLocation(3, 1), null);
artifactId1.setTextContent("appengine-maven-plugin");
markedPlugin.appendChild(artifactId1);
rootPlugin.appendChild(markedPlugin);
//plugin #2
Element ignoredPlugin = document.createElementNS("http://maven.apache.org/POM/4.0.0", "plugin");
ignoredPlugin.setUserData("location", new DocumentLocation(4, 1), null);
Element groupId2 = document.createElementNS("http://maven.apache.org/POM/4.0.0", "groupId");
groupId2.setUserData("location", new DocumentLocation(5, 1), null);
groupId2.setTextContent("com.google.cloud.tools");
ignoredPlugin.appendChild(groupId2);
Element artifactId2 = document.createElementNS("http://maven.apache.org/POM/4.0.0", "artifactId");
artifactId2.setUserData("location", new DocumentLocation(6, 1), null);
artifactId2.setTextContent("appengine-maven-plugin");
ignoredPlugin.appendChild(artifactId2);
rootPlugin.appendChild(ignoredPlugin);
//plugin #3
Element ignoredPlugin2 = document.createElementNS("http://maven.apache.org/POM/4.0.0", "plugin");
markedPlugin.setUserData("location", new DocumentLocation(7, 1), null);
Element groupId3 = document.createElementNS("http://maven.apache.org/POM/4.0.0", "groupId");
groupId3.setUserData("location", new DocumentLocation(8, 1), null);
groupId3.setTextContent("com.google.appengine");
ignoredPlugin2.appendChild(groupId3);
Element artifactId3 = document.createElementNS("http://maven.apache.org/POM/4.0.0", "artifactId");
artifactId3.setUserData("location", new DocumentLocation(9, 1), null);
artifactId3.setTextContent("ignore this case");
ignoredPlugin2.appendChild(artifactId3);
rootPlugin.appendChild(ignoredPlugin2);
document.appendChild(rootPlugin);
ArrayList<ElementProblem> problems = validator.checkForProblems(null, document);
assertEquals(1, problems.size());
String markerId = "com.google.cloud.tools.eclipse.appengine.validation.mavenPluginMarker";
assertEquals(markerId, problems.get(0).getMarkerId());
}
/**
* @see #hasXmlnsDeclaration(Element)
*/
public static void markAsHavingXmlnsDeclaration(Element el) {
el.setUserData(
HAS_XMLNS_DECLARATION_KEY, Boolean.TRUE,
HAS_XMLNS_DECLARATION_DATA_HANDLER);
}