// factory private static GenerationTarget createTarget(File templateFile, File root) { GenerationTarget target; // read XML document DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.parse(templateFile); // get name NodeList nodes = doc.getElementsByTagName("templates"); Node node = nodes.item(0); NamedNodeMap attrs = node.getAttributes(); Node attr = attrs.getNamedItem("name"); String templateName = attr.getNodeValue(); target = new GenerationTarget(templateFile, root, templateName); // get class directives nodes = doc.getElementsByTagName("project"); int nb = nodes.getLength(); for (int i = 0; i < nb; i++) { node = nodes.item(i); createProjectDirective(target, node); } } catch (ParserConfigurationException ex) { target = null; } catch (IOException ex) { target = null; } catch (SAXException ex) { target = null; } // end try return target; }
/** @param element */ private void validatePlugins(Element parent) { NodeList list = getChildrenByName(parent, "plugin"); // $NON-NLS-1$ for (int i = 0; i < list.getLength(); i++) { if (fMonitor.isCanceled()) return; Element plugin = (Element) list.item(i); assertAttributeDefined(plugin, "id", CompilerFlags.ERROR); // $NON-NLS-1$ assertAttributeDefined(plugin, "version", CompilerFlags.ERROR); // $NON-NLS-1$ NamedNodeMap attributes = plugin.getAttributes(); boolean isFragment = plugin.getAttribute("fragment").equals("true"); // $NON-NLS-1$ //$NON-NLS-2$ for (int j = 0; j < attributes.getLength(); j++) { Attr attr = (Attr) attributes.item(j); String name = attr.getName(); if (name.equals("id")) { // $NON-NLS-1$ validatePluginID(plugin, attr, isFragment); } else if (name.equals("version")) { // $NON-NLS-1$ validateVersionAttribute(plugin, attr); validateVersion(plugin, attr); } else if (name.equals("fragment") || name.equals("unpack")) { // $NON-NLS-1$ //$NON-NLS-2$ validateBoolean(plugin, attr); } else if (!name.equals("os") && !name.equals("ws") && !name.equals("nl") // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ && !name.equals("arch") && !name.equals("download-size") // $NON-NLS-1$ //$NON-NLS-2$ && !name.equals("install-size") && !name.equals("filter")) { // $NON-NLS-1$ //$NON-NLS-2$ reportUnknownAttribute(plugin, name, CompilerFlags.ERROR); } } validateUnpack(plugin); } }
private void gatherNamespaces(Element element, List<URI> namespaceSources) throws SAXException { NamedNodeMap attributes = element.getAttributes(); int attributeCount = attributes.getLength(); for (int i = 0; i < attributeCount; i++) { Attr attribute = (Attr) attributes.item(i); String namespace = attribute.getNamespaceURI(); if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespace)) { try { namespaceSources.add(new URI(attribute.getValue())); } catch (URISyntaxException e) { throw new SAXException( "Cannot validate this document with this class. Namespaces must be valid URIs. Found Namespace: '" + attribute.getValue() + "'.", e); } } } NodeList childNodes = element.getChildNodes(); int childCount = childNodes.getLength(); for (int i = 0; i < childCount; i++) { Node child = childNodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { gatherNamespaces((Element) child, namespaceSources); } } }
/** Returns a sorted list of attributes. */ protected Attr[] sortAttributes(NamedNodeMap attrs) { int len = (attrs != null) ? attrs.getLength() : 0; Attr array[] = new Attr[len]; for (int i = 0; i < len; i++) { array[i] = (Attr) attrs.item(i); } for (int i = 0; i < len - 1; i++) { String name = array[i].getNodeName(); int index = i; for (int j = i + 1; j < len; j++) { String curName = array[j].getNodeName(); if (curName.compareTo(name) < 0) { name = curName; index = j; } } if (index != i) { Attr temp = array[i]; array[i] = array[index]; array[index] = temp; } } return (array); } // sortAttributes(NamedNodeMap):Attr[]
void removeNamespace(Namespace namespace) { Namespace current = getNodeNamespace(); // Do not remove in-use namespace if (namespace.is(current)) return; NamedNodeMap attrs = this.dom.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { XmlNode attr = XmlNode.createImpl(attrs.item(i)); if (namespace.is(attr.getNodeNamespace())) return; } // TODO I must confess I am not sure I understand the spec fully. See ECMA357 13.4.4.31 String existingPrefix = getExistingPrefixFor(namespace); if (existingPrefix != null) { if (namespace.isUnspecifiedPrefix()) { // we should remove any namespace with this URI from scope; we do this by declaring a // namespace with the same // prefix as the existing prefix and setting its URI to the default namespace declareNamespace(existingPrefix, getDefaultNamespace().getUri()); } else { if (existingPrefix.equals(namespace.getPrefix())) { declareNamespace(existingPrefix, getDefaultNamespace().getUri()); } } } else { // the argument namespace is not declared in this scope, so do nothing. } }
/** * Runs the test case. * * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; NamedNodeMap notations; DocumentType docType; Node retval; Element elem; doc = (Document) load("hc_staff", true); docType = doc.getDoctype(); if (!(("text/html".equals(getContentType())))) { assertNotNull("docTypeNotNull", docType); notations = docType.getNotations(); assertNotNull("notationsNotNull", notations); elem = doc.createElementNS("http://www.w3.org/1999/xhtml", "br"); try { retval = notations.setNamedItemNS(elem); fail("throw_HIER_OR_NO_MOD_ERR"); } catch (DOMException ex) { switch (ex.code) { case 3: break; case 7: break; default: throw ex; } } } }
private static Address unmarshallAddress(Node addressNode) { Address address = new Address(); NamedNodeMap addressNodeAttributes = addressNode.getAttributes(); address.setCity(addressNodeAttributes.item(1).getNodeValue()); address.setCountryCode(addressNodeAttributes.item(2).getNodeValue()); address.setAddress(addressNodeAttributes.item(0).getNodeValue()); return address; }
/** * Get a list of the names for all of the attributes for this node. * * @webref xml:method * @brief Returns a list of names of all attributes as an array */ public String[] listAttributes() { NamedNodeMap nnm = node.getAttributes(); String[] outgoing = new String[nnm.getLength()]; for (int i = 0; i < outgoing.length; i++) { outgoing[i] = nnm.item(i).getNodeName(); } return outgoing; }
private void validateImports(Element parent) { NodeList list = getChildrenByName(parent, "import"); // $NON-NLS-1$ for (int i = 0; i < list.getLength(); i++) { if (fMonitor.isCanceled()) return; Element element = (Element) list.item(i); Attr plugin = element.getAttributeNode("plugin"); // $NON-NLS-1$ Attr feature = element.getAttributeNode("feature"); // $NON-NLS-1$ if (plugin == null && feature == null) { assertAttributeDefined(element, "plugin", CompilerFlags.ERROR); // $NON-NLS-1$ } else if (plugin != null && feature != null) { reportExclusiveAttributes( element, "plugin", "feature", CompilerFlags.ERROR); // $NON-NLS-1$//$NON-NLS-2$ } else if (plugin != null) { validatePluginID(element, plugin, false); } else if (feature != null) { validateFeatureID(element, feature); } NamedNodeMap attributes = element.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { Attr attr = (Attr) attributes.item(j); String name = attr.getName(); if (name.equals("version")) { // $NON-NLS-1$ validateVersionAttribute(element, attr); } else if (name.equals("match")) { // $NON-NLS-1$ if (element.getAttributeNode("patch") != null) { // $NON-NLS-1$ report( NLS.bind(PDECoreMessages.Builders_Feature_patchedMatch, attr.getValue()), getLine(element, attr.getValue()), CompilerFlags.ERROR, PDEMarkerFactory.CAT_FATAL); } else { validateMatch(element, attr); } } else if (name.equals("patch")) { // $NON-NLS-1$ if ("true".equalsIgnoreCase(attr.getValue()) && feature == null) { // $NON-NLS-1$ report( NLS.bind(PDECoreMessages.Builders_Feature_patchPlugin, attr.getValue()), getLine(element, attr.getValue()), CompilerFlags.ERROR, PDEMarkerFactory.CAT_FATAL); } else if ("true".equalsIgnoreCase(attr.getValue()) && element.getAttributeNode("version") == null) { // $NON-NLS-1$ //$NON-NLS-2$ report( NLS.bind(PDECoreMessages.Builders_Feature_patchedVersion, attr.getValue()), getLine(element, attr.getValue()), CompilerFlags.ERROR, PDEMarkerFactory.CAT_FATAL); } else { validateBoolean(element, attr); } } else if (!name.equals("plugin") && !name.equals("feature") && !name.equals("filter")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ reportUnknownAttribute(element, name, CompilerFlags.ERROR); } } } }
public String getAtributo(Node nodo, String nombreAtributo) { NamedNodeMap atts; atts = nodo.getAttributes(); if (atts == null) return null; for (int i = 0; i < atts.getLength(); i++) { Node atributo = atts.item(i); if (atributo.getNodeName().equalsIgnoreCase(nombreAtributo)) return atributo.getNodeValue(); } return null; }
XmlNode[] getAttributes() { NamedNodeMap attrs = this.dom.getAttributes(); // TODO Or could make callers handle null? if (attrs == null) throw new IllegalStateException("Must be element."); XmlNode[] rv = new XmlNode[attrs.getLength()]; for (int i = 0; i < attrs.getLength(); i++) { rv[i] = createImpl(attrs.item(i)); } return rv; }
public String getString(String name, String defaultValue) { NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { Node attr = attrs.getNamedItem(name); if (attr != null) { return attr.getNodeValue(); } } return defaultValue; }
private static Command parseCommand(Node n) { NamedNodeMap atts = n.getAttributes(); String name = atts.getNamedItem("name").getNodeValue(); String desc = atts.getNamedItem("description").getNodeValue(); String command = atts.getNamedItem("command").getNodeValue(); Command com = new Command(name, desc, command); NodeList children = n.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (!child.getNodeName().equals("Argument")) continue; NamedNodeMap childAtts = child.getAttributes(); String childName = childAtts.getNamedItem("name").getNodeValue(); String childDesc = childAtts.getNamedItem("description").getNodeValue(); String childVal = ""; if (childAtts.getNamedItem("default") != null) childVal = childAtts.getNamedItem("default").getNodeValue(); Argument arg = new Argument(childName, childDesc, childVal); com.addArgument(arg); } return com; }
/** * Counts the attributenode for an element (xmlns attributes ignored) * * @param attributesR attributesMap * @return number of attributes */ private int countAttributes(NamedNodeMap attributesR) { int cntAttributes = 0; for (int i = 0; i < attributesR.getLength(); i++) { if (!attributesR.item(i).getNodeName().startsWith(XMLConstants.XMLNS_ATTRIBUTE)) { cntAttributes++; } } return cntAttributes; }
private static Customer customerUnmarshaller(Node customerNode) { Customer customer = new Customer(); NamedNodeMap customerAttributes = customerNode.getAttributes(); customer.setFirstName(customerAttributes.item(0).getNodeValue()); customer.setLastName(customerAttributes.item(1).getNodeValue()); Node addressNode = customerNode.getFirstChild().getNextSibling(); customer.setAddress(unmarshallAddress(addressNode)); Node dataNode = customerNode.getFirstChild().getNextSibling().getNextSibling().getNextSibling(); customer.setData(unmarshallData(dataNode)); return customer; }
/** Возвращает имя метода. */ public String getName() { /** Получаем атрибуты узла метода. */ NamedNodeMap attributes = node.getAttributes(); /** Получаем узел аттрибута. */ Node nameAttrib = attributes.getNamedItem("name"); /** Возвращаем значение атрибута. */ return nameAttrib.getNodeValue(); }
protected Element copyElementToName(Element element, String tagName) { Element newElement = element.getOwnerDocument().createElement(tagName); NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attribute = attrs.item(i); newElement.setAttribute(attribute.getNodeName(), attribute.getNodeValue()); } for (int i = 0; i < element.getChildNodes().getLength(); i++) { newElement.appendChild(element.getChildNodes().item(i).cloneNode(true)); } return newElement; }
public static Map<String, String> getAttrList(Node node) { Map<String, String> map = new HashMap<String, String>(); NamedNodeMap list = node.getAttributes(); if (list != null) for (int i = 0; i < list.getLength(); i++) { Attr attr = (Attr) list.item(i); map.put(attr.getName(), attr.getValue()); } return map; }
private Properties parseAttributes(Node n) { Properties attributes = new Properties(); NamedNodeMap attributeNodes = n.getAttributes(); if (attributeNodes != null) { for (int i = 0; i < attributeNodes.getLength(); i++) { Node attribute = attributeNodes.item(i); String value = attribute.getNodeValue(); attributes.put(attribute.getNodeName(), value); } } return attributes; }
/** * Runs the test case. * * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; DocumentType docType; NamedNodeMap entitiesMap; Entity entity; String encodingName; doc = (Document) load("external_barfoo", false); docType = doc.getDoctype(); entitiesMap = docType.getEntities(); entity = (Entity) entitiesMap.getNamedItem("ent5"); encodingName = entity.getXmlEncoding(); assertNull("entitygetxmlencoding02", encodingName); }
/** * Runs the test case. * * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; NodeList elementList; Node testEmployee; NamedNodeMap attributes; int length; doc = (Document) load("staff", true); elementList = doc.getElementsByTagName("address"); testEmployee = elementList.item(2); attributes = testEmployee.getAttributes(); length = (int) attributes.getLength(); assertEquals("length", 2, length); }
// dump and element public void dump(Element e) throws Exception { System.out.println("Element " + e.getNodeName()); NamedNodeMap attrs = e.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); System.out.println(" " + attr.getName() + "=" + attr.getValue()); } NodeList nodes = e.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element) dump((Element) node); } }
private static void createProjectDirective(GenerationTarget target, Node node) { NamedNodeMap attrs = node.getAttributes(); Node attr = attrs.getNamedItem("file"); String templateFile = attr.getNodeValue(); attr = attrs.getNamedItem("baseName"); String baseName = attr.getNodeValue(); attr = attrs.getNamedItem("output"); String pattern = attr.getNodeValue(); ProjectDirective directive = new ProjectDirective(templateFile, baseName, pattern); target.directives.add(directive); }
private static Category parseCategory(Node n) { NamedNodeMap atts = n.getAttributes(); String name = atts.getNamedItem("name").getNodeValue(); String desc = atts.getNamedItem("description").getNodeValue(); Category cat = new Category(name, desc); NodeList children = n.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { parseNode(children.item(i), cat); } return cat; }
/** * Retrieves default values from xml. * * @param list the configuration list */ private static void parseDefaults(NodeList list) { for (int i = 0; i < list.getLength(); ++i) { NamedNodeMap mapping = list.item(i).getAttributes(); String attribute = mapping.getNamedItem("attribute").getNodeValue(); String value = mapping.getNamedItem("value").getNodeValue(); try { Default field = Default.fromString(attribute); DEFAULTS.put(field, value); } catch (IllegalArgumentException exc) { logger.warn("Unrecognized default attribute: " + attribute); } } }
private void validateInstallHandler(Element element) { NodeList elements = getChildrenByName(element, "install-handler"); // $NON-NLS-1$ if (elements.getLength() > 0) { if (fMonitor.isCanceled()) return; Element handler = (Element) elements.item(0); NamedNodeMap attributes = handler.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { String name = attributes.item(i).getNodeName(); if (!name.equals("library") && !name.equals("handler")) // $NON-NLS-1$ //$NON-NLS-2$ reportUnknownAttribute(handler, name, CompilerFlags.ERROR); } reportExtraneousElements(elements, 1); } }
/** * Get the default namespace associated with the supplied element. * * @param element The element to be checked. * @return The default namespace, or null if none was found. */ public static String getDefaultNamespace(Element element) { NamedNodeMap attributes = element.getAttributes(); int attributeCount = attributes.getLength(); for (int i = 0; i < attributeCount; i++) { Attr attribute = (Attr) attributes.item(i); if (XMLConstants.XMLNS_ATTRIBUTE.equals(attribute.getName()) && XMLConstants.XMLNS_ATTRIBUTE.equals(attribute.getLocalName())) { return attribute.getValue(); } } return null; }
/** * Runs the test case. * * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; NodeList addressList; Node testNode; NamedNodeMap attributes; Attr streetAttr; boolean state; doc = (Document) load("staff", false); addressList = doc.getElementsByTagName("address"); testNode = addressList.item(0); attributes = testNode.getAttributes(); streetAttr = (Attr) attributes.getNamedItem("street"); state = streetAttr.getSpecified(); assertFalse("streetNotSpecified", state); }
/** * Populates LOCALES list with contents of xml. * * @param list the configuration list */ private static void parseLocales(NodeList list) { for (int i = 0; i < list.getLength(); ++i) { Node node = list.item(i); NamedNodeMap attributes = node.getAttributes(); String label = ((Attr) attributes.getNamedItem("label")).getValue(); String code = ((Attr) attributes.getNamedItem("isoCode")).getValue(); String dictLocation = ((Attr) attributes.getNamedItem("dictionaryUrl")).getValue(); try { LOCALES.add(new Locale(label, code, new URL(dictLocation))); } catch (MalformedURLException exc) { logger.warn( "Unable to parse dictionary location of " + label + " (" + dictLocation + ")", exc); } } }
public void validateContent(IProgressMonitor monitor) { Element element = getDocumentRoot(); if (element == null) return; String elementName = element.getNodeName(); if (!"plugin".equals(elementName) && !"fragment".equals(elementName)) { // $NON-NLS-1$ //$NON-NLS-2$ reportIllegalElement(element, CompilerFlags.ERROR); } else { int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_DEPRECATED); if (severity != CompilerFlags.IGNORE) { NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { reportUnusedAttribute(element, attrs.item(i).getNodeName(), severity); } } NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if (monitor.isCanceled()) break; Element child = (Element) children.item(i); String name = child.getNodeName(); if (name.equals("extension")) { // $NON-NLS-1$ validateExtension(child); } else if (name.equals("extension-point")) { // $NON-NLS-1$ validateExtensionPoint(child); } else { if (!name.equals("runtime") && !name.equals("requires")) { // $NON-NLS-1$ //$NON-NLS-2$ severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT); if (severity != CompilerFlags.IGNORE) reportIllegalElement(child, severity); } else { severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_DEPRECATED); if (severity != CompilerFlags.IGNORE) reportUnusedElement(child, severity); } } } IExtensions extensions = fModel.getExtensions(); if (extensions != null && extensions.getExtensions().length == 0 && extensions.getExtensionPoints().length == 0) report( MDECoreMessages.Builders_Manifest_useless_file, -1, IMarker.SEVERITY_WARNING, MDEMarkerFactory.P_USELESS_FILE, MDEMarkerFactory.CAT_OTHER); } }