Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 2158 Details for
Bug 22208
requesting more options for 'compress package name segments' [package explorer] [browsing]
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
PackageNameCompressor class implementing suggested package name compression
PackageNameCompressor.java (text/plain), 12.65 KB, created by
Robert Varga
on 2002-10-10 15:16:02 EDT
(
hide
)
Description:
PackageNameCompressor class implementing suggested package name compression
Filename:
MIME Type:
Creator:
Robert Varga
Created:
2002-10-10 15:16:02 EDT
Size:
12.65 KB
patch
obsolete
>package test; > >import java.util.StringTokenizer; >import java.util.Vector; > >/** > * @author robert.varga > * > * To change this generated comment edit the template variable "typecomment": > * Window>Preferences>Java>Templates. > * To enable and disable the creation of type comments go to > * Window>Preferences>Java>Code Generation. > */ > >public class PackageNameCompressor { > > public static class Zone { > private int length; > private boolean processIfNoMore; > > private char replaceCharacter; > private int keepCharacters; > private boolean displayMiddleDots; > private boolean displayAfterDot; > > /** > * Returns the displayAfterDot. > * @return boolean > */ > public boolean isDisplayAfterDot() { > return displayAfterDot; > } > > /** > * Returns the displayMiddleDots. > * @return boolean > */ > public boolean isDisplayMiddleDots() { > return displayMiddleDots; > } > > /** > * Returns the keepCharacters. > * @return int > */ > public int getKeepCharacters() { > return keepCharacters; > } > > /** > * Returns the length. > * @return int > */ > public int getLength() { > return length; > } > > /** > * Returns the processIfNoMore. > * @return boolean > */ > public boolean isProcessIfNoMore() { > return processIfNoMore; > } > > /** > * Returns the replaceCharacter. > * @return char > */ > public char getReplaceCharacter() { > return replaceCharacter; > } > > /** > * Sets the displayAfterDot. > * @param displayAfterDot The displayAfterDot to set > */ > public void setDisplayAfterDot(boolean displayAfterDot) { > this.displayAfterDot = displayAfterDot; > } > > /** > * Sets the displayMiddleDots. > * @param displayMiddleDots The displayMiddleDots to set > */ > public void setDisplayMiddleDots(boolean displayMiddleDots) { > this.displayMiddleDots = displayMiddleDots; > } > > /** > * Sets the keepCharacters. > * @param keepCharacters The keepCharacters to set > */ > public void setKeepCharacters(int keepCharacters) { > this.keepCharacters = keepCharacters; > } > > /** > * Sets the length. > * @param length The length to set > */ > public void setLength(int length) { > this.length = length; > } > > /** > * Sets the processIfNoMore. > * @param processIfNoMore The processIfNoMore to set > */ > public void setProcessIfNoMore(boolean processIfNoMore) { > this.processIfNoMore = processIfNoMore; > } > > /** > * Sets the replaceCharacter. > * @param replaceCharacter The replaceCharacter to set > */ > public void setReplaceCharacter(char replaceCharacter) { > this.replaceCharacter = replaceCharacter; > } > > public String toString() { > return "Zone: length: "+length+" processIfNoMore: "+processIfNoMore+" middle: "+displayMiddleDots+" after: "+displayAfterDot+" keep: "+keepCharacters+" replace: '"+replaceCharacter+"'"; > } > } > > public static void processSegment(StringBuffer buf, String segment, Zone zone, > boolean endOfZone, boolean followingSegmentExists) { > > //System.out.println("Processing segment: "+segment); > if (zone==null) { > // not compress this segment > //System.out.print("No zone, not compressing. Output: "+segment); > buf.append(segment); > if (followingSegmentExists) { > buf.append('.'); > //System.out.print('.'); > } > //System.out.println(); > } else { > // compress this segment > //System.out.println("Zone: "+zone); > //System.out.print("Output: "); > if (zone.getKeepCharacters()>0) { > buf.append(segment.substring(0, zone.getKeepCharacters())); > //System.out.print(segment.substring(0, zone.getKeepCharacters())); > } > if (segment.length()>zone.getKeepCharacters()) { > if (zone.getReplaceCharacter()!=' ') { > buf.append(zone.getReplaceCharacter()); > //System.out.print(zone.getReplaceCharacter()); > } > } > if (followingSegmentExists) { > if (endOfZone) { > if (zone.isDisplayAfterDot() ) { > buf.append('.'); > //System.out.print('.'); > } > } else { > if (zone.isDisplayMiddleDots() ) { > buf.append('.'); > //System.out.print('.'); > } > } > } > //System.out.println(); > } > } > > public static String compressPackageName(String packageName, Zone[] zones) { > > if (zones==null || packageName==null) return packageName; > if (packageName.length()==0 || zones.length==0) return packageName; > > int zoneNum, segNum=0; > if ((zoneNum= packageName.length()-1)>-1) ++segNum; > for (; zoneNum>-1; --zoneNum) if (packageName.charAt(zoneNum)=='.') ++segNum; > zoneNum=zones.length; > > > int processible; > boolean lastNegative=false; > boolean next; > if ((processible=zones[zoneNum-1].getLength())<1) { > > if (segNum<=-processible) return packageName; > > lastNegative=true; > processible+=segNum; > > } else processible=segNum; > > //System.out.println("Zones: "+zoneNum+" LastNegative: "+lastNegative+" Segments: "+segNum+" Processible: "+processible); > > StringBuffer buf=new StringBuffer(packageName.length()); > int zone=0, segment=0, num=0; > StringTokenizer tok= new StringTokenizer(packageName, ".", false); > String actseg; > > > while ((zone<zoneNum) && (segment<processible)) { > //System.out.println("Inside loop: zone #"+zone+": "+zones[zone]+" , segment: "+segment); > > > if (lastNegative && zone==zoneNum-1) { > // last zone and it is negative: process all segments > // System.out.println("Last negative zone"); > while (segment<processible) { > actseg= tok.nextToken(); > ++segment; > processSegment(buf, actseg, zones[zone], segment==processible, segment<segNum); > } > break; > > } else { > //System.out.println("segment: "+segment+" zones[zone].length: "+zones[zone].getLength()+" processible: "+processible); > if (segment+zones[zone].getLength() >= processible) { >// || (segment+zones[zone].getLength() == segNum)) {// that implies segment+zones[zone].getLength() == processible as well > > // no segments after zone > > if (! zones[zone].processIfNoMore) { > // should not process if no segments after zone > if (!lastNegative) break; // break away, nothing more should be done ; > > // last zone is negative, go to last zone > zone=zoneNum-1; > continue; > } > } > > // enough segments or forced to process an incomplete positive segment > num=0; > //System.out.println("processing item in loop"); > while (segment<processible && num<zones[zone].getLength()) { > actseg= tok.nextToken(); > ++segment; ++num; > processSegment(buf, actseg, zones[zone], > (segment==processible) || (num==zones[zone].getLength()), segment<segNum); > } > ++zone; > } > } > > // append remaining segments > //System.out.println("appending unprocessed items"); > while (segment<segNum) { > actseg= tok.nextToken(); > ++segment; > processSegment(buf, actseg, null, false, segment<segNum); > } > > return buf.toString(); > } > > public static Zone[] parseZones(String pattern) { > Vector v=new Vector(); > StringTokenizer tok=new StringTokenizer(pattern, " ", false); > Zone zone; > String token; > while (tok.hasMoreTokens()) { > /* > * format = [<keepchars>=0][<replacechar>=' '][<dotchar=[. : >][{'/'|'\'}<length>] > * at least one of keepchars replacechar dotchar should be given > * > * dotchar meaning: . = both middleDot and afterDot is true > * ! = both middleDot and afterDot is false > * * = only afterDot is true > * | = only middleDot is true (not too meaningful) > * > * / in length means, process only if there is enough unprocessed segments > * \ in length means, process also when there are no more unprocessed segments after the zone > > * replacechar cannot be space > */ > token=tok.nextToken(); > int len=-1,keep=0; > boolean middle=false, after=false, process=false; > char replace=' ', dot='!'; > int ind, index; > if (-1==(index=token.lastIndexOf('/'))) if (-1<(index=token.lastIndexOf('\\'))) process=true; > if (index==0) throw new Exception("Zone definition is corrupt. Compression specifier must be present! Zone: "+token); > ind=-1; > if (ind==-1) if (-1<(ind=token.lastIndexOf('!'))) { > dot='!'; > middle=after=false; > } > if (ind==-1) if (-1<(ind=token.lastIndexOf('*'))) { > dot='*'; > middle=false; > after=true; > } > if (ind==-1) if (-1<(ind=token.lastIndexOf('.'))) { > dot='.'; > middle=after=true; > } > if (ind==-1) if (-1<(ind=token.lastIndexOf('|'))) { > dot='|'; > middle=true; > after=false; > } > if ((index==-1) && (ind>-1) && (ind<token.length()-1)) { > throw new Exception("Zone definition is corrupt. Dotchar must be the end of the zone, or it must be followed by a slash or backslash and optionally specifying the length of the zone! Zone: "+token); > } > > if (index>-1 && index<token.length()-1) len=Integer.parseInt(token.substring(index+1),10); > > if (ind>0) { > // there is place before the dotchar > replace= token.charAt(ind-1); > if ((replace<='9')&&(replace>='0')) replace=' '; > else --ind; > } else if (ind==-1) { > if (index==-1) replace=token.charAt(token.length()-1); > else replace=token.charAt(index-1); > } > > if (ind>0) keep=Integer.parseInt(token.substring(0,ind),10); > else if (ind==-1) { > if (index>-1) keep=Integer.parseInt(token.substring(0, index),10); > else keep=Integer.parseInt(token, 10); > } > > v.add(zone=new Zone()); > zone.setDisplayAfterDot(after); > zone.setDisplayMiddleDots(middle); > zone.setLength(len); > zone.setKeepCharacters(keep); > zone.setReplaceCharacter(replace); > zone.setProcessIfNoMore(process); > System.out.println("New "+zone); > } > Zone[] ret=new Zone[v.size()]; > v.copyInto(ret); > return ret; > } > > public static void testCompress(String pattern, String packageName) { > Zone[] zones= parseZones(pattern); > String result= compressPackageName(packageName, zones); > System.out.println("Pattern: "+pattern+" Package: "+packageName+" Compressed: "+result); > } > > public static void main(String[] args) { > if (args.length<2) System.out.println("Usage: java test.PackageNameCompressor pattern packagename"); > else testCompress(args[0], args[1]); > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 22208
: 2158 |
2159
|
2160
|
2163
|
2173