001/* 002 The contents of this file are subject to the Mozilla Public License Version 1.1 003 (the "License"); you may not use this file except in compliance with the License. 004 You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005 Software distributed under the License is distributed on an "AS IS" basis, 006 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007 specific language governing rights and limitations under the License. 008 009 The Original Code is "MessageVisitors.java ". Description: 010 "Static methods for working with MessageVisitor" 011 012 The Initial Developer of the Original Code is University Health Network. Copyright (C) 013 2013. All Rights Reserved. 014 015 Contributor(s): ______________________________________. 016 017 Alternatively, the contents of this file may be used under the terms of the 018 GNU General Public License (the "GPL"), in which case the provisions of the GPL are 019 applicable instead of those above. If you wish to allow use of your version of this 020 file only under the terms of the GPL and not to allow others to use your version 021 of this file under the MPL, indicate your decision by deleting the provisions above 022 and replace them with the notice and other provisions required by the GPL License. 023 If you do not delete the provisions above, a recipient may use your version of 024 this file under either the MPL or the GPL. 025 */ 026 027package ca.uhn.hl7v2.model; 028 029import ca.uhn.hl7v2.HL7Exception; 030import ca.uhn.hl7v2.Location; 031 032/** 033 * Static methods for working with MessageVisitor 034 */ 035public final class MessageVisitors { 036 037 private MessageVisitors() {} 038 039 /** 040 * Lets a visitor visit a message of parts thereof. 041 * 042 * @param visitor MessageVisitor 043 * @param visitable message element to be visited 044 * @return the MessageVisitor 045 * @throws HL7Exception if an error occurs while visiting 046 */ 047 public static <T extends MessageVisitor> T visit(Visitable visitable, T visitor) throws HL7Exception { 048 visitable.accept(visitor, new Location()); 049 return visitor; 050 } 051 052 /** 053 * Returns a MessageVisitor that only visits structures 054 * @param visitor delegate MessageVisitor instance 055 * @return MessageVisitor that only visits structures 056 */ 057 public static <T extends MessageVisitor> StructuresVisitor<T> visitStructures(T visitor) { 058 return new StructuresVisitor<T>(visitor); 059 } 060 061 /** 062 * Returns a MessageVisitor that only visits populated message elements. 063 * You can further nest delegate visitors, e.g. 064 * <code>structures(populated(new MyVisitor()))</code> 065 * 066 * @param visitor delegate MessageVisitor instance 067 * @return MessageVisitor that only visits populated message elements 068 */ 069 public static <T extends MessageVisitor> PopulatedVisitor<T> visitPopulatedElements(T visitor) { 070 return new PopulatedVisitor<T>(visitor); 071 } 072 073 074 public final static class StructuresVisitor<T extends MessageVisitor> extends DelegatingMessageVisitor<T> { 075 076 StructuresVisitor(T visitor) { 077 super(visitor); 078 } 079 080 @Override 081 public boolean start(Segment segment, Location location) throws HL7Exception { 082 super.start(segment, location); 083 return false; // don't descend into fields 084 } 085 086 @Override 087 public final boolean start(Composite type, Location location) throws HL7Exception { 088 return false; 089 } 090 091 @Override 092 public boolean visit(Primitive type, Location location) throws HL7Exception { 093 return false; 094 } 095 } 096 097 public final static class PopulatedVisitor<T extends MessageVisitor> extends DelegatingMessageVisitor<T> { 098 099 PopulatedVisitor(T visitor) { 100 super(visitor); 101 } 102 103 @Override 104 public boolean start(Group group, Location location) throws HL7Exception { 105 return !group.isEmpty() && super.start(group, location); 106 } 107 108 @Override 109 public boolean end(Group group, Location location) throws HL7Exception { 110 return !group.isEmpty() && super.end(group, location); 111 } 112 113 @Override 114 public boolean start(Segment segment, Location location) throws HL7Exception { 115 return !segment.isEmpty() && super.start(segment, location); 116 } 117 118 @Override 119 public boolean end(Segment segment, Location location) throws HL7Exception { 120 return !segment.isEmpty() && super.end(segment, location); 121 } 122 123 @Override 124 public final boolean start(Composite type, Location location) throws HL7Exception { 125 return !type.isEmpty() && super.start(type, location); 126 } 127 128 @Override 129 public boolean end(Composite type, Location location) throws HL7Exception { 130 return !type.isEmpty() && super.end(type, location); 131 } 132 133 @Override 134 public boolean visit(Primitive type, Location location) throws HL7Exception { 135 return !type.isEmpty() && super.visit(type, location); 136 } 137 138 139 } 140}