{"id":20227733,"url":"https://github.com/niesfisch/tokenreplacer","last_synced_at":"2025-04-10T17:24:34.943Z","repository":{"id":1076422,"uuid":"918778","full_name":"niesfisch/tokenreplacer","owner":"niesfisch","description":"Token Replacer is a simple and small Java Library that helps replacing tokens in strings. You can replace the tokens with static values or create values \"on-the-fly\" by calling a generator. You can even pass arguments to the generator which makes it pretty powerful.","archived":false,"fork":false,"pushed_at":"2020-10-13T09:23:13.000Z","size":131,"stargazers_count":13,"open_issues_count":2,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T15:03:58.431Z","etag":null,"topics":["generator","java","regex","replacement","tokenizer"],"latest_commit_sha":null,"homepage":"www.marcel-sauer.de","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/niesfisch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-09-17T16:10:23.000Z","updated_at":"2022-10-20T07:21:51.000Z","dependencies_parsed_at":"2022-08-16T12:00:17.665Z","dependency_job_id":null,"html_url":"https://github.com/niesfisch/tokenreplacer","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niesfisch%2Ftokenreplacer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niesfisch%2Ftokenreplacer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niesfisch%2Ftokenreplacer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niesfisch%2Ftokenreplacer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/niesfisch","download_url":"https://codeload.github.com/niesfisch/tokenreplacer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248261963,"owners_count":21074229,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["generator","java","regex","replacement","tokenizer"],"created_at":"2024-11-14T07:26:17.754Z","updated_at":"2025-04-10T17:24:34.918Z","avatar_url":"https://github.com/niesfisch.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":" [\u003cimg src=\"https://api.travis-ci.org/niesfisch/tokenreplacer.png\"/\u003e](http://travis-ci.org/niesfisch/tokenreplacer/builds)\n\n# What's that for? \n\nToken Replacer is a simple and small Java Library that helps replacing tokens in strings.\n\nYou can replace tokens with **simple static strings**:\n```Java\nString toReplace = \"i can count to {number}\";\nString result = new Toky().register(\"number\", \"123\").substitute(toReplace);\nSystem.out.println(result); // i can count to 123\n```\nor strings generated **\"on-the-fly\"**: \n```Java\nString toReplace = \"i can count to {number}\";\nString result = new Toky().register(new Token(\"number\").replacedBy(new Generator() {\n    \n\t@Override\n\tpublic void inject(String[] args) {\n\t    // store the arguments\n\t}\n\n\t@Override\n\tpublic String generate() {\n\t    return \"123\"; // some very sophisticated stuff happens here :), we just return 123 to keep it simple\n\t}\n})).substitute(toReplace);\nSystem.out.println(result); // i can count to 123\n```\nYou can even **pass arguments** to the generator which makes it pretty powerful:\n```Java\nString toReplace = \"i can count to {number(1,2,3)}\";\nString result = new Toky().register(new Token(\"number\").replacedBy(new Generator() {\n    \n\t@Override\n\tpublic void inject(String[] args) {\n\t    // store the arguments\n\t}\n\n\t@Override\n\tpublic String generate() {\n\t    return args[0] + args[1] + args[2]; // some very sophisticated stuff happens here :)\n\t}\n})).substitute(toReplace);\nSystem.out.println(result); // i can count to 123\n```\nIf you prefer to use **index based tokens**, you can also use this:\n```Java\ntoky.register(new String[] { \"one\", \"two\", \"three\" });\ntoky.substitute(\"abc {0} {1} {2} def\"); // will produce \"abc one two three def\";\n```\n\n## Getting the Jar File\n\nvia Maven:\n```\n\u003cdependency\u003e\n    \u003cgroupId\u003ede.marcelsauer\u003c/groupId\u003e\n    \u003cartifactId\u003etokenreplacer\u003c/artifactId\u003e\n    \u003cversion\u003e1.3.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\nor just take the latest \"tokenreplacer-x.y.jar\" from the [downloads](http://github.com/niesfisch/tokenreplacer/downloads) section and put it in your classpath.\nIf you also need the sources and javadoc download the \"tokenreplacer-x.y-sources.jar\" / \"tokenreplacer-x.y-javadoc.jar\".\n\n## Licence\n\nVersion \u003e= 1.2 -\u003e Apache 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt\n\nVersion \u003c= 1.1 -\u003e GPL 3\n\n## Release Notes\n\n[Release Notes](http://github.com/niesfisch/tokenreplacer/blob/master/releasenotes.txt)\n        \n## Usage\n\nsimplest use case, only **static values**\n\n```Java\nTokenReplacer toky = new Toky().register(\"number\", \"123\");\ntoky.substitute(\"i can count to {number}\");\n```\n\nis same as registering an **explicit {@link Token}**\n\n```Java\ntoky = new Toky().register(new Token(\"number\").replacedBy(\"123\"));\ntoky.substitute(\"i can count to {number}\");\n```\n\nwe can also use a **{@link Generator}** to **dynamically** get the\nvalue (which here does not really make sense ;-))\n\n```Java\ntoky = new Toky().register(new Token(\"number\").replacedBy(new Generator() {\n\n\t @Override\n\t public void inject(String[] args) {\n\t     // not relevant here\n\t }\n\n\t @Override\n\t public String generate() {\n\t     return \"123\";\n\t }\n}));\n```\n\nhere we use a generator and **pass the arguments** \"a,b,c\" to it, they\nwill be injected via {@link Generator#inject(String[] args)} before the call\nto {@link Generator#generate()} is done. it is up to the generator to decide\nwhat to do with them. this feature makes handling tokens pretty powerful\nbecause you can write very dynamic generators.\n\n```Java\ntoky.substitute(\"i can count to {number(a,b,c)}\");\n```\n\nif you prefer to use **index based tokens**, you can also use this:\n \n```Java\ntoky.register(new String[] { \"one\", \"two\", \"three\" });\ntoky.substitute(\"abc {0} {1} {2} def\"); // will produce \"abc one two three def\";\n```\n\nof course you can replace all default **delimiters** with your preferred\nones, just make sure start and end are different.\n\n```Java\ntoky.withTokenStart(\"*\"); // default is '{'\ntoky.withTokenEnd(\"#\"); // default is '}'\ntoky.withArgumentDelimiter(\";\"); // default is ','\ntoky.withArgumentStart(\"[\"); // default is '('\ntoky.withArgumentEnd(\"]\"); // default is ')'\n```\n\nby default Toky will throw IllegalStateExceptions if there was no matching\nvalue or generator found for a token. you can **enable/disable generating\nexceptions**.\n\n```Java\ntoky.doNotIgnoreMissingValues(); // which is the DEFAULT\n```\n\nwill turn error reporting for missing values \u003cb\u003eOFF\u003c/b\u003e\n\n```Java\ntoky.ignoreMissingValues();\n```\n\nyou can **enable/disable generator caching**. if you enable caching once a\ngenerator for a token returned a value this value will be used for all\nsubsequent tokens with the same name\n\n```Java\ntoky.enableGeneratorCaching();\ntoky.disableGeneratorCaching();\n```\n\n\n## More Samples\n\nHave a look at [the unit test of Toky](http://github.com/niesfisch/tokenreplacer/blob/master/src/test/java/de/marcelsauer/tokenreplacer/TokyTest.java) to see some more samples\n\n## peeking into the source code and building from scratch\n\n    $ git clone http://github.com/niesfisch/tokenreplacer.git tokenreplacer\n    $ cd tokenreplacer\n    $ mvn clean install\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniesfisch%2Ftokenreplacer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fniesfisch%2Ftokenreplacer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniesfisch%2Ftokenreplacer/lists"}