public void startElement()

in sources/java-incremental-compilation/jvm-inc-builder/src/com/intellij/tools/build/bazel/org/jdom/input/sax/SAXHandler.java [528:679]


  public void startElement(final String namespaceURI, String localName,
                           final String qName, final Attributes atts) throws SAXException {
    if (suppress) {
      return;
    }

    String prefix = "";

    // If QName is set, then set prefix and local name as necessary
    if (!"".equals(qName)) {
      final int colon = qName.indexOf(':');

      if (colon > 0) {
        prefix = qName.substring(0, colon);
      }

      // If local name is not set, try to get it from the QName
      if ((localName == null) || (localName.isEmpty())) {
        localName = qName.substring(colon + 1);
      }
    }
    // At this point either prefix and localName are set correctly or
    // there is an error in the parser.

    final Namespace namespace = Namespace
      .getNamespace(prefix, namespaceURI);
    final Element element = currentLocator == null ? factory.element(
      localName, namespace) : factory.element(
      currentLocator.getLineNumber(),
      currentLocator.getColumnNumber(), localName, namespace);

    // Take leftover declared namespaces and add them to this element's
    // map of namespaces
    if (!declaredNamespaces.isEmpty()) {
      transferNamespaces(element);
    }

    flushCharacters();

    if (atRoot) {
      factory.setRoot(currentDocument, element); // Yes, use a factory
      // call...
      atRoot = false;
    }
    else {
      factory.addContent(getCurrentElement(), element);
    }
    currentElement = element;

    // Handle attributes
    for (int i = 0, len = atts.getLength(); i < len; i++) {

      String attPrefix = "";
      String attLocalName = atts.getLocalName(i);
      final String attQName = atts.getQName(i);
      final boolean specified = !(atts instanceof Attributes2) || ((Attributes2)atts).isSpecified(i);

      // If attribute QName is set, then set attribute prefix and
      // attribute local name as necessary
      if (!attQName.isEmpty()) {
        // Bypass any xmlns attributes which might appear, as we got
        // them already in startPrefixMapping(). This is sometimes
        // necessary when SAXHandler is used with another source than
        // SAXBuilder, as with JDOMResult.
        if (attQName.startsWith("xmlns:") || attQName.equals("xmlns")) {
          continue;
        }

        final int attColon = attQName.indexOf(':');

        if (attColon > 0) {
          attPrefix = attQName.substring(0, attColon);
        }

        // If localName is not set, try to get it from the QName
        if ("".equals(attLocalName)) {
          attLocalName = attQName.substring(attColon + 1);
        }
      }

      final AttributeType attType = AttributeType.getAttributeType(atts
                                                                     .getType(i));
      final String attValue = atts.getValue(i);
      final String attURI = atts.getURI(i);

      if (XMLConstants.XMLNS_ATTRIBUTE.equals(attLocalName)
          || XMLConstants.XMLNS_ATTRIBUTE.equals(attPrefix)
          || XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attURI)) {
        // use the actual Namespace to check too, because, in theory, a
        // namespace-aware parser does not need to set the qName unless
        // the namespace-prefixes feature is set as well.
        continue;
      }
      // At this point either attPrefix and attLocalName are set
      // correctly or there is an error in the parser.

      // just one thing to sort out....
      // the prefix for the namespace.
      if (!"".equals(attURI) && attPrefix.isEmpty()) {
        // the localname and qName are the same, but there is a
        // Namspace URI. We need to figure out the namespace prefix.
        // this is an unusual condition. Currently the only known
        // trigger
        // is when there is a fixed/defaulted attribute from a
        // validating
        // XMLSchema, and the attribute is in a different namespace
        // than the rest of the document, this happens whenever there
        // is an attribute definition that has form="qualified".
        // <xs:attribute name="attname" form="qualified" ... />
        // or the schema sets attributeFormDefault="qualified"
        final HashMap<String, Namespace> tmpmap = new HashMap<>();
        for (final Namespace nss : element.getNamespacesInScope()) {
          if (!nss.getPrefix().isEmpty()
              && nss.getURI().equals(attURI)) {
            attPrefix = nss.getPrefix();
            break;
          }
          tmpmap.put(nss.getPrefix(), nss);
        }

        if (attPrefix.isEmpty()) {
          // we cannot find a 'prevailing' namespace that has a prefix
          // that is for this namespace.
          // This basically means that there's an XMLSchema, for the
          // DEFAULT namespace, and there's a defaulted/fixed
          // attribute definition in the XMLSchema that's targeted
          // for this namespace,... but, the user has either not
          // declared a prefixed version of the namespace, or has
          // re-declared the same prefix at a lower level with a
          // different namespace.
          // All of these things are possible.
          // Create some sort of default prefix.
          int cnt = 0;
          final String base = "attns";
          String pfx = base + cnt;
          while (tmpmap.containsKey(pfx)) {
            cnt++;
            pfx = base + cnt;
          }
          attPrefix = pfx;
        }
      }
      final Namespace attNs = Namespace.getNamespace(attPrefix, attURI);

      final Attribute attribute = factory.attribute(attLocalName, attValue, attType, attNs);
      if (!specified) {
        // it is a DTD defaulted value.
        attribute.setSpecified(false);
      }
      factory.setAttribute(element, attribute);
    }
  }