Java Code Examples for java.util.Stack#elementAt()

The following examples show how to use java.util.Stack#elementAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Problem3_1.java    From 99-problems with MIT License 6 votes vote down vote up
public int firstOffendingParenthesis(String input) {
    /*
    Algorithm:
        1. Iterate over all the characters of input String
        2. If character is ')' and stack is not empty then remove element from stack
        3. Else add the position into the stack
        4. After iteration, if stack is empty then return -1 else get the first element and return its value.
     */
    Stack<Integer> stack = new Stack<>();
    for (int i = 0; i < input.length(); i++) {
        if (input.charAt(i) == ')' && !stack.empty()) {
            stack.pop();
        } else {
            stack.push(i);
        }
    }
    return stack.empty() ? -1 : stack.elementAt(0);
}
 
Example 2
Source File: FragmentBuilder.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns the latest node of the specified type,
 * either on the stack, or on the 'popped' nodes.
 *
 * @param nodeType The node type
 * @param onStack Whether from the stack
 * @return The node, or null if not found
 */
public Node getLatestNode(String nodeType, boolean onStack) {
    Node node = null;

    synchronized (nodeStack) {
        Stack<Node> stack = (onStack ? nodeStack : poppedNodes);

        for (int i = 0; node == null && i < stack.size(); i++) {
            Node n = stack.elementAt(i);

            if (log.isLoggable(Level.FINEST)) {
                log.finest("Get latest node: checking node type '" + nodeType
                        + "' against '" + n.getClass().getSimpleName()
                        + "' with node=" + n);
            }

            if (n.getClass().getSimpleName().equals(nodeType)) {
                node = n;
            }
        }
    }

    return node;
}
 
Example 3
Source File: CarbonServerConfigurationService.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private String getKey(Stack<String> nameStack) {
	StringBuffer key = new StringBuffer();
	for (int i = 0; i < nameStack.size(); i++) {
		String name = nameStack.elementAt(i);
		key.append(name).append(".");
	}
	key.deleteCharAt(key.lastIndexOf("."));

	return key.toString();
}
 
Example 4
Source File: IdentityConfigParser.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private String getKey(Stack<String> nameStack) {
    StringBuffer key = new StringBuffer();
    for (int i = 0; i < nameStack.size(); i++) {
        String name = nameStack.elementAt(i);
        key.append(name).append(".");
    }
    key.deleteCharAt(key.lastIndexOf("."));

    return key.toString();
}
 
Example 5
Source File: APIManagerConfiguration.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private String getKey(Stack<String> nameStack) {

        StringBuilder key = new StringBuilder();
        for (int i = 0; i < nameStack.size(); i++) {
            String name = nameStack.elementAt(i);
            key.append(name).append('.');
        }
        key.deleteCharAt(key.lastIndexOf("."));

        return key.toString();
    }
 
Example 6
Source File: IdentityConfigParser.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
private String getKey(Stack<String> nameStack) {
    StringBuilder key = new StringBuilder();
    for (int i = 0; i < nameStack.size(); i++) {
        String name = nameStack.elementAt(i);
        key.append(name).append(".");
    }
    key.deleteCharAt(key.lastIndexOf("."));

    return key.toString();
}
 
Example 7
Source File: ThriftAuthenticationConfigParser.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private String getKey(Stack<String> nameStack) {
    StringBuilder key = new StringBuilder();
    for (int i = 0; i < nameStack.size(); i++) {
        String name = nameStack.elementAt(i);
        key.append(name).append(".");
    }
    key.deleteCharAt(key.lastIndexOf("."));

    return key.toString();
}
 
Example 8
Source File: ThriftAuthenticationConfigParser.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
private String getKey(Stack<String> nameStack) {
    StringBuilder key = new StringBuilder();
    for (int i = 0; i < nameStack.size(); i++) {
        String name = nameStack.elementAt(i);
        key.append(name).append(".");
    }
    key.deleteCharAt(key.lastIndexOf("."));

    return key.toString();
}
 
Example 9
Source File: Deadlock.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
    * Handle a deadlock when it has been detected. Find out if the waiter
    * that started looking for the deadlock is involved in it. If it isn't,
    * pick a victim among the waiters that are involved.
    *
    * @return {@code null} if the waiter that started looking for the deadlock
    * isn't involved in the deadlock (in which case another victim will have
    * been picked and awoken), or an array describing the deadlock otherwise
    */
private static Object[] handle(AbstractPool factory, Stack chain, int start,
							   Dictionary waiters, byte deadlockWake) {

	// If start is zero then the space that started looking for the
	// deadlock is activly involved in the deadlock.

	Object checker = chain.elementAt(0);

	int minLockCount = Integer.MAX_VALUE;
	Object victim = null;
	for (int i = start; i < chain.size(); i++) {
		Object space = chain.elementAt(i);
		if (space instanceof List) {
			continue;
		}

		// See if the checker is in the deadlock and we
		// already picked as a victim
		if ((checker.equals(space)) && (deadlockWake == Constants.WAITING_LOCK_DEADLOCK)) {
			victim = checker;
			break;
		}

		LockSpace ls = (LockSpace) space;
		int spaceCount = ls.deadlockCount(minLockCount);

		if (spaceCount <= minLockCount) {
			victim = space;
			minLockCount = spaceCount;
		}
	}

	// See if the vitim is the one doing the checking
	if (checker.equals(victim)) {
		Object[] data = new Object[2];
		data[0] = chain;
		data[1] = waiters;
		return data;
	}

	ActiveLock victimLock = (ActiveLock) waiters.get(victim);

	victimLock.wakeUp(Constants.WAITING_LOCK_DEADLOCK);

	return null;

}
 
Example 10
Source File: Deadlock.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private static Object[] handle(AbstractPool factory, Stack chain, int start,
							   Dictionary waiters, byte deadlockWake) {

	// If start is zero then the space that started looking for the
	// deadlock is activly involved in the deadlock.

	Object checker = chain.elementAt(0);

	int minLockCount = Integer.MAX_VALUE;
	Object victim = null;
	for (int i = start; i < chain.size(); i++) {
		Object space = chain.elementAt(i);
		if (space instanceof List) {
			continue;
		}

		// See if the checker is in the deadlock and we
		// already picked as a victim
		if ((checker.equals(space)) && (deadlockWake == Constants.WAITING_LOCK_DEADLOCK)) {
			victim = checker;
			break;
		}

		LockSpace ls = (LockSpace) space;
		int spaceCount = ls.deadlockCount(minLockCount);

		if (spaceCount <= minLockCount) {
			victim = space;
			minLockCount = spaceCount;
		}
	}

	// See if the vitim is the one doing the checking
	if (checker.equals(victim)) {
		Object[] data = new Object[2];
		data[0] = chain;
		data[1] = waiters;
		return data;
	}

	ActiveLock victimLock = (ActiveLock) waiters.get(victim);

	victimLock.wakeUp(Constants.WAITING_LOCK_DEADLOCK);

	return null;

}
 
Example 11
Source File: QName.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Construct a QName from a string, resolving the prefix
 * using the given namespace stack. The default namespace is
 * not resolved.
 *
 * @param qname Qualified name to resolve
 * @param namespaces Namespace stack to use to resolve namespace
 * @param validate If true the new QName will be validated and an IllegalArgumentException will
 *                 be thrown if it is invalid.
 */
public QName(String qname, Stack namespaces, boolean validate)
{

  String namespace = null;
  String prefix = null;
  int indexOfNSSep = qname.indexOf(':');

  if (indexOfNSSep > 0)
  {
    prefix = qname.substring(0, indexOfNSSep);

    if (prefix.equals("xml"))
    {
      namespace = S_XMLNAMESPACEURI;
    }
    // Do we want this?
    else if (prefix.equals("xmlns"))
    {
      return;
    }
    else
    {
      int depth = namespaces.size();

      for (int i = depth - 1; i >= 0; i--)
      {
        NameSpace ns = (NameSpace) namespaces.elementAt(i);

        while (null != ns)
        {
          if ((null != ns.m_prefix) && prefix.equals(ns.m_prefix))
          {
            namespace = ns.m_uri;
            i = -1;

            break;
          }

          ns = ns.m_next;
        }
      }
    }

    if (null == namespace)
    {
      throw new RuntimeException(
        XMLMessages.createXMLMessage(
          XMLErrorResources.ER_PREFIX_MUST_RESOLVE,
          new Object[]{ prefix }));  //"Prefix must resolve to a namespace: "+prefix);
    }
  }

  _localName = (indexOfNSSep < 0)
               ? qname : qname.substring(indexOfNSSep + 1);

  if (validate)
  {
      if ((_localName == null) || (!XML11Char.isXML11ValidNCName(_localName)))
      {
         throw new IllegalArgumentException(XMLMessages.createXMLMessage(
          XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); //"Argument 'localName' not a valid NCName");
      }
  }
  _namespaceURI = namespace;
  _prefix = prefix;
  m_hashCode = toString().hashCode();
}
 
Example 12
Source File: Deadlock.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private static Object[] handle(AbstractPool factory, Stack chain, int start,
							   Dictionary waiters, byte deadlockWake) {

	// If start is zero then the space that started looking for the
	// deadlock is activly involved in the deadlock.

	Object checker = chain.elementAt(0);

	int minLockCount = Integer.MAX_VALUE;
	Object victim = null;
	for (int i = start; i < chain.size(); i++) {
		Object space = chain.elementAt(i);
		if (space instanceof List) {
			continue;
		}

		// See if the checker is in the deadlock and we
		// already picked as a victim
		if ((checker.equals(space)) && (deadlockWake == Constants.WAITING_LOCK_DEADLOCK)) {
			victim = checker;
			break;
		}

		LockSpace ls = (LockSpace) space;
		int spaceCount = ls.deadlockCount(minLockCount);

		if (spaceCount <= minLockCount) {
			victim = space;
			minLockCount = spaceCount;
		}
	}

	// See if the vitim is the one doing the checking
	if (checker.equals(victim)) {
		Object[] data = new Object[2];
		data[0] = chain;
		data[1] = waiters;
		return data;
	}

	ActiveLock victimLock = (ActiveLock) waiters.get(victim);

	victimLock.wakeUp(Constants.WAITING_LOCK_DEADLOCK);

	return null;

}
 
Example 13
Source File: CorbaUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static TypeCode getTypeCode(ORB orb,
                                   QName type,
                                   CorbaType obj,
                                   CorbaTypeMap typeMap,
                                   Stack<QName> seenTypes) {
    if (type == null) {
        throw new CorbaBindingException("corba:typemap type or elemtype information required"
                + (obj == null ? "" : " for " + obj)
                + (seenTypes.empty() ? "" : ", Enclosing type: " + seenTypes.elementAt(0)));
    }

    TypeCode tc = null;
    // first see if it is a primitive
    tc = getPrimitiveTypeCode(orb, type);
    if (tc == null && type.equals(CorbaConstants.NT_CORBA_ANY)) {
        // Anys are handled in a special way
        tc = orb.get_primitive_tc(TCKind.from_int(TCKind._tk_any));
    } else if (tc == null) {
        if (typeMap == null) {
            throw new CorbaBindingException("Unable to locate typemap for namespace \""
                                            + type.getNamespaceURI() + "\"");
        }

        tc = typeMap.getTypeCode(type);

        if (tc == null) {
            if (obj == null) {
                obj = typeMap.getType(type.getLocalPart());
                if (obj == null) {
                    throw new CorbaBindingException("Unable to locate object definition");
                }
            }
            tc = getComplexTypeCode(orb, type, obj, typeMap, seenTypes);
            if (tc != null) {
                typeMap.addTypeCode(type, tc);
            }
        }
    }
    if (tc == null) {
        throw new CorbaBindingException("Corba type node with qname " + type + " is not supported");
    }
    return tc;
}
 
Example 14
Source File: QName.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Construct a QName from a string, resolving the prefix
 * using the given namespace stack. The default namespace is
 * not resolved.
 *
 * @param qname Qualified name to resolve
 * @param namespaces Namespace stack to use to resolve namespace
 * @param validate If true the new QName will be validated and an IllegalArgumentException will
 *                 be thrown if it is invalid.
 */
public QName(String qname, Stack namespaces, boolean validate)
{

  String namespace = null;
  String prefix = null;
  int indexOfNSSep = qname.indexOf(':');

  if (indexOfNSSep > 0)
  {
    prefix = qname.substring(0, indexOfNSSep);

    if (prefix.equals("xml"))
    {
      namespace = S_XMLNAMESPACEURI;
    }
    // Do we want this?
    else if (prefix.equals("xmlns"))
    {
      return;
    }
    else
    {
      int depth = namespaces.size();

      for (int i = depth - 1; i >= 0; i--)
      {
        NameSpace ns = (NameSpace) namespaces.elementAt(i);

        while (null != ns)
        {
          if ((null != ns.m_prefix) && prefix.equals(ns.m_prefix))
          {
            namespace = ns.m_uri;
            i = -1;

            break;
          }

          ns = ns.m_next;
        }
      }
    }

    if (null == namespace)
    {
      throw new RuntimeException(
        XMLMessages.createXMLMessage(
          XMLErrorResources.ER_PREFIX_MUST_RESOLVE,
          new Object[]{ prefix }));  //"Prefix must resolve to a namespace: "+prefix);
    }
  }

  _localName = (indexOfNSSep < 0)
               ? qname : qname.substring(indexOfNSSep + 1);

  if (validate)
  {
      if ((_localName == null) || (!XML11Char.isXML11ValidNCName(_localName)))
      {
         throw new IllegalArgumentException(XMLMessages.createXMLMessage(
          XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); //"Argument 'localName' not a valid NCName");
      }
  }
  _namespaceURI = namespace;
  _prefix = prefix;
  m_hashCode = toString().hashCode();
}
 
Example 15
Source File: QName.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a QName from a string, resolving the prefix
 * using the given namespace stack. The default namespace is
 * not resolved.
 *
 * @param qname Qualified name to resolve
 * @param namespaces Namespace stack to use to resolve namespace
 * @param validate If true the new QName will be validated and an IllegalArgumentException will
 *                 be thrown if it is invalid.
 */
public QName(String qname, Stack namespaces, boolean validate)
{

  String namespace = null;
  String prefix = null;
  int indexOfNSSep = qname.indexOf(':');

  if (indexOfNSSep > 0)
  {
    prefix = qname.substring(0, indexOfNSSep);

    if (prefix.equals("xml"))
    {
      namespace = S_XMLNAMESPACEURI;
    }
    // Do we want this?
    else if (prefix.equals("xmlns"))
    {
      return;
    }
    else
    {
      int depth = namespaces.size();

      for (int i = depth - 1; i >= 0; i--)
      {
        NameSpace ns = (NameSpace) namespaces.elementAt(i);

        while (null != ns)
        {
          if ((null != ns.m_prefix) && prefix.equals(ns.m_prefix))
          {
            namespace = ns.m_uri;
            i = -1;

            break;
          }

          ns = ns.m_next;
        }
      }
    }

    if (null == namespace)
    {
      throw new RuntimeException(
        XMLMessages.createXMLMessage(
          XMLErrorResources.ER_PREFIX_MUST_RESOLVE,
          new Object[]{ prefix }));  //"Prefix must resolve to a namespace: "+prefix);
    }
  }

  _localName = (indexOfNSSep < 0)
               ? qname : qname.substring(indexOfNSSep + 1);
               
  if (validate)
  {
      if ((_localName == null) || (!XML11Char.isXML11ValidNCName(_localName))) 
      {
         throw new IllegalArgumentException(XMLMessages.createXMLMessage(
          XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); //"Argument 'localName' not a valid NCName");
      }
  }                 
  _namespaceURI = namespace;
  _prefix = prefix;
  m_hashCode = toString().hashCode();
}
 
Example 16
Source File: QName.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Construct a QName from a string, resolving the prefix
 * using the given namespace stack. The default namespace is
 * not resolved.
 *
 * @param qname Qualified name to resolve
 * @param namespaces Namespace stack to use to resolve namespace
 * @param validate If true the new QName will be validated and an IllegalArgumentException will
 *                 be thrown if it is invalid.
 */
public QName(String qname, Stack namespaces, boolean validate)
{

  String namespace = null;
  String prefix = null;
  int indexOfNSSep = qname.indexOf(':');

  if (indexOfNSSep > 0)
  {
    prefix = qname.substring(0, indexOfNSSep);

    if (prefix.equals("xml"))
    {
      namespace = S_XMLNAMESPACEURI;
    }
    // Do we want this?
    else if (prefix.equals("xmlns"))
    {
      return;
    }
    else
    {
      int depth = namespaces.size();

      for (int i = depth - 1; i >= 0; i--)
      {
        NameSpace ns = (NameSpace) namespaces.elementAt(i);

        while (null != ns)
        {
          if ((null != ns.m_prefix) && prefix.equals(ns.m_prefix))
          {
            namespace = ns.m_uri;
            i = -1;

            break;
          }

          ns = ns.m_next;
        }
      }
    }

    if (null == namespace)
    {
      throw new RuntimeException(
        XMLMessages.createXMLMessage(
          XMLErrorResources.ER_PREFIX_MUST_RESOLVE,
          new Object[]{ prefix }));  //"Prefix must resolve to a namespace: "+prefix);
    }
  }

  _localName = (indexOfNSSep < 0)
               ? qname : qname.substring(indexOfNSSep + 1);

  if (validate)
  {
      if ((_localName == null) || (!XML11Char.isXML11ValidNCName(_localName)))
      {
         throw new IllegalArgumentException(XMLMessages.createXMLMessage(
          XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); //"Argument 'localName' not a valid NCName");
      }
  }
  _namespaceURI = namespace;
  _prefix = prefix;
  m_hashCode = toString().hashCode();
}
 
Example 17
Source File: QName.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Construct a QName from a string, resolving the prefix
 * using the given namespace stack. The default namespace is
 * not resolved.
 *
 * @param qname Qualified name to resolve
 * @param namespaces Namespace stack to use to resolve namespace
 * @param validate If true the new QName will be validated and an IllegalArgumentException will
 *                 be thrown if it is invalid.
 */
public QName(String qname, Stack namespaces, boolean validate)
{

  String namespace = null;
  String prefix = null;
  int indexOfNSSep = qname.indexOf(':');

  if (indexOfNSSep > 0)
  {
    prefix = qname.substring(0, indexOfNSSep);

    if (prefix.equals("xml"))
    {
      namespace = S_XMLNAMESPACEURI;
    }
    // Do we want this?
    else if (prefix.equals("xmlns"))
    {
      return;
    }
    else
    {
      int depth = namespaces.size();

      for (int i = depth - 1; i >= 0; i--)
      {
        NameSpace ns = (NameSpace) namespaces.elementAt(i);

        while (null != ns)
        {
          if ((null != ns.m_prefix) && prefix.equals(ns.m_prefix))
          {
            namespace = ns.m_uri;
            i = -1;

            break;
          }

          ns = ns.m_next;
        }
      }
    }

    if (null == namespace)
    {
      throw new RuntimeException(
        XMLMessages.createXMLMessage(
          XMLErrorResources.ER_PREFIX_MUST_RESOLVE,
          new Object[]{ prefix }));  //"Prefix must resolve to a namespace: "+prefix);
    }
  }

  _localName = (indexOfNSSep < 0)
               ? qname : qname.substring(indexOfNSSep + 1);

  if (validate)
  {
      if ((_localName == null) || (!XML11Char.isXML11ValidNCName(_localName)))
      {
         throw new IllegalArgumentException(XMLMessages.createXMLMessage(
          XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); //"Argument 'localName' not a valid NCName");
      }
  }
  _namespaceURI = namespace;
  _prefix = prefix;
  m_hashCode = toString().hashCode();
}
 
Example 18
Source File: QName.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Construct a QName from a string, resolving the prefix
 * using the given namespace stack. The default namespace is
 * not resolved.
 *
 * @param qname Qualified name to resolve
 * @param namespaces Namespace stack to use to resolve namespace
 * @param validate If true the new QName will be validated and an IllegalArgumentException will
 *                 be thrown if it is invalid.
 */
public QName(String qname, Stack namespaces, boolean validate)
{

  String namespace = null;
  String prefix = null;
  int indexOfNSSep = qname.indexOf(':');

  if (indexOfNSSep > 0)
  {
    prefix = qname.substring(0, indexOfNSSep);

    if (prefix.equals("xml"))
    {
      namespace = S_XMLNAMESPACEURI;
    }
    // Do we want this?
    else if (prefix.equals("xmlns"))
    {
      return;
    }
    else
    {
      int depth = namespaces.size();

      for (int i = depth - 1; i >= 0; i--)
      {
        NameSpace ns = (NameSpace) namespaces.elementAt(i);

        while (null != ns)
        {
          if ((null != ns.m_prefix) && prefix.equals(ns.m_prefix))
          {
            namespace = ns.m_uri;
            i = -1;

            break;
          }

          ns = ns.m_next;
        }
      }
    }

    if (null == namespace)
    {
      throw new RuntimeException(
        XMLMessages.createXMLMessage(
          XMLErrorResources.ER_PREFIX_MUST_RESOLVE,
          new Object[]{ prefix }));  //"Prefix must resolve to a namespace: "+prefix);
    }
  }

  _localName = (indexOfNSSep < 0)
               ? qname : qname.substring(indexOfNSSep + 1);

  if (validate)
  {
      if ((_localName == null) || (!XML11Char.isXML11ValidNCName(_localName)))
      {
         throw new IllegalArgumentException(XMLMessages.createXMLMessage(
          XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); //"Argument 'localName' not a valid NCName");
      }
  }
  _namespaceURI = namespace;
  _prefix = prefix;
  m_hashCode = toString().hashCode();
}
 
Example 19
Source File: QName.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Construct a QName from a string, resolving the prefix
 * using the given namespace stack. The default namespace is
 * not resolved.
 *
 * @param qname Qualified name to resolve
 * @param namespaces Namespace stack to use to resolve namespace
 * @param validate If true the new QName will be validated and an IllegalArgumentException will
 *                 be thrown if it is invalid.
 */
public QName(String qname, Stack namespaces, boolean validate)
{

  String namespace = null;
  String prefix = null;
  int indexOfNSSep = qname.indexOf(':');

  if (indexOfNSSep > 0)
  {
    prefix = qname.substring(0, indexOfNSSep);

    if (prefix.equals("xml"))
    {
      namespace = S_XMLNAMESPACEURI;
    }
    // Do we want this?
    else if (prefix.equals("xmlns"))
    {
      return;
    }
    else
    {
      int depth = namespaces.size();

      for (int i = depth - 1; i >= 0; i--)
      {
        NameSpace ns = (NameSpace) namespaces.elementAt(i);

        while (null != ns)
        {
          if ((null != ns.m_prefix) && prefix.equals(ns.m_prefix))
          {
            namespace = ns.m_uri;
            i = -1;

            break;
          }

          ns = ns.m_next;
        }
      }
    }

    if (null == namespace)
    {
      throw new RuntimeException(
        XMLMessages.createXMLMessage(
          XMLErrorResources.ER_PREFIX_MUST_RESOLVE,
          new Object[]{ prefix }));  //"Prefix must resolve to a namespace: "+prefix);
    }
  }

  _localName = (indexOfNSSep < 0)
               ? qname : qname.substring(indexOfNSSep + 1);

  if (validate)
  {
      if ((_localName == null) || (!XML11Char.isXML11ValidNCName(_localName)))
      {
         throw new IllegalArgumentException(XMLMessages.createXMLMessage(
          XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); //"Argument 'localName' not a valid NCName");
      }
  }
  _namespaceURI = namespace;
  _prefix = prefix;
  m_hashCode = toString().hashCode();
}
 
Example 20
Source File: QName.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a QName from a string, resolving the prefix
 * using the given namespace stack. The default namespace is
 * not resolved.
 *
 * @param qname Qualified name to resolve
 * @param namespaces Namespace stack to use to resolve namespace
 * @param validate If true the new QName will be validated and an IllegalArgumentException will
 *                 be thrown if it is invalid.
 */
public QName(String qname, Stack namespaces, boolean validate)
{

  String namespace = null;
  String prefix = null;
  int indexOfNSSep = qname.indexOf(':');

  if (indexOfNSSep > 0)
  {
    prefix = qname.substring(0, indexOfNSSep);

    if (prefix.equals("xml"))
    {
      namespace = S_XMLNAMESPACEURI;
    }
    // Do we want this?
    else if (prefix.equals("xmlns"))
    {
      return;
    }
    else
    {
      int depth = namespaces.size();

      for (int i = depth - 1; i >= 0; i--)
      {
        NameSpace ns = (NameSpace) namespaces.elementAt(i);

        while (null != ns)
        {
          if ((null != ns.m_prefix) && prefix.equals(ns.m_prefix))
          {
            namespace = ns.m_uri;
            i = -1;

            break;
          }

          ns = ns.m_next;
        }
      }
    }

    if (null == namespace)
    {
      throw new RuntimeException(
        XMLMessages.createXMLMessage(
          XMLErrorResources.ER_PREFIX_MUST_RESOLVE,
          new Object[]{ prefix }));  //"Prefix must resolve to a namespace: "+prefix);
    }
  }

  _localName = (indexOfNSSep < 0)
               ? qname : qname.substring(indexOfNSSep + 1);

  if (validate)
  {
      if ((_localName == null) || (!XML11Char.isXML11ValidNCName(_localName)))
      {
         throw new IllegalArgumentException(XMLMessages.createXMLMessage(
          XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); //"Argument 'localName' not a valid NCName");
      }
  }
  _namespaceURI = namespace;
  _prefix = prefix;
  m_hashCode = toString().hashCode();
}