Package com.sun.jsftemplating.layout.descriptors.handler

Examples of com.sun.jsftemplating.layout.descriptors.handler.Handler


  }
  if (def == null) {
      throw new IllegalArgumentException(
    "Unable to locate handler definition for '" + handlerId + "'!");
  }
  Handler handler = new Handler(def);
  if (args != null) {
      // Basic check to make sure we have valid arguments
      int size = args.length;
      if ((size % 2) == 1) {
    throw new IllegalArgumentException("Arguments to "
      + "dispatchHandler must be paired: name1, value1, "
      + "name2, value2.  An odd number was received which "
      + "is invalid.");
      }

      // Set all the input values
      String name = null;
      Object value = null;
      for (int count=0; count<size; count += 2) {
    name = (String) args[count];
    value = args[count+1];
    handler.setInputValue(name, value);
      }
  }

  // Put it in a List
  List<Handler> handlers = new ArrayList<Handler>();
View Full Code Here


     *
     *  <p> The {@link ProcessingContext} and
     *      {@link ProcessingContextEnvironment} are both available.</p>
     */
    public void process(ProcessingContext ctx, ProcessingContextEnvironment env, String eventName) throws IOException {
  Handler handler = null;
  List<Handler> handlers = new ArrayList<Handler>();
  TemplateReader reader = env.getReader();
  TemplateParser parser = reader.getTemplateParser();
  Handler parentHandler = null;
  Stack<Handler> handlerStack = new Stack<Handler>();
  LayoutElement parent = env.getParent();

  // Skip whitespace...
  parser.skipCommentsAndWhiteSpace(TemplateParser.SIMPLE_WHITE_SPACE);
  int ch = -1;

  // We now support 2 syntaxes:
  //   <!event type="beforeCreate">[handlers]</event>
  //   <!beforeCreate [handlers] />
  // If "eventName" is event, look for type and the closing '>' before
  // trying to parse the handlers.
  boolean useBodyContent = false;
  boolean createHandlerDefinitionOnLayoutDefinition = false;
  if (eventName.equals("handler")) {
      // We have a <handler id="foo"> tag...
      createHandlerDefinitionOnLayoutDefinition = true;
      useBodyContent = true;

      // Read type="...", no other options are supported at this time
      NameValuePair nvp = parser.getNVP(null);
      if (!nvp.getName().equals("id")) {
    throw new SyntaxException(
        "When defining and event, you must supply the event type! "
        + "Found \"...event " + nvp.getName() + "\" instead.");
      }
      eventName = nvp.getValue().toString();

      // Ensure the next character is '>'
      parser.skipCommentsAndWhiteSpace(TemplateParser.SIMPLE_WHITE_SPACE);
      ch = parser.nextChar();
      if (ch != '>') {
    throw new SyntaxException(
        "Syntax error in event definition, found: '...handler id=\""
        + eventName + "\" " + ((char) ch)
        + "\'.  Expected closing '>' for opening handler element.");
      }

      // Get ready to read the handlers now...
      parser.skipCommentsAndWhiteSpace(TemplateParser.SIMPLE_WHITE_SPACE);
      ch = parser.nextChar();
  } else if (eventName.equals("event")) {
      // We have the new syntax...
      useBodyContent = true;

      // Read type="...", no other options are supported at this time
      NameValuePair nvp = parser.getNVP(null);
      if (!nvp.getName().equals("type")) {
    throw new SyntaxException(
        "When defining and event, you must supply the event type! "
        + "Found \"...event " + nvp.getName() + "\" instead.");
      }
      eventName = nvp.getValue().toString();

      // Ensure the next character is '>'
      parser.skipCommentsAndWhiteSpace(TemplateParser.SIMPLE_WHITE_SPACE);
      ch = parser.nextChar();
      if (ch != '>') {
    throw new SyntaxException(
        "Syntax error in event definition, found: '...event type=\""
        + eventName + "\" " + ((char) ch)
        + "\'.  Expected closing '>' for opening event element.");
      }

      // Get ready to read the handlers now...
      parser.skipCommentsAndWhiteSpace(TemplateParser.SIMPLE_WHITE_SPACE);
      ch = parser.nextChar();
  } else {
      // Make sure to read the first char for the old syntax...
      ch = parser.nextChar();
  }

  // Read the Handler(s)...
  while (ch != -1) {
      if (useBodyContent) {
    // If we're using the new format.... check for "</event>"
    if (ch == '<') {
        // Just unread the '<', framework will validate the rest
        parser.unread('<');
        break;
    }
      } else {
    if ((ch == '/') || (ch == '>')) {
        // We found the end in the case where the handlers are
        // inside the tag (old syntax).
        break;
    }
      }
      // Check for {}'s
      if ((ch == LEFT_CURLY) || (ch == RIGHT_CURLY)) {
    if (ch == LEFT_CURLY) {
        // We are defining child handlers
        handlerStack.push(parentHandler);
        parentHandler = handler;
    } else {
        // We are DONE defining child handlers
        if (handlerStack.empty()) {
      throw new SyntaxException("Encountered unmatched '"
          + RIGHT_CURLY + "' when parsing handlers for '"
          + eventName + "' event.");
        }
        parentHandler = handlerStack.pop();
    }

    // ';' or ',' characters may appear between handlers
    parser.skipCommentsAndWhiteSpace(
      TemplateParser.SIMPLE_WHITE_SPACE + ",;");

    // We need to "continue" b/c we need to check next ch again
    ch = parser.nextChar();
    continue;
      }

      // Get Handler ID / Definition
      parser.unread(ch);

      // Read a Handler
      handler = readHandler(parser, eventName, parent);

      // Add the handler to the appropriate place
      if (parentHandler == null) {
    handlers.add(handler);
      } else {
    parentHandler.addChildHandler(handler);
      }

      // Look at the next character...
      ch = parser.nextChar();
  }
View Full Code Here

        + "build).");
      }
  }

  // Create a Handler
  Handler handler = new Handler(def);

  // Get the default name
  Map inputs = def.getInputDefs();
// FIXME: Allow for HandlerDefs to declare their default input
  if (inputs.size() == 1) {
      defVal = inputs.keySet().toArray()[0].toString();
  }

  // Get the outputs so we can see what outputs have been declared
  Map outputs = def.getOutputDefs();

  // Ensure we have an opening parenthesis
  parser.skipCommentsAndWhiteSpace(TemplateParser.SIMPLE_WHITE_SPACE);
  int ch = parser.nextChar();
  if (ch != '(') {
      throw new SyntaxException("While processing '&lt;!" + eventName
    + "...' the handler '" + handlerId
    + "' was missing the '(' character!");
  }

  // Move to the first char inside the parenthesis
  parser.skipWhiteSpace(TemplateParser.SIMPLE_WHITE_SPACE);
  ch = parser.nextChar();

  // We should not ignore '#' characters for 'if' (Issue #5)
  if ((ch != '#') || !handlerId.equals(IF_HANDLER)) {
      parser.unread(ch);
      parser.skipCommentsAndWhiteSpace(""); // Already skipped white
      ch = parser.nextChar();
  }

  // Allow if() handlers to be more flexible...
  if (handlerId.equals(IF_HANDLER)
    && (ch != '\'') && (ch != '"') && (ch != 'c')) {
// FIXME: check for "condition", otherwise expressions starting with 'c' will
// FIXME: not parse correctly
      // We have an if() w/o a condition="" && w/o quotes...
      // Take the entire value inside the ()'s to be the expression
      parser.unread(ch);
      handler.setCondition(parser.readUntil(')', false).trim());
      ch = ')';
  }

  // Read NVP(s)...
  while ((ch != -1) && (ch != ')')) {
      // Read NVP
      parser.unread(ch);
      try {
    nvp = parser.getNVP(defVal);
      } catch (SyntaxException ex) {
    throw new SyntaxException("Unable to process handler '"
      + handlerId + "'!", ex);
      }
      parser.skipCommentsAndWhiteSpace(
    TemplateParser.SIMPLE_WHITE_SPACE + ",;");
      ch = parser.nextChar();

      // Store the NVP..
      target = nvp.getTarget();
      if (target != null) {
    // "old-style" OutputMapping (key=>$attribute{value})
    // NOTE: 'value' must be a String for an OutputMapping
    handler.setOutputMapping(
        nvp.getName(), nvp.getValue().toString(), target);
      } else {
    // First check for special input value (condition)
    String name = nvp.getName();
    if (name.equals(CONDITION_ATTRIBUTE)
        && ((inputs.get(CONDITION_ATTRIBUTE) == null)
      || (handlerId.equals(IF_HANDLER)))) {
        // We have a Handler condition, set it
        handler.setCondition(nvp.getValue().toString());
    } else {
        // We still don't know if this is an input, output, or both
        // (EL is now supported as an output mapping: out="#{el}")
        boolean validIO = false;
        // We also check to see if the "old" output mapping was
        // used and DO NOT override it if it was.  This is useful
        // if there are cases where an input and output share a
        // name and the user does not fix this... the old syntax
        // can reliably declare an output without a namespace
        // problem.
        if (outputs.containsKey(name) && (handler.getOutputValue(name) == null)) {
      // We have an Output... use 2 arg method for this
      // syntax (expects EL, or uses simple String for a
      // request attribute).
      handler.setOutputMapping(
        name, nvp.getValue().toString(),
        OutputTypeManager.EL_TYPE);
      validIO = true;
        }
        // Don't do "else" b/c it may be BOTH an input AND output
        if (inputs.containsKey(name)) {
      // We have an Input
      handler.setInputValue(name, nvp.getValue());
      validIO = true;
        }
        if (!validIO) {
      throw new IllegalArgumentException(
          "Input or output named \"" + name
View Full Code Here

  Locale locale = (Locale) descriptor.getEvaluatedOption(context, "locale", parent);

  // Create a handler (needed to execute code each time displayed)...
  HandlerDefinition def = LayoutDefinitionManager.
    getGlobalHandlerDefinition("setResourceBundle");
  Handler handler = new Handler(def);
  handler.setInputValue("bundle", baseName);
  handler.setInputValue("key", var);
  handler.setInputValue("locale", locale);
  List<Handler> handlers = new ArrayList<Handler>();
  handlers.add(handler);
  event.getAttributes().put("beforeEncode", handlers);

  // Return (parent)
View Full Code Here

        + HANDLER_DEFINITION_ELEMENT + " with a matching "
        + ID_ATTRIBUTE + " attribute.");
  }

  // Create new Handler
  Handler handler =  new Handler(handlerDef);

  // Add the inputs
  Map<String, String> attributes = null;
  Node inputNode = null;
  Iterator<Node> it = getChildElements(handlerNode, INPUT_ELEMENT).iterator();
  while (it.hasNext()) {
      // Processing an INPUT_ELEMENT
      inputNode = it.next();
      attributes = getAttributes(inputNode);
      handler.setInputValue(attributes.get(NAME_ATTRIBUTE),
        getValueFromNode(inputNode, attributes));
  }

  // Add the OutputMapping objects
  it = getChildElements(handlerNode, OUTPUT_MAPPING_ELEMENT).iterator();
  while (it.hasNext()) {
      // Processing an OUTPUT_MAPPING_ELEMENT
      attributes = getAttributes(it.next());
      handler.setOutputMapping(
    attributes.get(OUTPUT_NAME_ATTRIBUTE),
    attributes.get(TARGET_KEY_ATTRIBUTE),
    attributes.get(TARGET_TYPE_ATTRIBUTE));
  }
View Full Code Here

TOP

Related Classes of com.sun.jsftemplating.layout.descriptors.handler.Handler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.