1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /*
  18. * Portions of this software are based upon public domain software
  19. * originally written at the National Center for Supercomputing Applications,
  20. * University of Illinois, Urbana-Champaign.
  21. */
  22. package org.apache.tools.ant.taskdefs.optional.perforce;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.Project;
  25. import org.apache.tools.ant.util.StringUtils;
  26. /**
  27. * simple implementation of P4HandlerAdapter used by tasks which are not
  28. * actually processing the output from Perforce
  29. */
  30. public class SimpleP4OutputHandler extends P4HandlerAdapter {
  31. P4Base parent;
  32. /**
  33. * simple constructor
  34. * @param parent a P4Base instance
  35. */
  36. public SimpleP4OutputHandler(P4Base parent) {
  37. this.parent = parent;
  38. }
  39. /**
  40. * process one line of stderr/stdout
  41. * if error conditions are detected, then setters are called on the
  42. * parent
  43. * @param line line of output
  44. * @throws BuildException does not throw exceptions any more
  45. */
  46. public void process(String line) throws BuildException {
  47. if (parent.util.match("/^exit/", line)) {
  48. return;
  49. }
  50. //Throw exception on errors (except up-to-date)
  51. //
  52. //When a server is down, the code expects :
  53. //Perforce client error:
  54. //Connect to server failed; check $P4PORT.
  55. //TCP connect to localhost:1666 failed.
  56. //connect: localhost:1666: Connection refused
  57. //Some forms producing commands (p4 -s change -o) do tag the output
  58. //others don't.....
  59. //Others mark errors as info, for example edit a file
  60. //which is already open for edit.....
  61. //Just look for error: - catches most things....
  62. if (parent.util.match("/^error:/", line)
  63. || parent.util.match("/^Perforce client error:/", line)) {
  64. //when running labelsync, if view elements are in sync,
  65. //Perforce produces a line of output
  66. //looking like this one :
  67. //error: //depot/file2 - label in sync.
  68. if (!parent.util.match("/label in sync/", line)
  69. && !parent.util.match("/up-to-date/", line)) {
  70. parent.setInError(true);
  71. } else {
  72. //sync says "error:" when a file is up-to-date
  73. line = parent.util.substitute("s/^[^:]*: //", line);
  74. }
  75. } else if (parent.util.match("/^info.*?:/", line)) {
  76. //sometimes there's "info1:
  77. line = parent.util.substitute("s/^[^:]*: //", line);
  78. }
  79. parent.log(line, parent.getInError() ? Project.MSG_ERR : Project.MSG_INFO);
  80. if (parent.getInError()) {
  81. parent.setErrorMessage(parent.getErrorMessage() + line + StringUtils.LINE_SEP);
  82. }
  83. }
  84. }